diff mbox series

xsysconf: Only fail on error results and errno set

Message ID 20210924023501.2994511-1-shorne@gmail.com
State New
Headers show
Series xsysconf: Only fail on error results and errno set | expand

Commit Message

Stafford Horne Sept. 24, 2021, 2:35 a.m. UTC
When testing nptl/tst-pthread-attr-affinity-fail fails with:

    error: xsysconf.c:33: sysconf (83): Cannot allocate memory
    error: 1 test failures

This happens as xsysconf checks the errno after running sysconf.
Internally the sysconf request for _SC_NPROCESSORS_CONF on linux
allocates memory.  But there is a problem, even though malloc succeeds
errno is getting set to ENOMEM.

POSIX allows successful calls to clobber errno.  So xsysconf just
checking errno is wrong.  Fix xsysconf by only failing if we have an
error result and errno is set.
---
 support/xsysconf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Florian Weimer Sept. 24, 2021, 6:27 a.m. UTC | #1
* Stafford Horne via Libc-alpha:

> When testing nptl/tst-pthread-attr-affinity-fail fails with:
>
>     error: xsysconf.c:33: sysconf (83): Cannot allocate memory
>     error: 1 test failures
>
> This happens as xsysconf checks the errno after running sysconf.
> Internally the sysconf request for _SC_NPROCESSORS_CONF on linux
> allocates memory.  But there is a problem, even though malloc succeeds
> errno is getting set to ENOMEM.
>
> POSIX allows successful calls to clobber errno.  So xsysconf just
> checking errno is wrong.  Fix xsysconf by only failing if we have an
> error result and errno is set.
> ---
>  support/xsysconf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/support/xsysconf.c b/support/xsysconf.c
> index 2607d3a720..fce7795417 100644
> --- a/support/xsysconf.c
> +++ b/support/xsysconf.c
> @@ -29,7 +29,7 @@ xsysconf (int name)
>    int old_errno = errno;
>    errno = 0;
>    long result = sysconf (name);
> -  if (errno != 0)
> +  if (result == -1 && errno != 0)
>      FAIL_EXIT1 ("sysconf (%d): %m", name);
>    errno = old_errno;
>    return result;

Looks good, thanks.

Florian
diff mbox series

Patch

diff --git a/support/xsysconf.c b/support/xsysconf.c
index 2607d3a720..fce7795417 100644
--- a/support/xsysconf.c
+++ b/support/xsysconf.c
@@ -29,7 +29,7 @@  xsysconf (int name)
   int old_errno = errno;
   errno = 0;
   long result = sysconf (name);
-  if (errno != 0)
+  if (result == -1 && errno != 0)
     FAIL_EXIT1 ("sysconf (%d): %m", name);
   errno = old_errno;
   return result;