Letu Ren wrote: > Thanks for the patch.I back-ported your patch and tested in ArchLinux > chroot. It still failed because the type of /proc/self/loginuid is uid_t > not string.
Thanks for testing and reporting back. > By the way, I accidentally created a new thread in the mailing list > because I'm not familiar with `git send-email`. That's not a problem; we can live with that. But there's no need to follow up in private email; please always keep the list in CC. > I made some changes based on your patch and tested > the patch under ArchLinux x86_64 chroot. > > > * tests/test-getlogin.h (test_getlogin_result): > According to the source code of glibc, the type of /proc/self/loginuid is > uid_t, which is unsigned int. > > Signed-off-by: Letu Ren <fantasq...@gmail.com> > --- > tests/test-getlogin.h | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/tests/test-getlogin.h b/tests/test-getlogin.h > index 3489e3e03..b5ed8b572 100644 > --- a/tests/test-getlogin.h > +++ b/tests/test-getlogin.h > @@ -58,9 +58,9 @@ test_getlogin_result (const char *buf, int err) > FILE *fp = fopen ("/proc/self/loginuid", "r"); > if (fp != NULL) > { > - char buf[3]; > + uid_t uid; > loginuid_undefined = > - (fread (buf, 1, 3, fp) == 2 && buf[0] == '-' && buf[1] == '1'); > + (fscanf (fp, "%u", &uid) == 1 && uid == (uid_t) -1); > fclose (fp); > } I don't like the *scanf family of functions. For a simple parsing function like strtoul(), I can understand for which inputs it behaves in which ways (empty input? not integer syntax? leading minus sign? unsigned integer overflow?); for fscanf there are just too many cases, I can't wrap my head around. So I'm committing this instead. 2022-06-19 Bruno Haible <br...@clisp.org> getlogin, getlogin_r tests: Really avoid test failure. Reported by Letu Ren <fantasq...@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. 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)