diff options
author | Bruno Haible <bruno@clisp.org> | 2022-06-19 16:14:19 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2022-06-19 16:14:19 +0200 |
commit | 3c406e6f08f6b4dec1a3adb16477559f5235a072 (patch) | |
tree | feb27027f39812541583776734712fa2246fa542 /tests/test-getlogin.h | |
parent | d682f8de7f9d384f4cfc482a3ba2960329a8db21 (diff) | |
download | gnulib-3c406e6f08f6b4dec1a3adb16477559f5235a072.tar.gz |
getlogin, getlogin_r tests: Really avoid test failure.
Reported by Letu Ren <fantasquex@gmail.com> in
<https://lists.gnu.org/archive/html/bug-gnulib/2022-06/msg00037.html>.
* tests/test-getlogin.h (test_getlogin_result): Parse the contents of
/proc/self/loginuid as an unsigned integer.
Diffstat (limited to 'tests/test-getlogin.h')
-rw-r--r-- | tests/test-getlogin.h | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/tests/test-getlogin.h b/tests/test-getlogin.h index 3489e3e03d..fcc7741f12 100644 --- a/tests/test-getlogin.h +++ b/tests/test-getlogin.h @@ -51,16 +51,25 @@ test_getlogin_result (const char *buf, int err) #if defined __linux__ /* On Linux, it is possible to set up a chroot environment in such a way that stdin is connected to a tty and nervertheless /proc/self/loginuid - contains "-1". In this situation, getlogin() and getlogin_r() fail; - this is expected. */ + contains the value (uid_t)(-1). In this situation, getlogin() and + getlogin_r() fail; this is expected. */ bool loginuid_undefined = false; - /* Does the special file /proc/self/loginuid contain "-1"? */ + /* Does the special file /proc/self/loginuid contain the value + (uid_t)(-1)? */ FILE *fp = fopen ("/proc/self/loginuid", "r"); if (fp != NULL) { - char buf[3]; - loginuid_undefined = - (fread (buf, 1, 3, fp) == 2 && buf[0] == '-' && buf[1] == '1'); + char buf[21]; + size_t n = fread (buf, 1, sizeof buf, fp); + if (n > 0 && n < sizeof buf) + { + buf[n] = '\0'; + errno = 0; + char *endptr; + unsigned long value = strtoul (buf, &endptr, 10); + if (*endptr == '\0' && errno == 0) + loginuid_undefined = ((uid_t) value == (uid_t)(-1)); + } fclose (fp); } if (loginuid_undefined) |