Skip to content

Commit

Permalink
Issue #792 - set errno=EINVAL if parsing the string in json_parse_int…
Browse files Browse the repository at this point in the history
…64 fails, to match the docs for json_object_get_int.
  • Loading branch information
hawicz committed Oct 26, 2022
1 parent 777dd06 commit 57bef5e
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion json_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,12 @@ int json_parse_int64(const char *buf, int64_t *retval)
val = strtoll(buf, &end, 10);
if (end != buf)
*retval = val;
return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
if ((val == 0 && errno != 0) || (end == buf))

This comment has been minimized.

Copy link
@fedepell

fedepell Oct 29, 2022

Is the first part of the check correct?

man page on my system says:

RETURN VALUE
       The strtol() function returns the result of the conversion, unless the value would underflow or overflow.  If an underflow occurs, strtol() returns LONG_MIN.  If an overflow occurs, strtol() returns LONG_MAX.  In both cases, errno is set to
       ERANGE.  Precisely the same holds for strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX).

So ie would a val = LONG_MIN with errno == ERANGE wrongly pass the test here? Should val==0 be dropped?

This comment has been minimized.

Copy link
@hawicz

hawicz Oct 30, 2022

Author Member

That is correct, an overflow or underflow are not considered errors. For the usage of json_parse_int64 in json_object_get_int I believe that is reasonably sensible behavior. For the usage in json_tokener_parse_ex it would probably make more sense to consider that a error that causes the parsing to fail entirely, but that would be a fairly significant behavior change. I'm taking a look at doing this, gated behind the JSON_TOKENER_STRICT flag.
As for json_parse_int64, I'll document it as setting errno=ERANGE.

{
errno = EINVAL;
return 1;
}
return 0;
}

int json_parse_uint64(const char *buf, uint64_t *retval)
Expand Down

0 comments on commit 57bef5e

Please sign in to comment.