Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-922] DECIMAL overflow for long/int/short not throwing exception
  • Loading branch information
diego Dupin committed Feb 10, 2022
1 parent dc36331 commit a180c21
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/mariadb/jdbc/plugin/codec/IntCodec.java
Expand Up @@ -127,9 +127,9 @@ public int decodeTextInt(final ReadableByteBuf buf, final int length, final Colu
case STRING:
String str = buf.readString(length);
try {
result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValue();
result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact();
break;
} catch (NumberFormatException nfe) {
} catch (NumberFormatException | ArithmeticException nfe) {
throw new SQLDataException(String.format("value '%s' cannot be decoded as Integer", str));
}

Expand Down Expand Up @@ -241,9 +241,9 @@ public int decodeBinaryInt(ReadableByteBuf buf, int length, Column column)
case STRING:
String str = buf.readString(length);
try {
result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValue();
result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact();
break;
} catch (NumberFormatException nfe) {
} catch (NumberFormatException | ArithmeticException nfe) {
throw new SQLDataException(String.format("value '%s' cannot be decoded as Integer", str));
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/mariadb/jdbc/plugin/codec/LongCodec.java
Expand Up @@ -139,8 +139,8 @@ public long decodeTextLong(ReadableByteBuf buf, int length, Column column)
case DECIMAL:
String str2 = buf.readAscii(length);
try {
return new BigDecimal(str2).setScale(0, RoundingMode.DOWN).longValue();
} catch (NumberFormatException nfe) {
return new BigDecimal(str2).setScale(0, RoundingMode.DOWN).longValueExact();
} catch (NumberFormatException | ArithmeticException nfe) {
throw new SQLDataException(String.format("value '%s' cannot be decoded as Long", str2));
}

Expand Down Expand Up @@ -269,7 +269,7 @@ public long decodeBinaryLong(ReadableByteBuf buf, int length, Column column)
String str = buf.readString(length);
try {
return new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact();
} catch (NumberFormatException nfe) {
} catch (NumberFormatException | ArithmeticException nfe) {
throw new SQLDataException(String.format("value '%s' cannot be decoded as Long", str));
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/mariadb/jdbc/plugin/codec/ShortCodec.java
Expand Up @@ -117,9 +117,9 @@ public short decodeTextShort(ReadableByteBuf buf, int length, Column column)
case STRING:
String str = buf.readString(length);
try {
result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValue();
result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact();
break;
} catch (NumberFormatException nfe) {
} catch (NumberFormatException | ArithmeticException nfe) {
throw new SQLDataException(String.format("value '%s' cannot be decoded as Short", str));
}

Expand Down Expand Up @@ -218,9 +218,9 @@ public short decodeBinaryShort(ReadableByteBuf buf, int length, Column column)
case STRING:
String str = buf.readString(length);
try {
result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValue();
result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact();
break;
} catch (NumberFormatException nfe) {
} catch (NumberFormatException | ArithmeticException nfe) {
throw new SQLDataException(String.format("value '%s' cannot be decoded as Short", str));
}

Expand Down
Expand Up @@ -32,10 +32,11 @@ public static void beforeAll2() throws SQLException {
drop();
Statement stmt = sharedConn.createStatement();
stmt.execute(
"CREATE TABLE DecimalCodec (t1 DECIMAL(10,0), t2 DECIMAL(10,6), t3 DECIMAL(10,3), t4 DECIMAL(10,0))");
"CREATE TABLE DecimalCodec (t1 DECIMAL(10,0), t2 DECIMAL(30,6), t3 DECIMAL(10,3), t4 DECIMAL(10,0))");
stmt.execute(
"CREATE TABLE DecimalCodec2 (t1 DECIMAL(10,0), t2 DECIMAL(10,6), t3 DECIMAL(10,3), t4 DECIMAL(10,0))");
stmt.execute("INSERT INTO DecimalCodec VALUES (0, 105.21, -1.6, null)");
stmt.execute(
"INSERT INTO DecimalCodec VALUES (0, 105.21, -1.6, null), (0, 9223372036854775808, 0, null)");
stmt.execute(
"CREATE TABLE DecimalCodec3 (id int not null primary key auto_increment, t1 DECIMAL(10,0))");
stmt.execute("FLUSH TABLES");
Expand Down Expand Up @@ -242,6 +243,11 @@ public void getShort(ResultSet rs) throws SQLException {
assertFalse(rs.wasNull());
assertEquals(0, rs.getShort(4));
assertTrue(rs.wasNull());
assertTrue(rs.next());
assertThrowsContains(
SQLDataException.class,
() -> rs.getShort(2),
"value '9223372036854775808.000000' cannot be decoded as Short");
}

@Test
Expand All @@ -265,6 +271,11 @@ public void getInt(ResultSet rs) throws SQLException {
assertFalse(rs.wasNull());
assertEquals(0, rs.getInt(4));
assertTrue(rs.wasNull());
assertTrue(rs.next());
assertThrowsContains(
SQLDataException.class,
() -> rs.getInt(2),
"value '9223372036854775808.000000' cannot be decoded as Int");
}

@Test
Expand All @@ -288,6 +299,11 @@ public void getLong(ResultSet rs) throws SQLException {
assertFalse(rs.wasNull());
assertEquals(0L, rs.getLong(4));
assertTrue(rs.wasNull());
assertTrue(rs.next());
assertThrowsContains(
SQLDataException.class,
() -> rs.getLong(2),
"value '9223372036854775808.000000' cannot be decoded as Long");
}

@Test
Expand Down Expand Up @@ -360,6 +376,8 @@ public void getBigDecimal(ResultSet rs) throws SQLException {
assertNull(rs.getBigDecimal(4));
assertNull(rs.getBigDecimal(4, BigDecimal.ROUND_CEILING));
assertTrue(rs.wasNull());
assertTrue(rs.next());
assertEquals(new BigDecimal("9223372036854775808.000000"), rs.getBigDecimal(2));
}

@Test
Expand Down Expand Up @@ -699,10 +717,10 @@ public void getMetaData() throws SQLException {
assertEquals("", meta.getSchemaName(1));
assertEquals(11, meta.getColumnDisplaySize(1));

assertEquals(10, meta.getPrecision(2));
assertEquals(30, meta.getPrecision(2));
assertEquals(6, meta.getScale(2));
assertEquals("", meta.getSchemaName(2));
assertEquals(12, meta.getColumnDisplaySize(2));
assertEquals(32, meta.getColumnDisplaySize(2));
}

@Test
Expand Down

0 comments on commit a180c21

Please sign in to comment.