mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Merge pull request #182 from Medios-lgtm/parse-number-slice-bounds
base: don't read past the end of the slice in parse_number_{u64,f64}
This commit is contained in:
4 files changed
+61
-36
No files matched your search
@@ -268,14 +268,12 @@ wuffs_private_impl__high_prec_dec__parse(wuffs_private_impl__high_prec_dec* h,
|
||||
uint8_t* q = s.ptr + s.len;
|
||||
|
||||
if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {
|
||||
for (;; p++) {
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
} else if (*p != '_') {
|
||||
break;
|
||||
}
|
||||
for (; (p < q) && (*p == '_'); p++) {
|
||||
}
|
||||
}
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
}
|
||||
|
||||
// Parse sign.
|
||||
do {
|
||||
@@ -288,14 +286,12 @@ wuffs_private_impl__high_prec_dec__parse(wuffs_private_impl__high_prec_dec* h,
|
||||
break;
|
||||
}
|
||||
if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {
|
||||
for (;; p++) {
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
} else if (*p != '_') {
|
||||
break;
|
||||
}
|
||||
for (; (p < q) && (*p == '_'); p++) {
|
||||
}
|
||||
}
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
}
|
||||
} while (0);
|
||||
|
||||
// Parse digits, up to (and including) a '.', 'E' or 'e'. Examples for each
|
||||
@@ -408,14 +404,12 @@ after_sep:
|
||||
after_exp:
|
||||
do {
|
||||
if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {
|
||||
for (;; p++) {
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
} else if (*p != '_') {
|
||||
break;
|
||||
}
|
||||
for (; (p < q) && (*p == '_'); p++) {
|
||||
}
|
||||
}
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
}
|
||||
|
||||
int32_t exp_sign = +1;
|
||||
if (*p == '+') {
|
||||
|
||||
@@ -215,6 +215,7 @@ wuffs_base__parse_number_u64(wuffs_base__slice_u8 s, uint32_t options) {
|
||||
if (p < q) {
|
||||
goto hexadecimal;
|
||||
}
|
||||
goto fail_bad_argument;
|
||||
|
||||
} else if ((*p == 'd') || (*p == 'D')) {
|
||||
p++;
|
||||
@@ -225,6 +226,7 @@ wuffs_base__parse_number_u64(wuffs_base__slice_u8 s, uint32_t options) {
|
||||
if (p < q) {
|
||||
goto decimal;
|
||||
}
|
||||
goto fail_bad_argument;
|
||||
}
|
||||
|
||||
if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES) {
|
||||
|
||||
@@ -21668,14 +21668,12 @@ wuffs_private_impl__high_prec_dec__parse(wuffs_private_impl__high_prec_dec* h,
|
||||
uint8_t* q = s.ptr + s.len;
|
||||
|
||||
if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {
|
||||
for (;; p++) {
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
} else if (*p != '_') {
|
||||
break;
|
||||
}
|
||||
for (; (p < q) && (*p == '_'); p++) {
|
||||
}
|
||||
}
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
}
|
||||
|
||||
// Parse sign.
|
||||
do {
|
||||
@@ -21688,14 +21686,12 @@ wuffs_private_impl__high_prec_dec__parse(wuffs_private_impl__high_prec_dec* h,
|
||||
break;
|
||||
}
|
||||
if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {
|
||||
for (;; p++) {
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
} else if (*p != '_') {
|
||||
break;
|
||||
}
|
||||
for (; (p < q) && (*p == '_'); p++) {
|
||||
}
|
||||
}
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
}
|
||||
} while (0);
|
||||
|
||||
// Parse digits, up to (and including) a '.', 'E' or 'e'. Examples for each
|
||||
@@ -21808,14 +21804,12 @@ after_sep:
|
||||
after_exp:
|
||||
do {
|
||||
if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {
|
||||
for (;; p++) {
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
} else if (*p != '_') {
|
||||
break;
|
||||
}
|
||||
for (; (p < q) && (*p == '_'); p++) {
|
||||
}
|
||||
}
|
||||
if (p >= q) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
}
|
||||
|
||||
int32_t exp_sign = +1;
|
||||
if (*p == '+') {
|
||||
@@ -23585,6 +23579,7 @@ wuffs_base__parse_number_u64(wuffs_base__slice_u8 s, uint32_t options) {
|
||||
if (p < q) {
|
||||
goto hexadecimal;
|
||||
}
|
||||
goto fail_bad_argument;
|
||||
|
||||
} else if ((*p == 'd') || (*p == 'D')) {
|
||||
p++;
|
||||
@@ -23595,6 +23590,7 @@ wuffs_base__parse_number_u64(wuffs_base__slice_u8 s, uint32_t options) {
|
||||
if (p < q) {
|
||||
goto decimal;
|
||||
}
|
||||
goto fail_bad_argument;
|
||||
}
|
||||
|
||||
if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES) {
|
||||
|
||||
@@ -1082,6 +1082,22 @@ test_wuffs_strconv_parse_number_f64_regular() {
|
||||
}
|
||||
}
|
||||
|
||||
// Input that ends right after a sign or an exponent marker must be rejected
|
||||
// without reading past the end of the slice. Use DEFAULT_OPTIONS, as
|
||||
// ALLOW_UNDERSCORES takes a different code path.
|
||||
{
|
||||
const char* strs[] = {"", "+", "-", "1e", "1E", "12.5e"};
|
||||
for (size_t i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(strs); i++) {
|
||||
wuffs_base__result_f64 r = wuffs_base__parse_number_f64(
|
||||
wuffs_base__make_slice_u8((uint8_t*)(void*)strs[i], strlen(strs[i])),
|
||||
WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS);
|
||||
if (r.status.repr != wuffs_base__error__bad_argument) {
|
||||
RETURN_FAIL("\"%s\": have \"%s\", want \"%s\"", strs[i],
|
||||
r.status.repr, wuffs_base__error__bad_argument);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1245,6 +1261,23 @@ test_wuffs_strconv_parse_number_u64() {
|
||||
}
|
||||
}
|
||||
|
||||
// A bare "0x" or "0d" prefix is invalid, even with
|
||||
// ALLOW_MULTIPLE_LEADING_ZEROES. Pass an explicit length (not strlen) so
|
||||
// that the byte just past the end of the slice is a digit, which must not be
|
||||
// read.
|
||||
{
|
||||
const char* strs[] = {"0x7", "0X7", "0d7", "0D7"};
|
||||
for (size_t i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(strs); i++) {
|
||||
wuffs_base__result_u64 r = wuffs_base__parse_number_u64(
|
||||
wuffs_base__make_slice_u8((uint8_t*)(void*)strs[i], 2),
|
||||
WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES);
|
||||
if (r.status.repr != wuffs_base__error__bad_argument) {
|
||||
RETURN_FAIL("\"%.2s\": have \"%s\", want \"%s\"", strs[i],
|
||||
r.status.repr, wuffs_base__error__bad_argument);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES.
|
||||
{
|
||||
for (int o = 0; o < 2; o++) {
|
||||
|
||||
Reference in new issue
Block a user