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

Assertion `len > alloc_length' failed in Binary_string::realloc_raw on JSON_UNQUOTE of a 512 MB latin1 JSON string, and an out of memory error on optimized builds

    XMLWordPrintable

Details

    Description

      Likely cause: MDEV-36765, commit cce76e72252eccf77599284ee53689d45241cf93, a follow-up fix in CS 10.11.14. It made JSON_UNQUOTE() reserve 4 bytes for each byte of a JSON string that is not utf8mb4, and keep that size in an int. CS 10.11.12 returns all 536870912 characters.

      SET @save_max_allowed_packet= @@global.max_allowed_packet;
      SET GLOBAL max_allowed_packet= 1073741824;
      --connect (con1,localhost,root)
      SET @j= CONCAT(_latin1'"', REPEAT(_latin1'a',536870912), _latin1'"');
      --error 0,ER_OUTOFMEMORY
      SET @v= JSON_UNQUOTE(@j);
      if ($mysql_errno)
      {
        --die JSON_UNQUOTE of a latin1 JSON string of 536870912 characters failed with an out of memory error
      }
      --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  0x00007af8de44527e in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
      #4  0x00007af8de4288ff in __GI_abort () at ./stdlib/abort.c:79
      #5  0x00007af8de42881b in __assert_fail_base (fmt=0x7af8de5d1208 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=assertion@entry=0x63ee2cdf2557 "len > alloc_length", file=file@entry=0x63ee2cdf24fb "sql/sql_string.cc", line=line@entry=89, function=function@entry=0x63ee2cdf256a "bool Binary_string::realloc_raw(size_t)") at ./assert/assert.c:96
      #6  0x00007af8de43b517 in __assert_fail (assertion=0x63ee2cdf2557 "len > alloc_length", file=0x63ee2cdf24fb "sql/sql_string.cc", line=89, function=0x63ee2cdf256a "bool Binary_string::realloc_raw(size_t)") at ./assert/assert.c:105
      #7  0x000063ee2be7d588 in Binary_string::realloc_raw (this=0x72f79001abb0, alloc_length=18446744071562068096) at sql/sql_string.cc:89
      #8  0x000063ee2bc01676 in Binary_string::realloc_with_extra (this=0x72f79001abb0, arg_length=18446744071562067968) at sql/sql_string.h:774
      #9  0x000063ee2bc014d6 in Binary_string::realloc_with_extra_if_needed (this=0x72f79001abb0, arg_length=18446744071562067968) at sql/sql_string.h:786
      #10 0x000063ee2bfcb26f in Item_func_json_unquote::val_str (this=0x72f79001a910, str=0x72f79001aba8) at sql/item_jsonfunc.cc:1088
      #11 0x000063ee2c23e560 in Item_func_set_user_var::check (this=0x72f79001aab0, use_result_field=false) at sql/item_func.cc:5250
      #12 0x000063ee2bc22a23 in set_var_user::check (this=0x72f79001ac20, thd=0x72f790000d60) at sql/set_var.cc:952
      #13 0x000063ee2bc2233c in sql_set_variables (thd=0x72f790000d60, var_list=0x72f7900065a0, free=true) at sql/set_var.cc:784
      #14 0x000063ee2bd78f29 in mysql_execute_command (thd=0x72f790000d60, is_called_from_prepared_stmt=false) at sql/sql_parse.cc:4971
      #15 0x000063ee2bd6de68 in mysql_parse (thd=0x72f790000d60, rawbuf=0x72f79001a6b0 "SET @v= JSON_UNQUOTE(@j)", length=24, parser_state=0x72f87f87b9c0) at sql/sql_parse.cc:8020
      #16 0x000063ee2bd6b61e in dispatch_command (command=COM_QUERY, thd=0x72f790000d60, packet=0x72f79000b6a1 "SET @v= JSON_UNQUOTE(@j)", packet_length=24, blocking=true) at sql/sql_parse.cc:1903
      #17 0x000063ee2bd6e8ea in do_command (thd=0x72f790000d60, blocking=true) at sql/sql_parse.cc:1437
      #18 0x000063ee2bf4bb6e in do_handle_one_connection (connect=0x63ee48a72b20, put_in_cache=true) at sql/sql_connect.cc:1529
      #19 0x000063ee2bf4b951 in handle_one_connection (arg=0x63ee48b5de70) at sql/sql_connect.cc:1441

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

      mysqltest: At line 9: JSON_UNQUOTE of a latin1 JSON string of 536870912 characters failed with an out of memory error

      The same statements in the client:

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

      --------------
      SET @v= JSON_UNQUOTE(@j)
      --------------
       
      ERROR 1037 (HY001) at line 2: Out of memory; restart server and try again (needed -2147483648 bytes)

      A JSON string of 536870911 characters works. MySQL 5.7, 8.0 and 9.1 return all 536870912 characters.

      From the code it seems that Item_func_json_unquote::val_str() keeps the buffer size in int buf_len, and multiplies it by my_charset_utf8mb4_bin.mbmaxlen when the JSON string is not utf8mb4. For 536870912 bytes that is 2147483648, which does not fit, so realloc_with_extra_if_needed() is called with a negative size.

      Attachments

        Issue Links

          Activity

            People

              psergei Sergei Petrunia
              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.