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

SIGSEGV in Item_func_like::turboBM_matches on LIKE over a 2 GB string, and 0 returned where the string matches

    XMLWordPrintable

Details

    Description

      Likely cause: old code in Item_func_like::turboBM_matches(), not a recent change. CS 10.11 and ES 10.6 have it too.

      # @b holds 2147483648 bytes 'a' and one byte 'b'
      --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
      --let $r= query_get_value(SELECT @b LIKE '%aaab%' AS r, r, 1)
      if ($r != 1)
      {
        --die LIKE did not find 'aaab' at the end of a 2147483649 byte string
      }
      DROP TABLE t1;

      Leads to:

      CS 13.1.2 87e62ca17270e163a6d41b3fc8d6f4d440f1bb88 (Optimized, Clang 22.1.8-20260622) Build 05/10/2026

      mysqltest: At line 16: LIKE did not find 'aaab' at the end of a 2147483649 byte string

      CS 13.1.0 da18481158c81ca94689702073c3e04aad85a6a3 (Debug, Clang 22.1.8-20260622) Build 18/08/2026

      mysqltest: At line 16: LIKE did not find 'aaab' at the end of a 2147483649 byte string

      In the client, with the same @b:

      CS 13.1.2 87e62ca17270e163a6d41b3fc8d6f4d440f1bb88 (Optimized, Clang 22.1.8-20260622) Build 05/10/2026

      SELECT @b LIKE '%aaab%' AS like_aaab, @b LIKE '%b' AS like_end_b, @b LIKE 'a%b' AS like_a_b, @b LIKE '%ab%' AS like_ab
      +-----------+------------+----------+---------+
      | like_aaab | like_end_b | like_a_b | like_ab |
      +-----------+------------+----------+---------+
      |         0 |          1 |        1 |       1 |
      +-----------+------------+----------+---------+
      1 row in set (52.550 sec)

      Only '%aaab%' gives the wrong result. A pattern of the form '%...%' with 4 or more characters between the % signs uses the Boyer-Moore search, the other patterns do not. On a short string, _binary'aaaaaaaaaab' LIKE '%aaab%' gives 1.

      In the client on a new server, the same SELECT @b LIKE '%aaab%' stopped the server with SIGSEGV in 1 of 9 runs on CS 13.1.2. The other 8 runs returned 0, and so did 10 runs on CS 13.3.0. The stack, from gdb attached to the server:

      CS 13.1.2 87e62ca17270e163a6d41b3fc8d6f4d440f1bb88 (Optimized, Clang 22.1.8-20260622) Build 05/10/2026

      Thread 2 "one_connection" received signal SIGSEGV, Segmentation fault.
      #0  0x00005a4018874039 in Item_func_like::turboBM_matches (this=this@entry=0x6e4c04018850, text=0x6e4a060e7030 'a' <repeats 200 times>..., text_len=<optimized out>) at sql/item_cmpfunc.cc:6704
      #1  0x00005a4018873e71 in Item_func_like::val_bool (this=0x6e4c04018850) at sql/item_cmpfunc.cc:5997
      #2  0x00005a40184cbb4d in Item_bool_func::val_int (this=0x6e4c0401a2a8) at sql/item_cmpfunc.h:245
      #3  0x00005a4018772f09 in Type_handler::Item_send_long (this=<optimized out>, item=0x6e4a060e7030, protocol=0x7ffffffd, buf=0x4) at sql/sql_type.cc:7712
      #4  Type_handler_long::Item_send (this=<optimized out>, item=0x6e4a060e7030, protocol=0x7ffffffd, buf=0x4) at sql/sql_type.h:6034
      #5  0x00005a40184d68a1 in Protocol::send_result_set_row (this=this@entry=0x6e4c040012b8, row_items=row_items@entry=0x6e4c04018048) at sql/protocol.cc:1358
      #6  0x00005a401854fa7f in select_send::send_data (this=0x6e4c040197e0, items=...) at sql/sql_class.cc:3454
      #7  0x00005a401854f82f in select_result_sink::send_data_with_check (this=0x6e4c0401a2a8, items=..., u=0x7ffffffd, sent=4, sent@entry=0) at sql/sql_class.cc:3352
      #8  0x00005a40185fd1c1 in JOIN::exec_inner (this=this@entry=0x6e4c04019810) at sql/sql_select.cc:4997
      #9  0x00005a40185e3997 in JOIN::exec (this=0x6e4c04019810) at sql/sql_select.cc:4914
      #10 mysql_select (thd=thd@entry=0x6e4c04000c70, tables=<optimized out>, fields=..., conds=<optimized out>, og_num=<optimized out>, order=<optimized out>, group=0x0, having=0x0, proc_param=0x0, select_options=<optimized out>, result=0x6e4c040197e0, unit=0x6e4c04005230, select_lex=0x6e4c04017d90) at sql/sql_select.cc:5440
      #11 0x00005a40185e35b0 in handle_select (thd=thd@entry=0x6e4c04000c70, lex=lex@entry=0x6e4c04005150, result=result@entry=0x6e4c040197e0, setup_tables_done_option=setup_tables_done_option@entry=0) at sql/sql_select.cc:637

      A later client run with the same bytes as a latin1 string (latin1_swedish_ci) also stopped the server with SIGSEGV, and gave a core. Frame #0 is the same loop, for a collation with a sort order. 3 more latin1 runs returned 0.

      MySQL

      +--------+------------------+
      | MySQL  | @b LIKE '%aaab%' |
      +--------+------------------+
      | 5.5.62 | 1                |
      | 5.6.51 | 0                |
      | 8.0.42 | 1                |
      | 9.1.0  | 1                |
      +--------+------------------+

      From the code it seems that Item_func_like::val_bool() passes res->length() to turboBM_matches(const char* text, int text_len). In the int, 2147483649 becomes -2147483647. Then tlmpl= text_len - pattern_len goes past INT_MIN. The index i + j in text[i + j] (frame #0) is an int as well, and turns negative at 2147483648.

      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.