summaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorLester Solbakken <lesters@oath.com>2021-09-02 15:59:15 +0200
committerLester Solbakken <lesters@oath.com>2021-09-02 15:59:15 +0200
commit691ddac6701fb0d45b0ef2bbd49e5ab99bcc6c17 (patch)
treec2982328f7351182e4d7451f4c326563f0b24ca7 /document
parent045321f2a1b2d00d33ccf7c64c2708b6e2c94667 (diff)
Disallow feeding empty indexed tensors
Diffstat (limited to 'document')
-rw-r--r--document/src/main/java/com/yahoo/document/json/readers/TensorReader.java4
-rw-r--r--document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java25
2 files changed, 29 insertions, 0 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java b/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java
index 27426f584bd..a005a15042f 100644
--- a/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java
+++ b/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java
@@ -94,6 +94,8 @@ public class TensorReader {
IndexedTensor.BoundBuilder indexedBuilder = (IndexedTensor.BoundBuilder)builder;
if (buffer.currentToken() == JsonToken.VALUE_STRING) {
double[] decoded = decodeHexString(buffer.currentText(), builder.type().valueType());
+ if (decoded.length == 0)
+ throw new IllegalArgumentException("The 'values' string does not contain any values");
for (int i = 0; i < decoded.length; i++) {
indexedBuilder.cellByDirectIndex(i, decoded[i]);
}
@@ -104,6 +106,8 @@ public class TensorReader {
for (buffer.next(); buffer.nesting() >= initNesting; buffer.next()) {
indexedBuilder.cellByDirectIndex(index++, readDouble(buffer));
}
+ if (index == 0)
+ throw new IllegalArgumentException("The 'values' array does not contain any values");
expectCompositeEnd(buffer.currentToken());
}
diff --git a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java
index e50fd9734f7..7f4b420cf9a 100644
--- a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java
+++ b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java
@@ -1282,6 +1282,22 @@ public class JsonReaderTestCase {
}
@Test
+ public void testDisallowedDenseTensorShortFormWithoutValues() {
+ assertCreatePutFails(inputJson("{ 'values': [] }"), "dense_tensor",
+ "The 'values' array does not contain any values");
+ assertCreatePutFails(inputJson("{ 'values': '' }"), "dense_tensor",
+ "The 'values' string does not contain any values");
+ }
+
+ @Test
+ public void testDisallowedMixedTensorShortFormWithoutValues() {
+ assertCreatePutFails(inputJson("{\"blocks\":{ \"a\": [] } }"),
+ "mixed_tensor", "Expected 3 values, but got 0");
+ assertCreatePutFails(inputJson("{\"blocks\":[ {\"address\":{\"x\":\"a\"}, \"values\": [] } ] }"),
+ "mixed_tensor", "Expected 3 values, but got 0");
+ }
+
+ @Test
public void testParsingOfSparseTensorWithCells() {
Tensor tensor = assertSparseTensorField("{{x:a,y:b}:2.0,{x:c,y:b}:3.0}}",
createPutWithSparseTensor(inputJson("{",
@@ -2029,4 +2045,13 @@ public class JsonReaderTestCase {
new JsonReader(types, jsonToInputStream(jsonData), parserFactory).next();
}
+ private void assertCreatePutFails(String tensor, String name, String msg) {
+ try {
+ createPutWithTensor(inputJson(tensor), name);
+ fail("Expected exception");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains(msg));
+ }
+ }
+
}