Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-42546

Assertion `len > alloc_length' failed in Binary_string::realloc_raw on JSON_QUOTE of a 44739243 byte string, and NULL on optimized builds

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.11, 11.4, 11.8, 12.3, 13.1, 13.2, 13.3
    • 10.11, 11.4, 11.8, 12.3, 13.1, 13.2
    • JSON
    • None

    Description

      Likely cause: old code, not a recent change. st_append_escaped() has kept the size it reserves for the result in an int since the JSON functions were added in CS 10.2.3 (MDEV-9143, commit 27025221fe2ea17aa737ad2ad31011407c00dcc9). CS 10.11.12 and ES 10.6 fail the same way.

      SET @save_max_allowed_packet= @@global.max_allowed_packet;
      SET GLOBAL max_allowed_packet= 67108864;
      --connect (con1,localhost,root)
      SET @l= LENGTH(JSON_QUOTE(REPEAT('a',44739243)));
      --let $l= query_get_value(SELECT @l AS l, l, 1)
      if ($l != 44739245)
      {
        --die JSON_QUOTE of a 44739243 byte string returned a result of length $l, not 44739245
      }
      --disconnect con1
      --connection default
      SET GLOBAL max_allowed_packet= @save_max_allowed_packet;

      Leads to:

      CS 13.3.0 392818112aa1738005b977c21387d272b4287842 (Debug, Clang 22.1.8-20260622) Build 19/11/2026

      mariadbd: sql/sql_string.cc:89: bool Binary_string::realloc_raw(size_t): Assertion `len > alloc_length' failed.

      CS 13.3.0 392818112aa1738005b977c21387d272b4287842 (Debug, Clang 22.1.8-20260622) Build 19/11/2026

      #0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:44
      #1  __pthread_kill_internal (signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:78
      #2  __GI___pthread_kill (threadid=<optimized out>, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
      #3  0x0000769909c4527e in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
      #4  0x0000769909c288ff in __GI_abort () at ./stdlib/abort.c:79
      #5  0x0000769909c2881b in __assert_fail_base (fmt=0x769909dd1208 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=assertion@entry=0x594dad5a1557 "len > alloc_length", file=file@entry=0x594dad5a14fb "sql/sql_string.cc", line=line@entry=89, function=function@entry=0x594dad5a156a "bool Binary_string::realloc_raw(size_t)") at ./assert/assert.c:96
      #6  0x0000769909c3b517 in __assert_fail (assertion=0x594dad5a1557 "len > alloc_length", file=0x594dad5a14fb "sql/sql_string.cc", line=89, function=0x594dad5a156a "bool Binary_string::realloc_raw(size_t)") at ./assert/assert.c:105
      #7  0x0000594dac62c588 in Binary_string::realloc_raw (this=0x6e97d001af98, alloc_length=18446744071562068120) at sql/sql_string.cc:89
      #8  0x0000594dac370345 in Binary_string::realloc (this=0x6e97d001af98, arg_length=18446744071562068119) at sql/sql_string.h:765
      #9  0x0000594dac62e60a in Binary_string::reserve (this=0x6e97d001af98, space_needed=18446744071562067984, grow_by=1024) at sql/sql_string.cc:818
      #10 0x0000594dac777d0c in st_append_escaped (s=0x6e97d001af90, a=0x6e97d001abc0) at sql/item_jsonfunc.cc:156
      #11 0x0000594dac779f6d in Item_func_json_quote::val_str (this=0x6e97d001ac70, str=0x6e97d001af90) at sql/item_jsonfunc.cc:1015
      #12 0x0000594dac9e6dd7 in Item_func_octet_length::val_int (this=0x6e97d001aed0) at sql/item_func.cc:3208
      #13 0x0000594dac9ed4ca in Item_func_set_user_var::check (this=0x6e97d001afb0, use_result_field=false) at sql/item_func.cc:5241
      #14 0x0000594dac3d1a23 in set_var_user::check (this=0x6e97d001b120, thd=0x6e97d0000d60) at sql/set_var.cc:952
      #15 0x0000594dac3d133c in sql_set_variables (thd=0x6e97d0000d60, var_list=0x6e97d00065a0, free=true) at sql/set_var.cc:784
      #16 0x0000594dac527f29 in mysql_execute_command (thd=0x6e97d0000d60, is_called_from_prepared_stmt=false) at sql/sql_parse.cc:4971
      #17 0x0000594dac51ce68 in mysql_parse (thd=0x6e97d0000d60, rawbuf=0x6e97d001a880 "SET @l= LENGTH(JSON_QUOTE(REPEAT('a',44739243)))", length=48, parser_state=0x7699080fa9c0) at sql/sql_parse.cc:8020
      #18 0x0000594dac51a61e in dispatch_command (command=COM_QUERY, thd=0x6e97d0000d60, packet=0x6e97d000b6a1 "SET @l= LENGTH(JSON_QUOTE(REPEAT('a',44739243)))", packet_length=48, blocking=true) at sql/sql_parse.cc:1903
      #19 0x0000594dac51d8ea in do_command (thd=0x6e97d0000d60, blocking=true) at sql/sql_parse.cc:1437
      #20 0x0000594dac6fab6e in do_handle_one_connection (connect=0x594dcfc53990, put_in_cache=true) at sql/sql_connect.cc:1529
      #21 0x0000594dac6fa951 in handle_one_connection (arg=0x594dcfd3ece0) at sql/sql_connect.cc:1441

      CS 13.3.0 392818112aa1738005b977c21387d272b4287842 (Optimized, Clang 22.1.8-20260622) Build 19/11/2026

      mysqltest: At line 8: JSON_QUOTE of a 44739243 byte string returned a result of length NULL, not 44739245

      On optimized builds, JSON_QUOTE() returns NULL, with no warning and no error. A string of 44739242 bytes works. With a utf8mb4 connection, JSON_ARRAY() and JSON_OBJECT() also return NULL for a value or key of 44739243 bytes, and JSON_OBJECTAGG() leaves the key out:

      CS 13.3.0 392818112aa1738005b977c21387d272b4287842 (Optimized, Clang 22.1.8-20260622) Build 19/11/2026

      SET NAMES utf8mb4;
      SELECT LENGTH(JSON_ARRAY(REPEAT('a', 44739243))) AS arr, LENGTH(JSON_OBJECT(REPEAT('k', 44739243), 1)) AS obj_key, LENGTH(JSON_ARRAY(REPEAT('a', 44739242))) AS arr_under;
      arr	obj_key	arr_under
      NULL	NULL	44739246
      SELECT LENGTH(JSON_OBJECTAGG(REPEAT('k', 44739243), 1)) AS objagg_key;
      objagg_key
      6

      With a latin1 connection, JSON_ARRAY() and JSON_OBJECT() return the full result. MySQL 5.7, 8.0 and 9.1 return all 44739245 bytes of the JSON_QUOTE() result.

      From the code it seems that st_append_escaped() computes the size to reserve, a->length() * 12 * s->charset()>mbmaxlen / a>charset()->mbminlen, into an int. For 44739243 bytes and a utf8mb4 result that is 2147483664, which does not fit, so Binary_string::reserve() is called with a negative size. On optimized builds the reserve fails, and Item_func_json_quote::val_str() returns NULL without an error.

      Attachments

        Issue Links

          Activity

            People

              bar Alexander Barkov
              Roel Roel Van de Paar
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - Not Specified
                  Not Specified
                  Logged:
                  Time Spent - 20m
                  20m

                  Git Integration

                    Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.