From 691ddac6701fb0d45b0ef2bbd49e5ab99bcc6c17 Mon Sep 17 00:00:00 2001 From: Lester Solbakken Date: Thu, 2 Sep 2021 15:59:15 +0200 Subject: Disallow feeding empty indexed tensors --- .../yahoo/document/json/readers/TensorReader.java | 4 ++++ .../yahoo/document/json/JsonReaderTestCase.java | 25 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) (limited to 'document') 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 @@ -1281,6 +1281,22 @@ public class JsonReaderTestCase { createPutWithSparseTensor(inputJson("{ 'cells': [] }"))); } + @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}}", @@ -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)); + } + } + } -- cgit v1.2.3