From 150e8b1276b4c984de89e9f2dfaa8cdfb1807cfa Mon Sep 17 00:00:00 2001 From: nivy Date: Tue, 1 Sep 2026 00:24:20 -0700 Subject: [PATCH] Add try/catch and failing test --- .../apache/cassandra/cql3/terms/InMarker.java | 11 +++- .../cassandra/cql3/terms/InMarkerTest.java | 64 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 test/unit/org/apache/cassandra/cql3/terms/InMarkerTest.java diff --git a/src/java/org/apache/cassandra/cql3/terms/InMarker.java b/src/java/org/apache/cassandra/cql3/terms/InMarker.java index 7941acbfde40..c16374b7d1c9 100644 --- a/src/java/org/apache/cassandra/cql3/terms/InMarker.java +++ b/src/java/org/apache/cassandra/cql3/terms/InMarker.java @@ -33,6 +33,7 @@ import org.apache.cassandra.db.marshal.ListType; import org.apache.cassandra.db.marshal.MultiElementType; import org.apache.cassandra.exceptions.InvalidRequestException; +import org.apache.cassandra.serializers.MarshalException; import org.apache.cassandra.utils.ByteBufferUtil; /** @@ -77,7 +78,15 @@ private Terminals toTerminals(ByteBuffer value, ListType type, java.util.function.Function terminalConverter) { - List elements = type.getSerializer().deserialize(value, ByteBufferAccessor.instance); + List elements; + try + { + elements = type.getSerializer().deserialize(value, ByteBufferAccessor.instance); + } + catch (MarshalException e) + { + throw new InvalidRequestException(e.getMessage()); + } List terminals = new ArrayList<>(elements.size()); for (T element : elements) { diff --git a/test/unit/org/apache/cassandra/cql3/terms/InMarkerTest.java b/test/unit/org/apache/cassandra/cql3/terms/InMarkerTest.java new file mode 100644 index 000000000000..36d7233cb395 --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/terms/InMarkerTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.cql3.terms; + +import java.nio.ByteBuffer; +import java.util.Collections; + +import org.junit.Test; + +import org.apache.cassandra.cql3.CQLTester; +import org.apache.cassandra.cql3.QueryOptions; +import org.apache.cassandra.cql3.statements.SelectStatement; +import org.apache.cassandra.exceptions.InvalidRequestException; +import org.apache.cassandra.service.QueryState; + +import static org.junit.Assert.fail; + +public class InMarkerTest extends CQLTester +{ + @Test + public void testNotEnoughBytesThrowsInvalidRequest() throws Throwable + { + assertInMarkerRejectsMalformedValue(new byte[]{ 0, 0, 0, 1 }); + } + + @Test + public void testExtraneousBytesThrowsInvalidRequest() throws Throwable + { + assertInMarkerRejectsMalformedValue(new byte[]{ 0, 0, 0, 0, 9, 9 }); + } + + private void assertInMarkerRejectsMalformedValue(byte[] malformedListBytes) throws Throwable + { + createTable("CREATE TABLE %s (pk int PRIMARY KEY, v int)"); + SelectStatement select = (SelectStatement) parseStatement("SELECT * FROM " + KEYSPACE + '.' + currentTable() + " WHERE pk IN ?"); + + QueryOptions options = QueryOptions.forInternalCalls(Collections.singletonList(ByteBuffer.wrap(malformedListBytes))); + try + { + select.getQuery(options, QueryState.forInternalCalls().getNowInSeconds()); + fail("Expected InvalidRequestException to be thrown for a malformed IN marker value"); + } + catch (InvalidRequestException e) + { + // expected + } + } +}