Elffuss BraidHub

kikocis/java-driver public

Add collection and data length checks during serialization (JAVA-363)

Patch contributed by @snazy.
Changeset6d3e2a121092cab38b8b03042aea799cf0b55694ba73db4d47b4853edb26e62e
Authorolim7t <omichallat+github@gmail.com> · 2014-08-25 17:57 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-08-25 18:07 +02:00
Parentsc6839ceb058a

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +6 −1
@@ -193,10 +193,15 @@
193193
194194 // Utility method for collections
195195 private static ByteBuffer pack(List<ByteBuffer> buffers, int elements, int size) {
196+ if (elements > 65535)
197+ throw new IllegalArgumentException("Native protocol version 2 supports up to 65535 elements in any collection - but collection contains " + elements + " elements");
196198 ByteBuffer result = ByteBuffer.allocate(2 + size);
197199 result.putShort((short)elements);
198200 for (ByteBuffer bb : buffers) {
199- result.putShort((short)bb.remaining());
201+ int elemSize = bb.remaining();
202+ if (elemSize > 65535)
203+ throw new IllegalArgumentException("Native protocol version 2 supports only elements with size up to 65535 bytes - but element size is " + elemSize + " bytes");
204+ result.putShort((short)elemSize);
200205 result.put(bb.duplicate());
201206 }
202207 return (ByteBuffer)result.flip();
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecTest.java +22 −0
@@ -15,6 +15,12 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import java.util.Collections;
19+import java.util.List;
20+
21+import org.testng.collections.Lists;
22+
23+import com.google.common.base.Strings;
1824 import org.testng.Assert;
1925 import org.testng.annotations.Test;
2026
@@ -47,4 +53,20 @@
4753 TypeCodec mapType = TypeCodec.mapOf(text(), CUSTOM_FOO);
4854 Assert.assertNotNull(mapType);
4955 }
56+
57+ @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class })
58+ public void collectionTooLargeTest() throws Exception {
59+ TypeCodec<List<Integer>> listType = TypeCodec.listOf(DataType.cint());
60+ List<Integer> list = Collections.nCopies(65536, 1);
61+
62+ listType.serialize(list);
63+ }
64+
65+ @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class })
66+ public void collectionElementTooLargeTest() throws Exception {
67+ TypeCodec<List<String>> listType = TypeCodec.listOf(DataType.text());
68+ List<String> list = Lists.newArrayList(Strings.repeat("a", 65536));
69+
70+ listType.serialize(list);
71+ }
5072 }