Skip to content

MDEV-39720: Optimizer Trace doesn't quote string values, may have inv…#5133

Open
spetrunia wants to merge 1 commit into
bb-main-MDEV-39768from
bb-main-MDEV-39720
Open

MDEV-39720: Optimizer Trace doesn't quote string values, may have inv…#5133
spetrunia wants to merge 1 commit into
bb-main-MDEV-39768from
bb-main-MDEV-39720

Conversation

@spetrunia
Copy link
Copy Markdown
Member

…alid JSON

  • Make Json_writer::add_str() do the quoting with json_escape_to_string(). = All other string writing methods are handled as they call this one.
  • Move json_escape_to_string() from opt_histogram_json.cc to my_json_writer.cc. This is needed because JSON writer unit tests do not link with opt_histogram_json.
  • Histogram code does its own escaping (with handling for special cases). Add Json_writer::add_escaped_str() to accept escaped strings. This is not intended to be commonly used.
  • opt_trace_print_expanded_query() did its own escaping for "expanded_query" string. It was done in MDEV-30354. Remove quoting there as otherwise it will be double-quoted.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses MDEV-39720 by ensuring that strings in the Optimizer Trace are properly escaped to prevent invalid JSON output. It refactors string escaping by moving json_escape_to_string to my_json_writer.cc and integrating it directly into Json_writer::add_str. The review feedback highlights two important issues: a potential use-after-free/dangling pointer vulnerability in json_escape_to_string if the input string points to the output buffer during reallocation, and a potential encoding issue caused by hardcoding &my_charset_utf8mb4_bin which may fail for non-UTF-8 input strings.

Comment thread sql/my_json_writer.cc
Comment thread sql/my_json_writer.cc
Comment thread sql/my_json_writer.cc
Comment thread sql/my_json_writer.cc
Comment thread sql/my_json_writer.cc
@spetrunia spetrunia force-pushed the bb-main-MDEV-39720 branch from 96232b3 to 2148b23 Compare May 28, 2026 09:40
@spetrunia spetrunia force-pushed the bb-main-MDEV-39768 branch 2 times, most recently from 12c98be to a06be02 Compare May 28, 2026 14:28
@spetrunia spetrunia force-pushed the bb-main-MDEV-39720 branch 5 times, most recently from 19d9377 to 1d75fff Compare May 29, 2026 07:43
…alid JSON

- Make Json_writer::add_str() do the quoting with json_escape_to_string().
  = All other string writing methods are handled as they call this one.
- Move json_escape_to_string() from opt_histogram_json.cc to
  my_json_writer.cc. This is needed because JSON writer unit tests do not
  link with opt_histogram_json.
- Histogram code does its own escaping (with handling for special cases).
  Add Json_writer::add_escaped_str() to accept escaped strings.
  This is not intended to be commonly used.
- opt_trace_print_expanded_query() did its own escaping for
  "expanded_query" string. It was done in MDEV-30354. Remove escaping
  there to avoid double escaping.
- dump_record_to_trace() also did own escaping when writing table DDLs
  into the optimizer trace. Remove double escaping.
- Adjust my_json_writer unit test.
@spetrunia spetrunia force-pushed the bb-main-MDEV-39720 branch from 1d75fff to 243e7cd Compare May 29, 2026 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants