Skip to content

Commit bd87966

Browse files
committed
Fix for Bug#98237 (30911870), PREPAREDSTATEMENT.SETOBJECT(I, "FALSE",
TYPES.BOOLEAN) ALWAYS SETS TRUE OR 1.
1 parent 884393d commit bd87966

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
Version 5.1.49
55

6+
- Fix for Bug#98237 (30911870), PREPAREDSTATEMENT.SETOBJECT(I, "FALSE", TYPES.BOOLEAN) ALWAYS SETS TRUE OR 1.
7+
68
- Fix for Bug#30866178, Not recommended default for 'allowLoadLocalInfile'. Back-port from Bug#94051 (29261254).
79

810
- Fix for Bug#30657312, Disable external entities in Fabric's XML parser.

src/com/mysql/jdbc/PreparedStatement.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
33
44
The MySQL Connector/J is licensed under the terms of the GPLv2
55
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
@@ -3682,7 +3682,16 @@ public void setObject(int parameterIndex, Object parameterObj, int targetSqlType
36823682

36833683
break;
36843684
} else if (parameterObj instanceof String) {
3685-
setBoolean(parameterIndex, "true".equalsIgnoreCase((String) parameterObj) || !"0".equalsIgnoreCase((String) parameterObj));
3685+
if ("true".equalsIgnoreCase((String) parameterObj) || "Y".equalsIgnoreCase((String) parameterObj)) {
3686+
setBoolean(parameterIndex, true);
3687+
} else if ("false".equalsIgnoreCase((String) parameterObj) || "N".equalsIgnoreCase((String) parameterObj)) {
3688+
setBoolean(parameterIndex, false);
3689+
} else if (((String) parameterObj).matches("-?\\d+\\.?\\d*")) {
3690+
setBoolean(parameterIndex, !((String) parameterObj).matches("-?[0]+[.]*[0]*"));
3691+
} else {
3692+
throw SQLError.createSQLException("No conversion from " + parameterObj + " to Types.BOOLEAN possible.",
3693+
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
3694+
}
36863695

36873696
break;
36883697
} else if (parameterObj instanceof Number) {

src/testsuite/regression/StatementRegressionTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,7 @@ public void testLoadData() throws Exception {
22602260
assertThrows(SQLException.class,
22612261
versionMeetsMinimum(8, 0, 19) ? "Loading local data is disabled;.*" : "The used command is not allowed with this MySQL version",
22622262
new Callable<Void>() {
2263+
@SuppressWarnings("synthetic-access")
22632264
public Void call() throws Exception {
22642265
StatementRegressionTest.this.stmt
22652266
.executeUpdate("LOAD DATA LOCAL INFILE '" + fileName + "' INTO TABLE loadDataRegress CHARACTER SET "
@@ -8858,4 +8859,60 @@ public void testBug96442() throws Exception {
88588859

88598860
}
88608861

8862+
/**
8863+
* Tests fix for Bug#98237 (30911870), PREPAREDSTATEMENT.SETOBJECT(I, "FALSE", TYPES.BOOLEAN) ALWAYS SETS TRUE OR 1.
8864+
*/
8865+
public void testBug98237() throws Exception {
8866+
createTable("testBug98237", "(b tinyint)");
8867+
8868+
String[] falses = new String[] { "False", "n", "0", "-0", "0.00", "-0.0" };
8869+
String[] trues = new String[] { "true", "y", "1", "-1", "1.0", "-1.0", "0.01", "-0.01" };
8870+
8871+
Properties props = new Properties();
8872+
boolean useSPS = false;
8873+
do {
8874+
props.setProperty("useServerPrepStmts", Boolean.toString(useSPS));
8875+
Connection con = getConnectionWithProps(props);
8876+
try {
8877+
final PreparedStatement ps = con.prepareStatement("insert into testBug98237 values(?)");
8878+
Statement st = con.createStatement();
8879+
8880+
con.createStatement().execute("truncate table testBug98237");
8881+
for (String val : falses) {
8882+
ps.clearParameters();
8883+
ps.setObject(1, val, Types.BOOLEAN);
8884+
ps.execute();
8885+
}
8886+
this.rs = st.executeQuery("select * from testBug98237");
8887+
for (String val : falses) {
8888+
assertTrue(this.rs.next());
8889+
assertEquals("'false' was expected for " + val, 0, this.rs.getInt(1));
8890+
}
8891+
8892+
con.createStatement().execute("truncate table testBug98237");
8893+
for (String val : trues) {
8894+
ps.clearParameters();
8895+
ps.setObject(1, val, Types.BOOLEAN);
8896+
ps.execute();
8897+
}
8898+
this.rs = st.executeQuery("select * from testBug98237");
8899+
for (String val : trues) {
8900+
assertTrue(this.rs.next());
8901+
assertEquals("'true' was expected for " + val, 1, this.rs.getInt(1));
8902+
}
8903+
8904+
ps.clearParameters();
8905+
assertThrows(SQLException.class, "No conversion from abc to Types.BOOLEAN possible.", new Callable<Void>() {
8906+
public Void call() throws Exception {
8907+
ps.setObject(1, "abc", Types.BOOLEAN);
8908+
return null;
8909+
}
8910+
});
8911+
8912+
} finally {
8913+
con.close();
8914+
}
8915+
} while (useSPS = !useSPS);
8916+
8917+
}
88618918
}

0 commit comments

Comments
 (0)