[PATCH] Init errno before strto* calls.

Arkadiusz Miśkiewicz
2014-07-13T17:38:17Z

Eric Sandeen noted that strtol(3) and friends require errno
initialization. From (fresh) man page:

NOTES
Since strtol() can legitimately return 0, LONG_MAX,
or LONG_MIN (LLONG_MAX or LLONG_MIN for strtoll()) on both
success and failure, the calling program should set errno
to 0 before the call, and then determine if an error
occurred by checking whether errno has a non-zero
value after the call.

So do it.
---
libxcmd/input.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/libxcmd/input.c b/libxcmd/input.c
index 397a124..711b527 100644
--- a/libxcmd/input.c
+++ b/libxcmd/input.c
@@ -153,6 +153,7 @@ cvtnum(
char *sp;
int c;

+ errno = 0;
i = strtoll(s, &sp, 0);
if ((i == LLONG_MIN || i == LLONG_MAX) && errno == ERANGE)
return -1LL;
@@ -239,6 +240,7 @@ cvttime(
unsigned long i;
char *sp;

+ errno = 0;
i = strtoul(s, &sp, 0);
if (i == ULONG_MAX && errno == ERANGE)
return 0;
@@ -348,6 +350,7 @@ prid_from_string(
* Allow either a full numeric or a valid projectname, even
* if it starts with a digit.
*/
+ errno = 0;
prid_long = strtoul(project, &sp, 10);
if (*project != '\0' && *sp == '\0') {
if ((prid_long == ULONG_MAX && errno == ERANGE)
@@ -369,6 +372,7 @@ uid_from_string(
unsigned long uid_long;
char *sp;

+ errno = 0;
uid_long = strtoul(user, &sp, 10);
if (sp != user) {
if ((uid_long == ULONG_MAX && errno == ERANGE)
@@ -390,6 +394,7 @@ gid_from_string(
unsigned long gid_long;
char *sp;

+ errno = 0;
gid_long = strtoul(group, &sp, 10);
if (sp != group) {
if ((gid_long == ULONG_MAX && errno == ERANGE)
--
2.0.1