From c0143611b8f5325c0370830688efb80543394b04 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 24 Oct 2012 19:15:03 +0100 Subject: [PATCH] Fixed a SQL typo and a race condition. The typo caused the select statement to be under-selective; the race condition could have occurred if a contact transport was removed while an outgoing connection was being created. --- components/net/sf/briar/db/JdbcDatabase.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/components/net/sf/briar/db/JdbcDatabase.java b/components/net/sf/briar/db/JdbcDatabase.java index c3afa3b95..288b97877 100644 --- a/components/net/sf/briar/db/JdbcDatabase.java +++ b/components/net/sf/briar/db/JdbcDatabase.java @@ -2036,13 +2036,17 @@ abstract class JdbcDatabase implements Database { try { // Get the current connection counter String sql = "SELECT outgoing FROM secrets" - + " WHERE contactId = ? AND transportId = ? AND period + ?"; + + " WHERE contactId = ? AND transportId = ? AND period = ?"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); ps.setBytes(2, t.getBytes()); ps.setLong(3, period); rs = ps.executeQuery(); - if(!rs.next()) throw new DbStateException(); + if(!rs.next()) { + rs.close(); + ps.close(); + return -1L; + } long connection = rs.getLong(1); if(rs.next()) throw new DbStateException(); rs.close(); @@ -2055,7 +2059,7 @@ abstract class JdbcDatabase implements Database { ps.setBytes(2, t.getBytes()); ps.setLong(3, period); int affected = ps.executeUpdate(); - if(affected > 1) throw new DbStateException(); + if(affected != 1) throw new DbStateException(); ps.close(); return connection; } catch(SQLException e) {