diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerParameterMetaData.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerParameterMetaData.java index 56fce55c0..aeb7a99d9 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerParameterMetaData.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerParameterMetaData.java @@ -397,7 +397,8 @@ private Map getParameterInfo(int param) { private boolean isValidParamProc(int n) { // Note row 1 is the 'return value' meta data - return ((stmtParent.bReturnValueSyntax && isTVP && procMetadata.size() >= n) || procMetadata.size() > n); + return (n >= 1 // JDBC index is 1 based + && ((stmtParent.bReturnValueSyntax && isTVP && procMetadata.size() >= n) || procMetadata.size() > n)); } private boolean isValidParamQuery(int n) { diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/parametermetadata/ParameterMetaDataTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/parametermetadata/ParameterMetaDataTest.java index 61b9936af..736a431c9 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/parametermetadata/ParameterMetaDataTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/parametermetadata/ParameterMetaDataTest.java @@ -4,6 +4,7 @@ */ package com.microsoft.sqlserver.jdbc.parametermetadata; +import static org.junit.Assert.assertThrows; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -127,6 +128,10 @@ public void testParameterMetaData() throws SQLException { assert (metadata.getPrecision(2) == 38); assert (metadata.getScale(2) == 5); assert (!metadata.isSigned(1)); + + // test invalid index + assertThrows(SQLException.class, () -> metadata.getParameterType(0)); + assertThrows(SQLException.class, () -> metadata.getParameterType(3)); } } finally { TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt); @@ -160,6 +165,10 @@ public void testParameterMetaDataProc() throws SQLException { assert (metadata.getScale(1) == 0); assert (metadata.isNullable(1) == ParameterMetaData.parameterNullable); assert (!metadata.isSigned(1)); + + // test invalid index + assertThrows(SQLException.class, () -> metadata.getParameterType(0)); + assertThrows(SQLException.class, () -> metadata.getParameterType(3)); } } }