From: Bruno Haible Date: Sun, 19 Jun 2022 14:14:19 +0000 (+0200) Subject: getlogin, getlogin_r tests: Really avoid test failure. X-Git-Tag: v1.0~2261 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=3c406e6f08f6b4dec1a3adb16477559f5235a072;p=gnulib.git getlogin, getlogin_r tests: Really avoid test failure. Reported by Letu Ren in . * tests/test-getlogin.h (test_getlogin_result): Parse the contents of /proc/self/loginuid as an unsigned integer. --- diff --git a/ChangeLog b/ChangeLog index 2daa6d8c81..c9975f436b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2022-06-19 Bruno Haible + + getlogin, getlogin_r tests: Really avoid test failure. + Reported by Letu Ren in + . + * tests/test-getlogin.h (test_getlogin_result): Parse the contents of + /proc/self/loginuid as an unsigned integer. + 2022-06-12 Paul Eggert fchmodat: port better to MS-Windows etc. 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)