nanosleep: Work around bug on newer 32-bit mingw.
authorBruno Haible <bruno@clisp.org>
Sun, 4 Sep 2022 11:26:19 +0000 (13:26 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 5 Sep 2022 01:58:07 +0000 (03:58 +0200)
* m4/nanosleep.m4 (gl_FUNC_NANOSLEEP): Test for 32-bit mingw bug.
* tests/test-nanosleep.c (main): Add another test.
* doc/posix-functions/nanosleep.texi: Mention the mingw bug.

ChangeLog
doc/posix-functions/nanosleep.texi
m4/nanosleep.m4
tests/test-nanosleep.c

index ab704f151078c561feb45580f7e8ecc525a16592..97f1a5a35e0f3d13fb72ed13471c8a087d4628f4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2022-09-04  Bruno Haible  <bruno@clisp.org>
+
+       nanosleep: Work around bug on newer 32-bit mingw.
+       * m4/nanosleep.m4 (gl_FUNC_NANOSLEEP): Test for 32-bit mingw bug.
+       * tests/test-nanosleep.c (main): Add another test.
+       * doc/posix-functions/nanosleep.texi: Mention the mingw bug.
+
 2022-09-03  Bruno Haible  <bruno@clisp.org>
 
        fmal: Work around glibc 2.17 bug on x86_64.
index 8ede0b01f90a34650b829b9f466972e48a1e8111..be54aef571969a26a654270dfe5e83f1caf018f6 100644 (file)
@@ -18,6 +18,10 @@ Linux 64-bit, Solaris 64-bit.
 @item
 This function cannot sleep longer than 49.7 days on some platforms:
 Cygwin 1.5.x.
+@item
+This function does not fail when passed a negative nanosecond value on some
+platforms:
+newer 32-bit mingw.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index 6a51f28e302f64088a921c8b21beeafb97876196..56d6f1f4aadc93ab146c1d947d51141900eadce0 100644 (file)
@@ -1,4 +1,4 @@
-# serial 40
+# serial 40.1
 
 dnl From Jim Meyering.
 dnl Check for the nanosleep function.
@@ -106,15 +106,22 @@ AC_DEFUN([gl_FUNC_NANOSLEEP],
             #else /* A simpler test for native Windows.  */
             if (nanosleep (&ts_sleep, &ts_remaining) < 0)
               return 3;
+            /* Test for 32-bit mingw bug: negative nanosecond values do not
+               cause failure.  */
+            ts_sleep.tv_sec = 1;
+            ts_sleep.tv_nsec = -1;
+            if (nanosleep (&ts_sleep, &ts_remaining) != -1)
+              return 7;
             #endif
             return 0;
           }]])],
        [gl_cv_func_nanosleep=yes],
-       [case $? in dnl (
-        4|5|6) gl_cv_func_nanosleep='no (mishandles large arguments)';; dnl (
-        *)   gl_cv_func_nanosleep=no;;
+       [case $? in
+        4|5|6) gl_cv_func_nanosleep='no (mishandles large arguments)' ;;
+        7)     gl_cv_func_nanosleep='no (mishandles negative tv_nsec)' ;;
+        *)     gl_cv_func_nanosleep=no ;;
         esac],
-       [case "$host_os" in dnl ((
+       [case "$host_os" in
           linux*) # Guess it halfway works when the kernel is Linux.
             gl_cv_func_nanosleep='guessing no (mishandles large arguments)' ;;
           mingw*) # Guess no on native Windows.
index c03a2c2ccf6fb93b607c7f0a97f3297eaa52f65c..c8ad3b36ee7f374c4a9d5e08ef981a5bf953e687 100644 (file)
@@ -43,16 +43,27 @@ main (void)
 {
   struct timespec ts;
 
+  /* Check that negative nanosecond values cause failure.  */
+  ts.tv_sec = 1;
+  ts.tv_nsec = -1;
+  errno = 0;
+  ASSERT (nanosleep (&ts, NULL) == -1);
+  ASSERT (errno == EINVAL);
+
   ts.tv_sec = 1000;
   ts.tv_nsec = -1;
   errno = 0;
   ASSERT (nanosleep (&ts, NULL) == -1);
   ASSERT (errno == EINVAL);
+
+  /* Check that too large nanosecond values cause failure.  */
+  ts.tv_sec = 1000;
   ts.tv_nsec = 1000000000;
   errno = 0;
   ASSERT (nanosleep (&ts, NULL) == -1);
   ASSERT (errno == EINVAL);
 
+  /* Check successful call.  */
   ts.tv_sec = 0;
   ts.tv_nsec = 1;
   ASSERT (nanosleep (&ts, &ts) == 0);