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

SIGSEGV in my_instr_bin on LOCATE with a start position over 2147483647, and 0 returned where the string matches

    XMLWordPrintable

Details

    Description

      Likely cause: old code in Item_func_locate::val_int(), 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
      SET @l= LOCATE('b',@b,2147483649);
      --let $l= query_get_value(SELECT @l AS l, l, 1)
      if ($l != 2147483649)
      {
        --die LOCATE from position 2147483649 did not find the 'b' at position 2147483649, it returned $l
      }
      DROP TABLE t1;

      Leads to:

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

      mysqltest: At line 17: LOCATE from position 2147483649 did not find the 'b' at position 2147483649, it returned 0

      The @b value is 2147483648 bytes 'a' followed by one byte 'b'. A start position above 2147483647 gives 0 whatever the string holds, and no warning.

      With a 3221225473 byte value in the client, the same start position reads outside the string and the server can crash:

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

      Thread 2 "one_connection" received signal SIGSEGV, Segmentation fault.
      0x0000606fad7be56a in my_instr_bin (cs=<optimized out>, b=0x72bada78702d <error: Cannot access memory at address 0x72bada78702d>, b_length=<optimized out>, s=0x72be98018960 "a", s_length=1, match=0x7abf90ae0204, nmatch=<optimized out>) at strings/ctype-bin.c:503
      #0  0x0000606fad7be56a in my_instr_bin (cs=<optimized out>, b=0x72bada78702d <error: Cannot access memory at address 0x72bada78702d>, b_length=<optimized out>, s=0x72be98018960 "a", s_length=1, match=0x7abf90ae0204, nmatch=<optimized out>) at strings/ctype-bin.c:503
      #1  0x0000606fad3259fd in charset_info_st::instr (this=0x72bada78702e, b_length=126146854809649, s=0x72be98018960 "a", s_length=1, match=0x7abf90ae0204, nmatch=1, b=<optimized out>) at include/m_ctype.h:1240
      #2  Item_func_locate::val_int (this=0x72be98018c70) at sql/item_func.cc:3294
      #3  0x0000606fad1f6f09 in Type_handler::Item_send_long (this=<optimized out>, item=0x72bada78702d, protocol=0x72bada787031, buf=0x72be98018960) at sql/sql_type.cc:7712
      #4  Type_handler_long::Item_send (this=<optimized out>, item=0x72bada78702d, protocol=0x72bada787031, buf=0x72be98018960) at sql/sql_type.h:6034
      #5  0x0000606facf5a8a1 in Protocol::send_result_set_row (this=this@entry=0x72be980012b8, row_items=row_items@entry=0x72be98018008) at sql/protocol.cc:1358
      #6  0x0000606facfd3a7f in select_send::send_data (this=0x72be98019740, items=...) at sql/sql_class.cc:3454
      #7  0x0000606facfd382f in select_result_sink::send_data_with_check (this=0x72bada78702e, items=..., u=0x72bada787031, sent=126162919590240, sent@entry=0) at sql/sql_class.cc:3352
      #8  0x0000606fad0811c1 in JOIN::exec_inner (this=this@entry=0x72be98019770) at sql/sql_select.cc:4997
      #9  0x0000606fad067997 in JOIN::exec (this=0x72be98019770) at sql/sql_select.cc:4914
      #10 mysql_select (thd=thd@entry=0x72be98000c70, 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=0x72be98019740, unit=0x72be98005230, select_lex=0x72be98017d50) at sql/sql_select.cc:5440

      That crash came from this statement, run in the session that loaded the value:

      SELECT LOCATE('b',@b), INSTR(@b,'b'), LOCATE('a',@b,3221225470);

      The third expression on its own returns 3221225470 and does not crash, so whether the read lands on mapped memory decides the outcome.

      More forms, in the client, with the 3221225473 byte value:

      • LOCATE('b',@b) and INSTR(@b,'b'): 3221225473, correct
      • LOCATE('b',@b,2147483649) and LOCATE('b',@b,3221225473): 0
      • LOCATE('a',@b,2147483647): 2147483647, correct

      MySQL

      +--------+---------------------------+
      | MySQL  | LOCATE('b',@b,2147483649) |
      +--------+---------------------------+
      | 5.5.62 | 0                         |
      | 5.6.51 | 0                         |
      | 8.0.42 | 0                         |
      | 9.1.0  | 2147483649                |
      +--------+---------------------------+

      From the code it seems that Item_func_locate::val_int() casts the start position to int before charpos(). Above 2147483647 that int is negative, and String::charpos() returns a negative input unchanged. The check that follows, start + b->length() > a->length(), stays false for a negative start, so a->ptr() + start goes to instr() and the read is outside the string.

      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 - 0.5h
                  0.5h

                  Git Integration

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