Skip to content

Commit

Permalink
lib: fix strntol's end pointer when str has leading spaces
Browse files Browse the repository at this point in the history
Fix unittests/lib/strntol.c test case failure.

Given behavior of strtol(3), "end" pointer should be calculated
based on "beg" pointer instead of "str" on success.

Otherwise if "str" has leading spaces (which are explicitly ignored
by strntol()), "end" won't point to "the first invalid character"
for not counting those spaces.

Glibc's strtol(3) says as follows.
--
 If endptr is not NULL, strtol() stores the address of the first
 invalid character in *endptr.  If there were no digits at all,
 strtol() stores the original value of nptr in *endptr (and returns 0).
 In particular, if *nptr is not '\0' but **endptr is '\0' on return,
 the entire string is valid.

Signed-off-by: Tomohiro Kusumi <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
kusumi authored and axboe committed Oct 26, 2018
1 parent 2f84d0c commit a9d9582
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/strntol.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ long strntol(const char *str, size_t sz, char **end, int base)
if (ret == LONG_MIN || ret == LONG_MAX)
return ret;
if (end)
*end = (char *)str + (*end - buf);
*end = (char *)beg + (*end - buf);
return ret;
}

0 comments on commit a9d9582

Please sign in to comment.