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

CONCAT, CONCAT_WS, EXPORT_SET, HEX, LOWER, UPPER, SPACE and CAST AS CHAR compute the result length in 32 bits, and give a cut or wrong result past 4 GB, or assert on debug builds

    XMLWordPrintable

Details

    Description

      Likely cause: old code, not a recent change. The max_allowed_packet check in Item_func_concat_ws::val_str() has added the lengths in 32 bits since 2001 (commit 6257ee7613836c397bda6c3533ca4a114d60e045). MySQL 5.5 and 5.6 crash on this testcase, while MySQL 8.0 and 9.1 return NULL.

      --perl
      open(my $f, ">", "$ENV{MYSQLTEST_VARDIR}/tmp/t1.txt") or die;
      my $s= "a" x 1048576;
      print $f $s for 1..2048;
      print $f "b";
      close($f);
      EOF
      CREATE TABLE t1 (a INT);
      --replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
      --eval LOAD DATA INFILE '$MYSQLTEST_VARDIR/tmp/t1.txt' INTO TABLE t1 CHARACTER SET binary (@b) SET a=1
      --remove_file $MYSQLTEST_VARDIR/tmp/t1.txt
      SET @n= CONCAT_WS('',@b,@b) IS NULL;
      --let $n= query_get_value(SELECT @n AS n, n, 1)
      if ($n != 1)
      {
        --die CONCAT_WS of two 2147483649 byte strings did not return NULL, while its result is larger than max_allowed_packet
      }
      DROP TABLE t1;

      Leads to:

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

      mysqltest: At line 16: CONCAT_WS of two 2147483649 byte strings did not return NULL, while its result is larger than max_allowed_packet

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

      mysqltest: At line 12: query 'SET @n= CONCAT_WS('',@b,@b) IS NULL' failed: <Unknown> (2013): Lost connection to server during query
      mariadbd: sql/sql_string.cc:89: bool Binary_string::realloc_raw(size_t): Assertion `len > alloc_length' failed.

      The stack, for SELECT LENGTH(CONCAT_WS('', @b, @b)) in the client:

      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  0x00007d466184527e in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
      #4  0x00007d46618288ff in __GI_abort () at ./stdlib/abort.c:79
      #5  0x00007d466182881b in __assert_fail_base (fmt=0x7d46619d1208 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=assertion@entry=0x58e113227557 "len > alloc_length", file=file@entry=0x58e1132274fb "sql/sql_string.cc", line=line@entry=89, function=function@entry=0x58e11322756a "bool Binary_string::realloc_raw(size_t)") at ./assert/assert.c:96
      #6  0x00007d466183b517 in __assert_fail (assertion=0x58e113227557 "len > alloc_length", file=0x58e1132274fb "sql/sql_string.cc", line=89, function=0x58e11322756a "bool Binary_string::realloc_raw(size_t)") at ./assert/assert.c:105
      #7  0x000058e1122b2588 in Binary_string::realloc_raw (this=0x75450c01b278, alloc_length=4294967426) at sql/sql_string.cc:89
      #8  0x000058e112036676 in Binary_string::realloc_with_extra (this=0x75450c01b278, arg_length=4294967298) at sql/sql_string.h:774
      #9  0x000058e1120364d6 in Binary_string::realloc_with_extra_if_needed (this=0x75450c01b278, arg_length=4294967298) at sql/sql_string.h:786
      #10 0x000058e11203645b in Binary_string::append (this=0x75450c01b278, s=0x754322788030 'a' <repeats 200 times>..., size=2147483649) at sql/sql_string.h:624
      #11 0x000058e11203640f in Binary_string::append (this=0x75450c01b278, s=@0x75450c01b0c8: {<Sql_alloc> = {<No data fields>}, Ptr = 0x754322788030 'a' <repeats 200 times>..., str_length = 2147483649, Alloced_length = 2147483656, extra_alloc = 0, alloced = true, thread_specific = false}) at sql/sql_string.h:635
      #12 0x000058e112030625 in String::append (this=0x75450c01b270, s=@0x75450c01b0c0: {<Charset> = {m_charset = 0x58e113d227e0 <my_charset_bin>}, <Binary_string> = {<Sql_alloc> = {<No data fields>}, Ptr = 0x754322788030 'a' <repeats 200 times>..., str_length = 2147483649, Alloced_length = 2147483656, extra_alloc = 0, alloced = true, thread_specific = false}, <No data fields>}) at sql/sql_string.h:1017
      #13 0x000058e1126ad3dc in Item_func_concat_ws::val_str (this=0x75450c01b000, str=0x75450c01b270) at sql/item_strfunc.cc:1004
      #14 0x000058e11266cdd7 in Item_func_octet_length::val_int (this=0x75450c01b1b0) at sql/item_func.cc:3208
      #15 0x000058e1124725f5 in Type_handler::Item_send_long (this=0x58e113f50980 <type_handler_slong>, item=0x75450c01b1b0, protocol=0x75450c001440, buf=0x7d463b88d180) at sql/sql_type.cc:7792
      #16 0x000058e11248511d in Type_handler_long::Item_send (this=0x58e113f50980 <type_handler_slong>, item=0x75450c01b1b0, protocol=0x75450c001440, buf=0x7d463b88d180) at sql/sql_type.h:6101
      #17 0x000058e111ff84fd in Item::send (this=0x75450c01b1b0, protocol=0x75450c001440, buffer=0x7d463b88d180) at sql/item.h:1250
      #18 0x000058e112042fdc in Protocol::send_result_set_row (this=0x75450c001440, row_items=0x75450c01aa18) at sql/protocol.cc:1358
      #19 0x000058e11211117e in select_send::send_data (this=0x75450c01bc40, items=@0x75450c01aa18: {<base_list> = {<Sql_alloc> = {<No data fields>}, first = 0x75450c01b290, last = 0x75450c01b290, elements = 1}, <No data fields>}) at sql/sql_class.cc:3490
      #20 0x000058e112110e6f in select_result_sink::send_data_with_check (this=0x75450c01bc40, items=@0x75450c01aa18: {<base_list> = {<Sql_alloc> = {<No data fields>}, first = 0x75450c01b290, last = 0x75450c01b290, elements = 1}, <No data fields>}, u=0x75450c0055a0, sent=0) at sql/sql_class.cc:3388
      #21 0x000058e112232812 in JOIN::exec_inner (this=0x75450c01bc70) at sql/sql_select.cc:5043
      #22 0x000058e1122322b3 in JOIN::exec (this=0x75450c01bc70) at sql/sql_select.cc:4960
      #23 0x000058e11221035b in mysql_select (thd=0x75450c000d60, tables=0x0, fields=@0x75450c01aa18: {<base_list> = {<Sql_alloc> = {<No data fields>}, first = 0x75450c01b290, last = 0x75450c01b290, elements = 1}, <No data fields>}, conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2164525824, result=0x75450c01bc40, unit=0x75450c0055a0, select_lex=0x75450c01a760) at sql/sql_select.cc:5380
      #24 0x000058e11220febd in handle_select (thd=0x75450c000d60, lex=0x75450c005210, result=0x75450c01bc40, setup_tables_done_option=0) at sql/sql_select.cc:652
      #25 0x000058e1121b3f8c in execute_sqlcom_select (thd=0x75450c000d60, all_tables=0x0) at sql/sql_parse.cc:6273
      #26 0x000058e1121aa1fb in mysql_execute_command (thd=0x75450c000d60, is_called_from_prepared_stmt=false) at sql/sql_parse.cc:4044
      #27 0x000058e1121a2e68 in mysql_parse (thd=0x75450c000d60, rawbuf=0x75450c01a6b0 "SELECT LENGTH(CONCAT_WS('', @b, @b))", length=36, parser_state=0x7d463b88f9c0) at sql/sql_parse.cc:8020
      #28 0x000058e1121a061e in dispatch_command (command=COM_QUERY, thd=0x75450c000d60, packet=0x75450c00b6a1 "", packet_length=36, blocking=true) at sql/sql_parse.cc:1903
      #29 0x000058e1121a38ea in do_command (thd=0x75450c000d60, blocking=true) at sql/sql_parse.cc:1437
      #30 0x000058e112380b6e in do_handle_one_connection (connect=0x58e11709c990, put_in_cache=true) at sql/sql_connect.cc:1529
      #31 0x000058e112380951 in handle_one_connection (arg=0x58e117187ce0) at sql/sql_connect.cc:1441

      In the client, with the same @b:

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

      --------------
      SELECT LENGTH(CONCAT_WS('', @b, @b)) AS cws, HEX(RIGHT(CONCAT_WS('', @b, @b), 1)) AS cws_last
      --------------
       
      +------------+----------+
      | cws        | cws_last |
      +------------+----------+
      | 2147483649 | 62       |
      +------------+----------+
      1 row in set (4.476 sec)
       
      --------------
      SHOW WARNINGS
      --------------
       
      Empty set (0.526 sec)
       
      --------------
      SELECT LENGTH(CONCAT(@b, @b)) AS c2
      --------------
       
      +------+
      | c2   |
      +------+
      | NULL |
      +------+
      1 row in set (2.287 sec)
       
      --------------
      SHOW WARNINGS
      --------------
       
      Empty set (0.265 sec)
       
      --------------
      SET sql_mode=ORACLE
      --------------
       
      Query OK, 0 rows affected (0.000 sec)
       
      --------------
      SELECT LENGTH(@b || @b) AS c_oracle
      --------------
       
      +----------+
      | c_oracle |
      +----------+
      |     NULL |
      +----------+
      1 row in set (2.240 sec)
       
      --------------
      SHOW WARNINGS
      --------------
       
      Empty set (0.266 sec)

      CONCAT_WS() returns only its first string, with no warning. CONCAT() and || in Oracle mode return NULL, also with no warning. The result would be 4294967298 bytes, larger than max_allowed_packet, so NULL with warning 1301 is expected for all three. On the debug build, CONCAT(@b, @b) gives the same assertion. The comments below show the same 32 bit length computation in EXPORT_SET(), HEX(), LOWER() and UPPER(), SPACE(), and CAST(... AS CHAR(N)), each with its own testcase.

      From the code it seems that both checks add the lengths in 32 bits. Item_func_concat::append_value() keeps res->length() + app->length() in a uint, and Item_func_concat_ws::val_str() compares res->length() + sep_str->length() + res2->length() with max_allowed_packet. For 4294967298 bytes both sums wrap to 2, so the checks pass. The append() that follows cannot grow the String past 4 GB: debug builds assert in Binary_string::realloc_raw(), and optimized builds return an error. CONCAT() then returns NULL, and Item_func_concat_ws::val_str() does not check the return value of str->append(*res2), so it returns the first string.

      Attachments

        Issue Links

          Activity

            People

              bar Alexander Barkov
              Roel Roel Van de Paar
              Votes:
              0 Vote for this issue
              Watchers:
              2 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.