Evgeny Stanilovsky created CALCITE-6211:
-------------------------------------------

             Summary: SUBSTRING with Integer.MIN_VALUE as a second parameter 
raise unexpected exception
                 Key: CALCITE-6211
                 URL: https://issues.apache.org/jira/browse/CALCITE-6211
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.36.0
            Reporter: Evgeny Stanilovsky


SUBSTRING function with minimal possible integer offset parameter raise 
exception.

Simple way to reproduce :
append into *SqlOperatorTest* smth like:

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

{noformat}

And exception will raised:

{noformat}
String index out of range: -2147483641
{noformat}

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

{noformat}
  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);
  }
{noformat}






--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to