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

This commit is contained in:
Eric Haszlakiewicz
2012-07-29 12:13:54 -05:00
parent 2da148df56
commit 77c6239465
3 changed files with 19 additions and 3 deletions

View File

@@ -147,11 +147,14 @@ int json_parse_int64(const char *buf, int64_t *retval)
int64_t num64;
const char *buf_skip_space;
int orig_has_neg;
int _errno;
errno = 0; // sscanf won't always set errno, so initialize
if (sscanf(buf, "%" SCNd64, &num64) != 1)
{
MC_DEBUG("Failed to parse, sscanf != 1\n");
return 1;
}
_errno = errno;
buf_skip_space = buf;
orig_has_neg = 0;
// 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')
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_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)
num64 = INT64_MIN;