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

A binary string over 2 GB compares as smaller than its own prefix

    XMLWordPrintable

Details

    Description

      Likely cause: old code in my_strnncoll_binary() and my_strnncoll_8bit_bin(), 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 > '' AS r, r, 1)
      if ($r != 1)
      {
        --die A 2147483649 byte binary string did not compare as greater than the empty 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: A 2147483649 byte binary string did not compare as greater than the empty string

      The @b value is 2147483649 bytes, so it should compare as greater than the empty string. More forms, in the client:

      • SELECT @b > '': 0, and STRCMP(@b,''): -1
      • with a 3221225472 byte value, SELECT @b > 'a': 0, and STRCMP(@b,'a'): -1
      • the same bytes in a latin1 user variable: 1, the expected answer
        None of these give a warning.

      MySQL 5.5, 5.6, 8.0 and 9.1 give the same results, 0 and -1.

      From the code it seems that my_strnncoll_binary() and my_strnncoll_8bit_bin() return the length difference as an int when one string is a prefix of the other. Here the difference is 2147483649, so the int is negative and the longer string reads as the smaller one.

      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.