diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBResultMetadata.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBResultMetadata.java index 59279d88465c..2c0658a34dd7 100644 --- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBResultMetadata.java +++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBResultMetadata.java @@ -83,6 +83,7 @@ public T unwrap(Class arg0) throws SQLException { }) // ignore Cognitive Complexity of methods should not be too high @Override public String getCatalogName(int column) throws SQLException { + checkColumnIndex(column); String systemSchmea = "_system_schmea"; String system = "_system"; String systemUser = "_system_user"; @@ -92,9 +93,6 @@ public String getCatalogName(int column) throws SQLException { String systemNull = ""; String columnName = columnInfoList.get(column - 1); List listColumns = columnInfoList; - if (column < 1 || column > columnInfoList.size()) { - throw new SQLException(Constant.METHOD_NOT_SUPPORTED); - } if ("SHOW".equals(operationType)) { if ("count".equals(listColumns.get(0))) { return systemDatabase; diff --git a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBResultMetadataTest.java b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBResultMetadataTest.java index dc9869bddda8..5e495b8e36e0 100644 --- a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBResultMetadataTest.java +++ b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBResultMetadataTest.java @@ -33,6 +33,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class IoTDBResultMetadataTest { @@ -173,4 +174,26 @@ public void testGetColumnType() throws SQLException { assertEquals(metadata.getColumnType(i + 1), types[i - 1]); } } + + @Test + public void getCatalogNameOutOfRangeThrowsSQLExceptionNotIndexOutOfBounds() throws SQLException { + String[] columns = {"root.a.b.c1", "root.a.b.c2", "root.a.b.c3"}; + metadata = new IoTDBResultMetadata(false, null, "LIST_USER", Arrays.asList(columns), null, false); + + // out-of-range columns must throw SQLException, not a raw IndexOutOfBoundsException from + // columnInfoList.get(column - 1) running before the range check. + for (int badColumn : new int[] {0, columns.length + 1}) { + try { + metadata.getCatalogName(badColumn); + fail("getCatalogName(" + badColumn + ") should throw SQLException for an out-of-range column"); + } catch (SQLException expected) { + // expected + } catch (IndexOutOfBoundsException e) { + fail("getCatalogName(" + badColumn + ") threw IndexOutOfBoundsException instead of SQLException"); + } + } + + // a valid column is unaffected by the added guard + assertEquals("_system_user", metadata.getCatalogName(1)); + } }