Hello all,

Found a little bug in sleep, for insanely large values (so insane people might use them to sleep forever and they will be disappointed when forever means zero) sleep might return immediately ( config SLEEP_FLOAT is impact, config SLEEP and config USLEEP are not affected).


Now sleep (with floating point support) functions as follows:

edb@lapedb:~/edb-stuff/toybox/toybox$ strace -e nanosleep ./toybox sleep 24855d
nanosleep({2147472000, 0}, ^C <unfinished ...>
edb@lapedb:~/edb-stuff/toybox/toybox$ strace -e nanosleep ./toybox sleep 24856d
nanosleep({2147558400, 2147483648}, NULL) = -1 EINVAL (Invalid argument)

So it'd better not sleep longer than 68 years :D.

This is caused by an overflow in the argument parsing, if it exceeds INT_MAX (0x80000000 on 32 bit) nanosleep will start to consider certain variables as negative. I've attached a patch which just tops this off to INT_MAX. With it, it behaves more or less as 'real' sleep:

edb@lapedb:~/edb-stuff/toybox/toybox$ strace -e nanosleep ./toybox sleep 24856d
nanosleep({2147483647, 0}, ^C <unfinished ...>


edb@lapedb:~/edb-stuff/toybox/toybox$ strace -e nanosleep  sleep 24856d
nanosleep({2147483647, 999999999}, ^C <unfinished ...>

(okay, one second offset, but let's not argue about that for the coming first 68 years). I would sleep much better tonight if somebody could also give this a spin on a 64 bit system.

Below proof that usleep and sleep function reasonably well.

* usleep:
edb@lapedb:~/edb-stuff/toybox/toybox$ strace -e nanosleep ./toybox usleep 500000000000000000000000000000000000000000000000000
nanosleep({2147, 483647000}, ^C <unfinished ...>

this because it bumps into the limits of the integer used)

 * sleep without floating point support

edb@lapedb:~/edb-stuff/toybox/toybox$ strace -e nanosleep ./toybox sleep 5000000000000000
nanosleep({2147483647, 0}, ^C <unfinished ...>

same, bumps into the limits of int).


my 2 cents
E.

--
Elie De Brauwer
.
# HG changeset patch
# User Elie De Brauwer <[email protected]>
# Date 1345034610 -7200
# Node ID bc5a57adc4d28578d21cf22cb007c1664cba1ad4
# Parent  131571cf708c03065d1f1f4417c0bea27b4bcae2
Avoid overflow in argument parsing causing an -EINVAL

diff -r 131571cf708c -r bc5a57adc4d2 toys/sleep.c
--- a/toys/sleep.c	Wed Aug 15 12:53:54 2012 +0200
+++ b/toys/sleep.c	Wed Aug 15 14:43:30 2012 +0200
@@ -45,6 +45,7 @@
 			d *= ismhd[c-smhd];
 		}
 
+		if (d > INT_MAX) d = INT_MAX;
 		tv.tv_nsec=1000000000*(d-(tv.tv_sec = (unsigned long)d));
 		toys.exitval = !!nanosleep(&tv, NULL);
 	}
_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to