[number] Handle rejection of invalid input better

Previously it was reporting the far it could parse on such cases but
with this change it's able to revert to what it couldn't parse.
This commit is contained in:
Ebrahim committed 2026-10-09 01:03:07 +03:30
1 parent cc5fef8df1
commit 8ec1db922b
3 files changed
+228 -56

No files matched your search

+125 -52
View File
@@ -33,65 +33,80 @@
#line 35 "hb-number-parser.hh"
static const unsigned char _double_parser_trans_keys[] = {
0u, 0u, 43u, 57u, 46u, 57u, 48u, 57u, 43u, 57u, 48u, 57u, 48u, 101u, 48u, 57u,
0u, 0u, 46u, 57u, 48u, 57u, 43u, 57u, 48u, 57u, 43u, 57u, 48u, 101u, 48u, 57u,
46u, 101u, 0
};
static const char _double_parser_key_spans[] = {
0, 15, 12, 10, 15, 10, 54, 10,
0, 12, 10, 15, 10, 15, 54, 10,
56
};
static const unsigned char _double_parser_index_offsets[] = {
0, 0, 16, 29, 40, 56, 67, 122,
0, 0, 13, 24, 40, 51, 67, 122,
133
};
static const char _double_parser_indicies[] = {
0, 1, 2, 3, 1, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4,
1, 3, 1, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 1, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
1, 6, 1, 7, 1, 1, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
1, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 1, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 9, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 9, 1, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 1, 3, 1,
4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 9, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 9, 1, 0
0, 1, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
1, 5, 4, 6, 4, 4, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7,
4, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 4, 8, 1, 9, 0,
1, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 1, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 11, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 11, 10, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 10, 12, 10,
2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 11, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 11, 10, 0
};
static const char _double_parser_trans_targs[] = {
2, 0, 2, 3, 8, 6, 5, 5,
7, 4
2, 0, 8, 6, 5, 4, 4, 7,
1, 1, 5, 3, 6
};
static const char _double_parser_trans_actions[] = {
0, 0, 1, 0, 2, 3, 0, 4,
5, 0
0, 0, 1, 2, 3, 0, 4, 5,
0, 8, 9, 0, 10
};
static const int double_parser_start = 1;
static const int double_parser_first_final = 6;
static const char _double_parser_to_state_actions[] = {
0, 0, 0, 0, 0, 6, 0, 0,
0
};
static const char _double_parser_from_state_actions[] = {
0, 0, 0, 0, 0, 7, 0, 0,
0
};
static const char _double_parser_eof_trans[] = {
0, 0, 0, 5, 5, 0, 11, 11,
11
};
static const int double_parser_start = 5;
static const int double_parser_first_final = 5;
static const int double_parser_error = 0;
static const int double_parser_en_main = 1;
static const int double_parser_en_main = 5;
#line 70 "hb-number-parser.rl"
#line 76 "hb-number-parser.rl"
/* Works only for n < 512 */
@@ -137,15 +152,25 @@ strtod_rl (const char *p, const char **end_ptr /* IN/OUT */)
const char *pe = *end_ptr;
while (p < pe && ISSPACE (*p))
p++;
const char *p_start = p; /* subject sequence must start here */
int cs;
const char *ts = p;
const char *te = p;
int act = 0;
const char *eof = pe;
bool matched = false;
(void) act;
#line 144 "hb-number-parser.hh"
#line 166 "hb-number-parser.hh"
{
cs = double_parser_start;
ts = 0;
te = 0;
act = 0;
}
#line 149 "hb-number-parser.hh"
#line 174 "hb-number-parser.hh"
{
int _slen;
int _trans;
@@ -156,6 +181,14 @@ strtod_rl (const char *p, const char **end_ptr /* IN/OUT */)
if ( cs == 0 )
goto _out;
_resume:
switch ( _double_parser_from_state_actions[cs] ) {
case 7:
#line 1 "NONE"
{ts = p;}
break;
#line 190 "hb-number-parser.hh"
}
_keys = _double_parser_trans_keys + (cs<<1);
_inds = _double_parser_indicies + _double_parser_index_offsets[cs];
@@ -164,13 +197,14 @@ _resume:
(*p) <= _keys[1] ?
(*p) - _keys[0] : _slen ];
_eof_trans:
cs = _double_parser_trans_targs[_trans];
if ( _double_parser_trans_actions[_trans] == 0 )
goto _again;
switch ( _double_parser_trans_actions[_trans] ) {
case 1:
case 8:
#line 37 "hb-number-parser.rl"
{ neg = true; }
break;
@@ -178,13 +212,44 @@ _resume:
#line 38 "hb-number-parser.rl"
{ exp_neg = true; }
break;
case 2:
case 5:
#line 52 "hb-number-parser.rl"
{
if (likely (exp * 10 + ((*p) - '0') <= MAX_EXP))
exp = exp * 10 + ((*p) - '0');
else
exp_overflow = true;
}
break;
case 10:
#line 1 "NONE"
{te = p+1;}
break;
case 9:
#line 61 "hb-number-parser.rl"
{te = p;p--;{
matched = true;
{p++; goto _out; }
}}
break;
case 3:
#line 61 "hb-number-parser.rl"
{{p = ((te))-1;}{
matched = true;
{p++; goto _out; }
}}
break;
case 1:
#line 1 "NONE"
{te = p+1;}
#line 40 "hb-number-parser.rl"
{
value = value * 10. + ((*p) - '0');
}
break;
case 3:
case 2:
#line 1 "NONE"
{te = p+1;}
#line 43 "hb-number-parser.rl"
{
if (likely (frac <= MAX_FRACT / 10))
@@ -196,31 +261,39 @@ _resume:
else if (frac_drop < 0) frac_drop = (*p) - '0'; else if ((*p) != '0') frac_sticky = true;
}
break;
case 5:
#line 52 "hb-number-parser.rl"
{
if (likely (exp * 10 + ((*p) - '0') <= MAX_EXP))
exp = exp * 10 + ((*p) - '0');
else
exp_overflow = true;
}
break;
#line 209 "hb-number-parser.hh"
#line 265 "hb-number-parser.hh"
}
_again:
switch ( _double_parser_to_state_actions[cs] ) {
case 6:
#line 1 "NONE"
{ts = 0;}
break;
#line 274 "hb-number-parser.hh"
}
if ( cs == 0 )
goto _out;
if ( ++p != pe )
goto _resume;
_test_eof: {}
if ( p == eof )
{
if ( _double_parser_eof_trans[cs] > 0 ) {
_trans = _double_parser_eof_trans[cs] - 1;
goto _eof_trans;
}
}
_out: {}
}
#line 120 "hb-number-parser.rl"
#line 133 "hb-number-parser.rl"
*end_ptr = p;
// end_ptr = end of match on success, else start of subject sequence
*end_ptr = (matched && ts == p_start) ? p : p_start;
// Apply round-to-nearest-even using the dropped tail recorded above
if (frac_drop > 5 ||
+18 -4
View File
@@ -58,14 +58,20 @@ action add_exp {
num = [0-9]+;
main := (
action end_number {
matched = true;
fbreak;
}
main := |*
(
(('+'|'-'@see_neg)? num @add_int) ('.' num @add_frac)?
(('+'|'-'@see_neg)? num @add_int) ('.' (num @add_frac)?)?
|
(('+'|'-'@see_neg)? '.' num @add_frac)
)
(('e'|'E') (('+'|'-'@see_exp_neg)? num @add_exp))?
);
=> end_number;
*|;
}%%
@@ -112,14 +118,22 @@ strtod_rl (const char *p, const char **end_ptr /* IN/OUT */)
const char *pe = *end_ptr;
while (p < pe && ISSPACE (*p))
p++;
const char *p_start = p; /* subject sequence must start here */
int cs;
const char *ts = p;
const char *te = p;
int act = 0;
const char *eof = pe;
bool matched = false;
(void) act;
%%{
write init;
write exec;
}%%
*end_ptr = p;
// end_ptr = end of match on success, else start of subject sequence
*end_ptr = (matched && ts == p_start) ? p : p_start;
// Apply round-to-nearest-even using the dropped tail recorded above
if (frac_drop > 5 ||
+85
View File
@@ -270,5 +270,90 @@ main (int argc, char **argv)
hb_always_assert (pv == 0.5);
}
{
const char str[] = "1e";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv;
hb_always_assert (hb_parse_double (&pp, end, &pv));
hb_always_assert (pv == 1.0);
hb_always_assert (pp == str + 1);
}
{
const char str[] = "1e+";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv;
hb_always_assert (hb_parse_double (&pp, end, &pv));
hb_always_assert (pv == 1.0);
hb_always_assert (pp == str + 1);
}
{
const char str[] = "-";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv = 99.0;
hb_always_assert (!hb_parse_double (&pp, end, &pv));
}
{
const char str[] = ".";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv = 99.0;
hb_always_assert (!hb_parse_double (&pp, end, &pv));
}
{
const char str[] = "1e-";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv;
hb_always_assert (hb_parse_double (&pp, end, &pv));
hb_always_assert (pv == 1.0);
hb_always_assert (pp == str + 1);
}
{
const char str[] = "-1e";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv;
hb_always_assert (hb_parse_double (&pp, end, &pv));
hb_always_assert (pv == -1.0);
hb_always_assert (pp == str + 2);
}
{
const char str[] = "+";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv = 99.0;
hb_always_assert (!hb_parse_double (&pp, end, &pv));
}
{
const char str[] = "-.";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv = 99.0;
hb_always_assert (!hb_parse_double (&pp, end, &pv));
}
// But be careful with fractional parts
{
const char str[] = "1.";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv;
hb_always_assert (hb_parse_double (&pp, end, &pv, true));
hb_always_assert (pv == 1.0);
hb_always_assert (pp == end);
}
{
const char str[] = "1.abc";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv;
hb_always_assert (hb_parse_double (&pp, end, &pv));
hb_always_assert (pv == 1.0);
hb_always_assert (pp == str + 2); // at least "1." was consumed
}
return 0;
}