← Back to Pull requests

MDEV-41898 JSON_VALID accepts a number with an empty exponent ('1e') inside arrays and objects #6248

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 26, 2026 22:42 UTC
Author

Problem

A number whose exponent has no digits (1e, 1e+, 1E-), or a lone -, is
not valid JSON. It is rejected at the top level, but accepted as soon as it sits
inside an array or object:

SELECT JSON_VALID('1e'), JSON_VALID('[1e]'), JSON_VALID('{"a":1e}'),
       JSON_VALID('[1e+]'), JSON_VALID('[-]');
0  1  1  1  1

SELECT JSON_VALUE('[1e]', '$[0]');
1e

| Input | Before | After |
|---|---|---|
| 1e | 0 | 0 |
| [1e], {"a":1e}, [1e,2], [1e ] | 1 | 0 |
| [1e+], [1E-], [0e], [1.5e] | 1 | 0 |
| [-], {"a":-} | 1 | 0 |
| 1e+-5, [1e--+5], 1e++5, {"a":1E-+5} (repeated exponent sign) | 1 | 0 |
| JSON_VALUE('[1e]', '$[0]') | 1e | NULL |
| [1e5], {"a":1E+5}, [1e-5, 2], [-1 ] | 1 | 1 (unchanged) |

Root cause

skip_num_constant() (strings/json_lib.c) scans a number one character at a
time through the json_num_states table. There are two ways the number can stop:

if ((c_len= json_next_char(&j->s)) > 0 && j->s.c_next < 128)
{
  if ((state= json_num_states[state][json_num_chr_map[j->s.c_next]]) > 0)
  {
    j->s.c_str+= c_len;
    continue;
  }
  break;                                   /* (1) followed by a character */
}

if ((j->s.error=
      json_eos(&j->s) ? json_num_states[state][N_END] : JE_BAD_CHR) < 0)
  return 1;                                /* (2) end of the text */

  • (2) End of the text checks json_num_states[state][N_END], i.e. whether

  • the number can end in its current state, and fails if not. That is why '1e'
    is rejected.
  • (1) Followed by a character. ], ,, } and whitespace are class

  • N_END, so the lookup is the same "can the number end here?" check, and for
    an incomplete number it returns JE_SYN. But the loop breaks on any
    non-positive result without telling NS_OK from an error, so the error is
    lost and the scanner carries on as if a complete number had been read.

    Repeated exponent signs

    A second, independent defect sits in the same table. JSON allows at most one
    sign after the exponent marker, but the EX row sends - and + back to
    NS_EX itself, so any number of signs is consumed before the digits:

    /*         -        +       0         1..9    POINT    E       END_OK   ERROR */
    /*EX*/   { NS_EX,   NS_EX,  NS_EX1,   NS_EX1, JE_SYN,  JE_SYN, JE_SYN, JE_BAD_CHR },
    

    This is accepted everywhere, top level included: JSON_VALID('1e+-5') and
    JSON_VALID('[1e--+5]') both return 1.

    Fix

    When the number stops at a character that is not part of it, apply the same
    N_END check as at the end of the text and fail with its error:

    int next_state= json_num_states[state][json_num_chr_map[j->s.c_next]];
    if (next_state > 0)
    {
      state= next_state;
      j->s.c_str+= c_len;
      continue;
    }
    if ((j->s.error= json_num_states[state][N_END]) < 0)
      return 1;
    break;
    

    The check is on the state the number was in (its N_END transition), not on
    the failed transition. So:

  • a complete number followed by a delimiter behaves exactly as before;

  • a complete number followed by an invalid character (e.g. [1x]) is still

  • reported by the code that reads the next token, as before — its error code and
    position do not change;
  • only an incomplete number now fails, with the same JE_SYN the top-level case

  • already reported, at the position of the delimiter.

    Add a state for the exponent sign, NS_EXS. EX moves to it on - or +, and
    NS_EXS accepts only a digit, so a second sign is JE_SYN. It cannot end the
    number either, so 1e+ is still rejected via the N_END check above:

    /*EX*/   { NS_EXS,  NS_EXS, NS_EX1,   NS_EX1, JE_SYN,  JE_SYN, JE_SYN, JE_BAD_CHR },
    /*EXS*/  { JE_SYN,  JE_SYN, NS_EX1,   NS_EX1, JE_SYN,  JE_SYN, JE_SYN, JE_BAD_CHR },
    

    The new state carries no num_flags (JSON_NUM_EXP is already set by NS_EX),
    and the states are private to json_lib.c, so nothing else is affected.

     strings/json_lib.c               | 16 +++++++++--
     mysql-test/main/func_json.test   | 32 ++++++++++++++++++
     mysql-test/main/func_json.result | 60 ++++++++++++++++++++++++++++++
    

    Testing

    Added to mysql-test/main/func_json.test. Verified failing before the change
    (10 of the 11 incomplete numbers returned 1, JSON_VALUE('[1e]','$[0]')
    returned 1e, and the four repeated-sign cases returned 1) and passing after. It covers:

  • an empty exponent at the top level, in an array and in an object, followed by

  • ], ,, } and whitespace;
  • an exponent sign with no digits (1e+, 1E-), and after 0 and a fraction;

  • a lone - in an array and in an object;

  • repeated exponent signs (1e+-5, [1e--+5], 1e++5, {"a":1E-+5},

  • [1e+-]), at the top level and nested, with a single sign (1e+5, 1E-5)
    still valid;
  • JSON_VALUE no longer returning the incomplete number, and JSON_EXTRACT

  • reporting the error at the delimiter rather than inside its own result;
  • complete numbers next to the same delimiters still valid, so the test cannot

  • pass by over-rejecting.

    The parser is shared by every JSON function, by JSON histograms and by
    mysql.global_priv, so the suites covering those callers were run in full:

    | Suite | Result |
    |---|---|
    | all JSON tests (json suite, main.json) | 38/38 pass |
    | main, roles, json (full) | 1293/1293 pass |
    | main.func_json, embedded server | pass |

    No existing result file changed, so no existing error message or position moved.

    ---

    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, Inc.

    Assisted-by: Claude:claude-opus-5-5

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: gkodinov Review duration: 6214m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #6248"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%
    Repository