Discussion:
fix nap.h logic
(too old to reply)
Bruno Haible
2017-04-24 22:04:25 UTC
Permalink
Raw Message
With MSVC 14, test-utimens fails:

../../gltests/nap.h:69: assertion 'nanosleep (&delay_spec, 0) == 0' failed

Obviously a negative delay has been passed to nanosleep.
The code in nap.h is wrong: it multiplies the delay by 2, but a signed integer
eventually wraps around and becomes negative.

This fixes it.


2017-04-23 Bruno Haible <***@clisp.org>

nap.h: Fix logic.
* tests/nap.h (nap): Avoid signed integer overflow in loop.

diff --git a/tests/nap.h b/tests/nap.h
index b2726d7..ce8f95a 100644
--- a/tests/nap.h
+++ b/tests/nap.h
@@ -117,9 +117,18 @@ nap (void)
delay = delay / 2; /* Try half of the previous delay. */
ASSERT (0 < delay);

- for ( ; delay <= 2147483647; delay = delay * 2)
- if (nap_works (nap_fd, delay, old_st))
- return;
+ for (;;)
+ {
+ if (nap_works (delay, old_st))
+ return;
+ if (delay <= (2147483647 - 1) / 2)
+ {
+ delay = delay * 2 + 1;
+ continue;
+ }
+ else
+ break;
+ }

/* Bummer: even the highest nap delay didn't work. */
ASSERT (0);

Loading...