Fix NPE when deleting/setting by index on null list collection

Patch by Jeff Jirsa; reviewed by Benjamin Lerer for CASSANDRA-9077
This commit is contained in:
Jeff Jirsa 2015-04-01 12:13:52 -05:00 committed by Tyler Hobbs
parent 28af4fa04f
commit cc672f3615
3 changed files with 28 additions and 24 deletions

View File

@ -1,4 +1,6 @@
2.1.5
* Fix NullPointerException when deleting or setting an element by index on
a null list collection (CASSANDRA-9077)
* Buffer bloom filter serialization (CASSANDRA-9066)
* Fix anti-compaction target bloom filter size (CASSANDRA-9060)
* Make FROZEN and TUPLE unreserved keywords in CQL (CASSANDRA-9047)

View File

@ -342,6 +342,8 @@ public abstract class Lists
List<Cell> existingList = params.getPrefetchedList(rowKey, column.name);
int idx = ByteBufferUtil.toInt(index);
if (existingList == null)
throw new InvalidRequestException("Attempted to set an element on a list which is null");
if (idx < 0 || idx >= existingList.size())
throw new InvalidRequestException(String.format("List index %d out of bound, list has size %d", idx, existingList.size()));
@ -453,6 +455,8 @@ public abstract class Lists
// We want to call bind before possibly returning to reject queries where the value provided is not a list.
Term.Terminal value = t.bind(params.options);
if (existingList == null)
throw new InvalidRequestException("Attempted to delete an element from a list which is null");
if (existingList.isEmpty())
return;
@ -496,6 +500,8 @@ public abstract class Lists
List<Cell> existingList = params.getPrefetchedList(rowKey, column.name);
int idx = ByteBufferUtil.toInt(index.get(params.options));
if (existingList == null)
throw new InvalidRequestException("Attempted to delete an element from a list which is null");
if (idx < 0 || idx >= existingList.size())
throw new InvalidRequestException(String.format("List index %d out of bound, list has size %d", idx, existingList.size()));

View File

@ -166,51 +166,47 @@ public class CollectionsTest extends CQLTester
execute("INSERT INTO %s(k, l) VALUES (0, ?)", list("v1", "v2", "v3"));
assertRows(execute("SELECT l FROM %s WHERE k = 0"),
row(list("v1", "v2", "v3"))
);
assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v1", "v2", "v3")));
execute("DELETE l[?] FROM %s WHERE k = 0", 1);
assertRows(execute("SELECT l FROM %s WHERE k = 0"),
row(list("v1", "v3"))
);
assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v1", "v3")));
execute("UPDATE %s SET l[?] = ? WHERE k = 0", 1, "v4");
assertRows(execute("SELECT l FROM %s WHERE k = 0"),
row(list("v1", "v4"))
);
assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v1", "v4")));
// Full overwrite
execute("UPDATE %s SET l = ? WHERE k = 0", list("v6", "v5"));
assertRows(execute("SELECT l FROM %s WHERE k = 0"),
row(list("v6", "v5"))
);
assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v6", "v5")));
execute("UPDATE %s SET l = l + ? WHERE k = 0", list("v7", "v8"));
assertRows(execute("SELECT l FROM %s WHERE k = 0"),
row(list("v6", "v5", "v7", "v8"))
);
assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v6", "v5", "v7", "v8")));
execute("UPDATE %s SET l = ? + l WHERE k = 0", list("v9"));
assertRows(execute("SELECT l FROM %s WHERE k = 0"),
row(list("v9", "v6", "v5", "v7", "v8"))
);
assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v9", "v6", "v5", "v7", "v8")));
execute("UPDATE %s SET l = l - ? WHERE k = 0", list("v5", "v8"));
assertRows(execute("SELECT l FROM %s WHERE k = 0"),
row(list("v9", "v6", "v7"))
);
assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v9", "v6", "v7")));
execute("DELETE l FROM %s WHERE k = 0");
assertRows(execute("SELECT l FROM %s WHERE k = 0"),
row((Object)null)
);
assertRows(execute("SELECT l FROM %s WHERE k = 0"), row((Object) null));
assertInvalidMessage("Attempted to delete an element from a list which is null",
"DELETE l[0] FROM %s WHERE k=0 ");
assertInvalidMessage("Attempted to set an element on a list which is null",
"UPDATE %s SET l[0] = ? WHERE k=0", list("v10"));
assertInvalidMessage("Attempted to delete an element from a list which is null",
"UPDATE %s SET l = l - ? WHERE k=0 ",
list("v11"));
assertRows(execute("SELECT l FROM %s WHERE k = 0"), row((Object) null));
}
}