8000 Prevent NullPointerException when calling delete with null parameter … · sqlcipher/sqlcipher-android@ce1d19f · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit ce1d19f

Browse files
Prevent NullPointerException when calling delete with null parameter for where args
1 parent 6e87aec commit ce1d19f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/SQLCipherDatabaseTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,21 @@ public void shouldAllowCursorWindowToResize(){
504504
SQLiteCursor.resetCursorWindowSize();
505505
}
506506
}
507+
508+
@Test
509+
public void shouldSupportDeleteWithNullWhereArgs(){
510+
long rowsFound = -1L;
511+
database.execSQL("create table t1(a,b);");
512+
Object[] whereArgs = null;
513+
database.execSQL("insert into t1(a,b) values(?,?)", new Object[]{1, 2});
514+
database.execSQL("insert into t1(a,b) values(?,?)", new Object[]{3, 4});
515+
long rowsDeleted = database.delete("t1", "a in (1, 3)", whereArgs);
516+
assertThat(rowsDeleted, is(2L));
517+
Cursor cursor = database.rawQuery("select count(*) from t1;", null);
518+
if(cursor != null && cursor.moveToNext()){
519+
rowsFound = cursor.getLong(0);
520+
cursor.close();
521+
}
522+
assertThat(rowsFound, is(0L));
523+
}
507524
}

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteDatabase.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,17 +1180,19 @@ public long insert(String table, int conflictAlgorithm, ContentValues values) th
11801180

11811181
@Override
11821182
public int delete(String table, String whereClause, Object[] whereArgs) {
1183-
String[] args = new String[whereArgs.length];
1184-
for(int index = 0; index < whereArgs.length; index++) {
1183+
int length = whereArgs == null ? 0 : whereArgs.length;
1184+
String[] args = new String[length];
1185+
for(int index = 0; index < length; index++) {
11851186
args[index] = whereArgs[index].toString();
11861187
}
11871188
return delete(table, whereClause, args);
11881189
}
11891190

11901191
@Override
11911192
public int update(String table, int conflictAlgorithm, ContentValues values, String whereClause, Object[] whereArgs) {
1192-
String[] args = new String[whereArgs.length];
1193-
for(int index = 0; index < whereArgs.length; index++) {
1193+
int length = whereArgs == null ? 0 : whereArgs.length;
1194+
String[] args = new String[length];
1195+
for(int index = 0; index < length; index++) {
11941196
args[index] = whereArgs[index].toString();
11951197
}
11961198
return updateWithOnConflict(table, values, whereClause, args, conflictAlgorithm);

0 commit comments

Comments
 (0)
0