Browse Source

Fixed a bug in parsing 64 bit integers

Return value errno == ERANGE is a bit tricky, because its 1)
non-standard and 2) it does NOT get reset across multiple calls. So once
we get an ERANGE error all consecutive parses will fail with an ERANGE.
pull/64/head
Iskren Chernev 12 years ago
parent
commit
a396aa2c50
1 changed files with 9 additions and 4 deletions
  1. +9
    -4
      json_util.c

+ 9
- 4
json_util.c View File

@@ -141,11 +141,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 sscanf_errno;
errno = 0;
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;
} }
sscanf_errno = errno;
buf_skip_space = buf; buf_skip_space = buf;
orig_has_neg = 0; orig_has_neg = 0;
// Skip leading spaces // Skip leading spaces
@@ -161,9 +164,11 @@ int json_parse_int64(const char *buf, int64_t *retval)
buf_skip_space++; buf_skip_space++;
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 (sscanf_errno != ERANGE)
{ {
// Some platforms don't support ERANGE for sscanf, so we
// perform a manual check
char buf_cmp[100]; char buf_cmp[100];
char *buf_cmp_start = buf_cmp; char *buf_cmp_start = buf_cmp;
int recheck_has_neg = 0; int recheck_has_neg = 0;
@@ -190,10 +195,10 @@ int json_parse_int64(const char *buf, int64_t *retval)
) )
) )
{ {
errno = ERANGE;
sscanf_errno = ERANGE;
} }
} }
if (errno == ERANGE)
if (sscanf_errno == ERANGE)
{ {
if (orig_has_neg) if (orig_has_neg)
num64 = INT64_MIN; num64 = INT64_MIN;


Loading…
Cancel
Save