free: Parse -s option correctly.

If the -s option was the first option on the command line, free
would report seconds argument failed. This only appeared on the
Debian free, not the one in git.

Closer examination revealed that if a valid float string is
given to strtof() it doesn't set errno to 0, but just leaves it
alone. As we are explicitly testing errno for overflows, this
means the previous errno change is picked up here.

The simple answer is to set errno to 0 before calling strtof().

References:
 https://bugs.debian/org/733758
 https://enc.com.au/2015/08/08/be-careful-with-errno/
This commit is contained in:
Craig Small
2015-08-08 21:04:01 +10:00
parent 1f67b7dc71
commit fd007d6d1d
2 changed files with 2 additions and 0 deletions

1
free.c
View File

@ -323,6 +323,7 @@ int main(int argc, char **argv)
break;
case 's':
flags |= FREE_REPEAT;
errno = 0;
args.repeat_interval = (1000000 * strtof(optarg, &endptr));
if (errno || optarg == endptr || (endptr && *endptr))
xerrx(EXIT_FAILURE, _("seconds argument `%s' failed"), optarg);