Skip to content

Commit 615f4a8

Browse files
robinnewhousemariadb-RuchaDeodhar
authored andcommitted
MDEV-32587 Allow json exponential notation starting with zero
Modify the NS_ZERO state in the JSON number parser to allow exponential notation with a zero coefficient (e.g. 0E-4). The NS_ZERO state transition on 'E' was updated to move to the NS_EX state rather than returning a syntax error. Similar change was made for the NS_ZE1 (negative zero) starter state. This allows accepted number grammar to include cases like: - 0E4 - -0E-10 which were previously disallowed. Numeric parsing remains the same for all other states. Test cases are added to func_json.test to validate parsing for various exponential numbers starting with zero coefficients. All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services.
1 parent 8e337e0 commit 615f4a8

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

mysql-test/main/func_json.result

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,5 +1300,38 @@ f
13001300
foo
13011301
SET @@COLLATION_CONNECTION= @old_collation_connection;
13021302
#
1303+
# MDEV-32587 JSON_VALID fail to validate integer zero in scientific notation
1304+
#
1305+
select JSON_VALID(' {"number": 1E-4}');
1306+
JSON_VALID(' {"number": 1E-4}')
1307+
1
1308+
select JSON_VALID(' {"number": 0E-4}');
1309+
JSON_VALID(' {"number": 0E-4}')
1310+
1
1311+
select JSON_VALID(' {"number": 0.0}');
1312+
JSON_VALID(' {"number": 0.0}')
1313+
1
1314+
select JSON_VALID(' {"number": 0.1E-4}');
1315+
JSON_VALID(' {"number": 0.1E-4}')
1316+
1
1317+
select JSON_VALID(' {"number": 0e-4}');
1318+
JSON_VALID(' {"number": 0e-4}')
1319+
1
1320+
select JSON_VALID(' {"number": -0E-4}');
1321+
JSON_VALID(' {"number": -0E-4}')
1322+
1
1323+
select JSON_VALUE(' {"number": 0E-4}', '$.number');
1324+
JSON_VALUE(' {"number": 0E-4}', '$.number')
1325+
0E-4
1326+
select JSON_VALID(' {"number": 00E-4}');
1327+
JSON_VALID(' {"number": 00E-4}')
1328+
0
1329+
select JSON_VALID(' {"number": 01E-4}');
1330+
JSON_VALID(' {"number": 01E-4}')
1331+
0
1332+
select JSON_VALID(' {"number": 0E-4.0}');
1333+
JSON_VALID(' {"number": 0E-4.0}')
1334+
0
1335+
#
13031336
# End of 10.4 tests
13041337
#

mysql-test/main/func_json.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,22 @@ SELECT JSON_VALUE('["foo"]', '$**[0]') AS f;
840840

841841
SET @@COLLATION_CONNECTION= @old_collation_connection;
842842

843+
--echo #
844+
--echo # MDEV-32587 JSON_VALID fail to validate integer zero in scientific notation
845+
--echo #
846+
# Passing
847+
select JSON_VALID(' {"number": 1E-4}');
848+
select JSON_VALID(' {"number": 0E-4}');
849+
select JSON_VALID(' {"number": 0.0}');
850+
select JSON_VALID(' {"number": 0.1E-4}');
851+
select JSON_VALID(' {"number": 0e-4}');
852+
select JSON_VALID(' {"number": -0E-4}');
853+
select JSON_VALUE(' {"number": 0E-4}', '$.number');
854+
# Failing
855+
select JSON_VALID(' {"number": 00E-4}');
856+
select JSON_VALID(' {"number": 01E-4}');
857+
select JSON_VALID(' {"number": 0E-4.0}');
858+
843859
--echo #
844860
--echo # End of 10.4 tests
845861
--echo #

strings/json_lib.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,12 @@ enum json_num_states {
467467

468468
static int json_num_states[NS_NUM_STATES][N_NUM_CLASSES]=
469469
{
470-
/* - + 0 1..9 POINT E END_OK ERROR */
470+
/* - + 0 1..9 POINT E END_OK ERROR */
471471
/*OK*/ { JE_SYN, JE_SYN, JE_SYN, JE_SYN, JE_SYN, JE_SYN, JE_SYN, JE_BAD_CHR },
472472
/*GO*/ { NS_GO1, JE_SYN, NS_Z, NS_INT, JE_SYN, JE_SYN, JE_SYN, JE_BAD_CHR },
473473
/*GO1*/ { JE_SYN, JE_SYN, NS_Z1, NS_INT, JE_SYN, JE_SYN, JE_SYN, JE_BAD_CHR },
474-
/*ZERO*/ { JE_SYN, JE_SYN, JE_SYN, JE_SYN, NS_FRAC, JE_SYN, NS_OK, JE_BAD_CHR },
475-
/*ZE1*/ { JE_SYN, JE_SYN, JE_SYN, JE_SYN, NS_FRAC, JE_SYN, NS_OK, JE_BAD_CHR },
474+
/*ZERO*/ { JE_SYN, JE_SYN, JE_SYN, JE_SYN, NS_FRAC, NS_EX, NS_OK, JE_BAD_CHR },
475+
/*ZE1*/ { JE_SYN, JE_SYN, JE_SYN, JE_SYN, NS_FRAC, NS_EX, NS_OK, JE_BAD_CHR },
476476
/*INT*/ { JE_SYN, JE_SYN, NS_INT, NS_INT, NS_FRAC, NS_EX, NS_OK, JE_BAD_CHR },
477477
/*FRAC*/ { JE_SYN, JE_SYN, NS_FRAC, NS_FRAC,JE_SYN, NS_EX, NS_OK, JE_BAD_CHR },
478478
/*EX*/ { NS_EX, NS_EX, NS_EX1, NS_EX1, JE_SYN, JE_SYN, JE_SYN, JE_BAD_CHR },

0 commit comments

Comments
 (0)