Browse Source

Initialize errno before calling sscanf in json_parse_int64() so parsing valid numbers after parsing an out of range number works.

tags/json-c-0.11-20130402
Eric Haszlakiewicz 13 years ago
parent
commit
77c6239465
3 changed files with 19 additions and 3 deletions
  1. +6
    -3
      json_util.c
  2. +10
    -0
      tests/test_parse_int64.c
  3. +3
    -0
      tests/test_parse_int64.expected

+ 6
- 3
json_util.c View File

@@ -147,11 +147,14 @@ int json_parse_int64(const char *buf, int64_t *retval)
int64_t num64; int64_t num64;
const char *buf_skip_space; const char *buf_skip_space;
int orig_has_neg; int orig_has_neg;
int _errno;
errno = 0; // sscanf won't always set errno, so initialize
if (sscanf(buf, "%" SCNd64, &num64) != 1) if (sscanf(buf, "%" SCNd64, &num64) != 1)
{ {
MC_DEBUG("Failed to parse, sscanf != 1\n"); MC_DEBUG("Failed to parse, sscanf != 1\n");
return 1; return 1;
} }
_errno = errno;
buf_skip_space = buf; buf_skip_space = buf;
orig_has_neg = 0; orig_has_neg = 0;
// Skip leading spaces // Skip leading spaces
@@ -168,7 +171,7 @@ int json_parse_int64(const char *buf, int64_t *retval)
if (buf_skip_space[0] == '0' && buf_skip_space[1] == '\0') if (buf_skip_space[0] == '0' && buf_skip_space[1] == '\0')
orig_has_neg = 0; // "-0" is the same as just plain "0" orig_has_neg = 0; // "-0" is the same as just plain "0"
if (errno != ERANGE)
if (_errno != ERANGE)
{ {
char buf_cmp[100]; char buf_cmp[100];
char *buf_cmp_start = buf_cmp; char *buf_cmp_start = buf_cmp;
@@ -196,10 +199,10 @@ int json_parse_int64(const char *buf, int64_t *retval)
) )
) )
{ {
errno = ERANGE;
_errno = ERANGE;
} }
} }
if (errno == ERANGE)
if (_errno == ERANGE)
{ {
if (orig_has_neg) if (orig_has_neg)
num64 = INT64_MIN; num64 = INT64_MIN;


+ 10
- 0
tests/test_parse_int64.c View File

@@ -80,6 +80,9 @@ int main()
strcpy(buf, "-21474836480"); // INT32_MIN * 10 strcpy(buf, "-21474836480"); // INT32_MIN * 10
checkit(buf); checkit(buf);


strcpy(buf, "9223372036854775806"); // INT64_MAX - 1
checkit(buf);

strcpy(buf, "9223372036854775807"); // INT64_MAX strcpy(buf, "9223372036854775807"); // INT64_MAX
checkit(buf); checkit(buf);


@@ -92,6 +95,9 @@ int main()
strcpy(buf, "-9223372036854775809"); // INT64_MIN - 1 strcpy(buf, "-9223372036854775809"); // INT64_MIN - 1
checkit(buf); checkit(buf);


strcpy(buf, "18446744073709551614"); // UINT64_MAX - 1
checkit(buf);

strcpy(buf, "18446744073709551615"); // UINT64_MAX strcpy(buf, "18446744073709551615"); // UINT64_MAX
checkit(buf); checkit(buf);


@@ -101,5 +107,9 @@ int main()
strcpy(buf, "-18446744073709551616"); // -UINT64_MAX strcpy(buf, "-18446744073709551616"); // -UINT64_MAX
checkit(buf); checkit(buf);


// Ensure we can still parse valid numbers after parsing out of range ones.
strcpy(buf, "123");
checkit(buf);

return 0; return 0;
} }

+ 3
- 0
tests/test_parse_int64.expected View File

@@ -17,10 +17,13 @@ buf=-2147483647 parseit=0, value=-2147483647
buf=-2147483648 parseit=0, value=-2147483648 buf=-2147483648 parseit=0, value=-2147483648
buf=-2147483649 parseit=0, value=-2147483649 buf=-2147483649 parseit=0, value=-2147483649
buf=-21474836480 parseit=0, value=-21474836480 buf=-21474836480 parseit=0, value=-21474836480
buf=9223372036854775806 parseit=0, value=9223372036854775806
buf=9223372036854775807 parseit=0, value=9223372036854775807 buf=9223372036854775807 parseit=0, value=9223372036854775807
buf=9223372036854775808 parseit=0, value=9223372036854775807 buf=9223372036854775808 parseit=0, value=9223372036854775807
buf=-9223372036854775808 parseit=0, value=-9223372036854775808 buf=-9223372036854775808 parseit=0, value=-9223372036854775808
buf=-9223372036854775809 parseit=0, value=-9223372036854775808 buf=-9223372036854775809 parseit=0, value=-9223372036854775808
buf=18446744073709551614 parseit=0, value=9223372036854775807
buf=18446744073709551615 parseit=0, value=9223372036854775807 buf=18446744073709551615 parseit=0, value=9223372036854775807
buf=18446744073709551616 parseit=0, value=9223372036854775807 buf=18446744073709551616 parseit=0, value=9223372036854775807
buf=-18446744073709551616 parseit=0, value=-9223372036854775808 buf=-18446744073709551616 parseit=0, value=-9223372036854775808
buf=123 parseit=0, value=123

Loading…
Cancel
Save