Browse Source

Fix parse bug with numbers between 2^63 and 2^64-1.

If a bad number is encountered, all numbers after it are set to INT_MAX
until some syscall resets errno, which may or may not happen.

This is caused by incorrect use of errno, which is unchanged if sscanf succeeds.
In glibc, sscanf succeeds for int64 values between 2^63 and 2^64-1 despite the number
being out of range. This causes the sanity check to fail.

The check for ERANGE was incorrect -- if sscanf succeeds, it cannot change errno.
Hence, the only place where ERANGE could be set is in the sanity check.
pull/38/head
Patrick Horn 13 years ago
parent
commit
35d0a7cf85
1 changed files with 5 additions and 10 deletions
  1. +5
    -10
      json_util.c

+ 5
- 10
json_util.c View File

@@ -167,8 +167,7 @@ int json_parse_int64(const char *buf, int64_t *retval)
buf_skip_space++;
if (buf_skip_space[0] == '0' && buf_skip_space[1] == '\0')
orig_has_neg = 0; // "-0" is the same as just plain "0"
if (errno != ERANGE)

{
char buf_cmp[100];
char *buf_cmp_start = buf_cmp;
@@ -196,16 +195,12 @@ int json_parse_int64(const char *buf, int64_t *retval)
)
)
{
errno = ERANGE;
if (orig_has_neg)
num64 = INT64_MIN;
else
num64 = INT64_MAX;
}
}
if (errno == ERANGE)
{
if (orig_has_neg)
num64 = INT64_MIN;
else
num64 = INT64_MAX;
}
*retval = num64;
return 0;
}


Loading…
Cancel
Save