Skip to content

Commit

Permalink
sleep() with negative argument makes no sense.
Browse files Browse the repository at this point in the history
Give by default a warning, do not sleep, and return zero.

(the signedness problem detected by Coverity, CID 104844)

Other options would include not giving a warning at all,
giving a warning only if asked, and finally croaking.
(Python had this problem earlier, and chose croaking.)

Earlier discussion in
http://www.nntp.perl.org/group/perl.perl5.porters/2015/03/msg226304.html

See also:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/sleep.html
http://linux.die.net/man/3/sleep
https://www.freebsd.org/cgi/man.cgi?query=sleep&sektion=3
  • Loading branch information
jhi committed Jun 27, 2015
1 parent add0dc9 commit 393bc9b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pp_sys.c
Expand Up @@ -4749,7 +4749,16 @@ PP(pp_sleep)
PerlProc_pause();
else {
duration = POPi;
PerlProc_sleep((unsigned int)duration);
if (duration < 0) {
/* diag_listed_as: %s() with negative argument */
Perl_ck_warner_d(aTHX_ packWARN(WARN_MISC),
"sleep() with negative argument");
SETERRNO(EINVAL, LIB_INVARG);
XPUSHi(0);
RETURN;
} else {
PerlProc_sleep((unsigned int)duration);
}
}
(void)time(&when);
XPUSHi(when - lasttime);
Expand Down
9 changes: 9 additions & 0 deletions t/lib/warnings/pp_sys
Expand Up @@ -930,3 +930,12 @@ alarm(-1);

EXPECT
alarm() with negative argument at - line 2.

########
# pp_sys.c [pp_sleep]
sleep(-1);
no warnings "misc";
sleep(-1);

EXPECT
sleep() with negative argument at - line 2.

0 comments on commit 393bc9b

Please sign in to comment.