Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-6211

SUBSTRING with Integer.MIN_VALUE as a second parameter raise unexpected exception

    XMLWordPrintableJSON

Details

    Description

      SUBSTRING function with minimal possible integer offset parameter raise exception.

      Simple way to reproduce :
      append into SqlOperatorTest smth like:

          f.checkScalar(
              String.format("{fn SUBSTRING('abcdef', %d)}", Integer.MIN_VALUE),
              "abcdef",
              "VARCHAR(6) NOT NULL");
      
      

      And exception will raised:

      String index out of range: -2147483641
      

      it`s all due to integer overflow, check implementation near:

        public static String substring(String c, int s) {
          final int s0 = s - 1;  // -2147483648 - 1 = 2147483647
          if (s0 <= 0) { // this check is broken
            return c;
          }
          if (s > c.length()) {
            return "";
          }
          return c.substring(s0);
        }
      

      Attachments

        Issue Links

          Activity

            People

              zstan Evgeny Stanilovsky
              zstan Evgeny Stanilovsky
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: