Skip to content

Commit

Permalink
[Minor] Backport fixes from libucl
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Jan 15, 2023
1 parent 38600c0 commit 3c6ca1d
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions contrib/libucl/ucl_parser.c
Expand Up @@ -179,7 +179,7 @@ ucl_skip_comments (struct ucl_parser *parser)
if (!quoted) {
if (*p == '*') {
ucl_chunk_skipc (chunk, p);
if (*p == '/') {
if (chunk->remain > 0 && *p == '/') {
comments_nested --;
if (comments_nested == 0) {
if (parser->flags & UCL_PARSER_SAVE_COMMENTS) {
Expand Down Expand Up @@ -746,13 +746,13 @@ ucl_maybe_parse_number (ucl_object_t *obj,
const char *p = start, *c = start;
char *endptr;
bool got_dot = false, got_exp = false, need_double = false,
is_time = false, valid_start = false, is_hex = false,
is_neg = false;
is_time = false, valid_start = false, is_hex = false;
int is_neg = 0;
double dv = 0;
int64_t lv = 0;

if (*p == '-') {
is_neg = true;
is_neg = 1;
c ++;
p ++;
}
Expand All @@ -768,6 +768,7 @@ ucl_maybe_parse_number (ucl_object_t *obj,
is_hex = true;
allow_double = false;
c = p + 1;
p ++;
}
else if (allow_double) {
if (p == c) {
Expand Down Expand Up @@ -816,41 +817,68 @@ ucl_maybe_parse_number (ucl_object_t *obj,
break;
}
}
else if (!allow_double && *p == '.') {
/* Unexpected dot */
*pos = start;
return EINVAL;
}
else {
break;
}
}

if (!valid_start) {
if (!valid_start || p == c) {
*pos = start;
return EINVAL;
}

char numbuf[128];

if ((size_t)(p - c + 1) >= sizeof(numbuf)) {
*pos = start;
return EINVAL;
}

if (is_neg) {
numbuf[0] = '-';
ucl_strlcpy (&numbuf[1], c, p - c + 1);
}
else {
ucl_strlcpy (numbuf, c, p - c + 1);
}

errno = 0;
if (need_double) {
dv = strtod (c, &endptr);
dv = strtod (numbuf, &endptr);
}
else {
if (is_hex) {
lv = strtoimax (c, &endptr, 16);
lv = strtoimax (numbuf, &endptr, 16);
}
else {
lv = strtoimax (c, &endptr, 10);
lv = strtoimax (numbuf, &endptr, 10);
}
}
if (errno == ERANGE) {
*pos = start;
return ERANGE;
}

/* Now check endptr */
/* Now check endptr and move it from numbuf to the real ending */
if (endptr != NULL) {
long shift = endptr - numbuf - is_neg;
endptr = (char *)c + shift;
}
if (endptr >= end) {
p = end;
goto set_obj;
}
if (endptr == NULL || ucl_lex_is_atom_end (*endptr) || *endptr == '\0') {
p = endptr;
goto set_obj;
}

if (endptr < end && endptr != start) {
p = endptr;
switch (*p) {
case 'm':
case 'M':
Expand Down Expand Up @@ -983,7 +1011,7 @@ ucl_maybe_parse_number (ucl_object_t *obj,
}
else if (endptr == end) {
/* Just a number at the end of chunk */
p = endptr;
p = end;
goto set_obj;
}

Expand All @@ -999,11 +1027,11 @@ ucl_maybe_parse_number (ucl_object_t *obj,
else {
obj->type = UCL_TIME;
}
obj->value.dv = is_neg ? (-dv) : dv;
obj->value.dv = dv;
}
else {
obj->type = UCL_INT;
obj->value.iv = is_neg ? (-lv) : lv;
obj->value.iv = lv;
}
}
*pos = p;
Expand Down

0 comments on commit 3c6ca1d

Please sign in to comment.