diff --git a/cpp/src/encoding/dictionary_decoder.h b/cpp/src/encoding/dictionary_decoder.h index 2962c66ba..e9ea4d8ef 100644 --- a/cpp/src/encoding/dictionary_decoder.h +++ b/cpp/src/encoding/dictionary_decoder.h @@ -58,8 +58,12 @@ class DictionaryDecoder : public Decoder { } int read_String(common::String& ret_value, common::PageArena& pa, common::ByteStream& in) override { - auto std_str = read_string(in); - return ret_value.dup_from(std_str, pa); + std::string str; + int ret = read_string(str, in); + if (ret != common::E_OK) { + return ret; + } + return ret_value.dup_from(str, pa); } void init() { value_decoder_.init(); } @@ -69,13 +73,24 @@ class DictionaryDecoder : public Decoder { entry_index_.clear(); } - std::string read_string(common::ByteStream& buffer) { + int read_string(std::string& str, common::ByteStream& buffer) { + int ret = common::E_OK; if (entry_index_.empty()) { - init_map(buffer); + ret = init_map(buffer); + if (ret != common::E_OK) { + return ret; + } } int32_t code = 0; - value_decoder_.read_int(code, buffer); - return entry_index_[code]; + ret = value_decoder_.read_int(code, buffer); + if (ret != common::E_OK) { + return ret; + } + if (code < 0 || static_cast(code) >= entry_index_.size()) { + return common::E_TSFILE_CORRUPTED; + } + str = entry_index_[code]; + return ret; } bool has_next(common::ByteStream& buffer) { @@ -91,6 +106,9 @@ class DictionaryDecoder : public Decoder { if (RET_FAIL(common::SerializationUtil::read_var_int(length, buffer))) { return common::E_PARTIAL_READ; } + if (length < 0) { + return common::E_TSFILE_CORRUPTED; + } for (int i = 0; i < length; i++) { std::string str; if (RET_FAIL( diff --git a/cpp/test/encoding/dictionary_codec_test.cc b/cpp/test/encoding/dictionary_codec_test.cc index 8ec578284..f8910b794 100644 --- a/cpp/test/encoding/dictionary_codec_test.cc +++ b/cpp/test/encoding/dictionary_codec_test.cc @@ -73,7 +73,9 @@ TEST_F(DictionaryTest, ReadJavaBinaryDictionary) { decoder.init(); for (const auto& value : expected) { ASSERT_TRUE(decoder.has_next(stream)); - ASSERT_EQ(decoder.read_string(stream), value); + std::string decoded; + ASSERT_EQ(decoder.read_string(decoded, stream), common::E_OK); + ASSERT_EQ(decoded, value); } ASSERT_FALSE(decoder.has_next(stream)); } @@ -94,7 +96,9 @@ TEST_F(DictionaryTest, DictionaryPreservesBinaryStrings) { decoder.init(); for (const auto& expected : values) { ASSERT_TRUE(decoder.has_next(stream)); - ASSERT_EQ(decoder.read_string(stream), expected); + std::string decoded; + ASSERT_EQ(decoder.read_string(decoded, stream), common::E_OK); + ASSERT_EQ(decoded, expected); } ASSERT_FALSE(decoder.has_next(stream)); } @@ -113,17 +117,22 @@ TEST_F(DictionaryTest, DictionaryEncoderAndDecoder) { DictionaryDecoder decoder; decoder.init(); + std::string decoded; ASSERT_TRUE(decoder.has_next(stream)); - ASSERT_EQ(decoder.read_string(stream), "apple"); + ASSERT_EQ(decoder.read_string(decoded, stream), common::E_OK); + ASSERT_EQ(decoded, "apple"); ASSERT_TRUE(decoder.has_next(stream)); - ASSERT_EQ(decoder.read_string(stream), "banana"); + ASSERT_EQ(decoder.read_string(decoded, stream), common::E_OK); + ASSERT_EQ(decoded, "banana"); ASSERT_TRUE(decoder.has_next(stream)); - ASSERT_EQ(decoder.read_string(stream), "cherry"); + ASSERT_EQ(decoder.read_string(decoded, stream), common::E_OK); + ASSERT_EQ(decoded, "cherry"); ASSERT_TRUE(decoder.has_next(stream)); - ASSERT_EQ(decoder.read_string(stream), "apple"); + ASSERT_EQ(decoder.read_string(decoded, stream), common::E_OK); + ASSERT_EQ(decoded, "apple"); } TEST_F(DictionaryTest, DictionaryEncoderAndDecoderOneItem) { @@ -138,7 +147,9 @@ TEST_F(DictionaryTest, DictionaryEncoderAndDecoderOneItem) { decoder.init(); ASSERT_TRUE(decoder.has_next(stream)); - ASSERT_EQ(decoder.read_string(stream), "apple"); + std::string decoded; + ASSERT_EQ(decoder.read_string(decoded, stream), common::E_OK); + ASSERT_EQ(decoded, "apple"); ASSERT_FALSE(decoder.has_next(stream)); } @@ -160,7 +171,9 @@ TEST_F(DictionaryTest, DictionaryEncoderAndDecoderRepeatedItems) { for (char c = 'a'; c <= 'z'; c++) { for (int i = 0; i < 100; i++) { - ASSERT_EQ(decoder.read_string(stream), std::string(c, 3)); + std::string decoded; + ASSERT_EQ(decoder.read_string(decoded, stream), common::E_OK); + ASSERT_EQ(decoded, std::string(c, 3)); } } } @@ -209,7 +222,8 @@ TEST_F(DictionaryTest, // Decode and verify all strings for (const auto& expected_str : test_strings) { - std::string decoded_str = decoder.read_string(stream); + std::string decoded_str; + ASSERT_EQ(decoder.read_string(decoded_str, stream), common::E_OK); ASSERT_EQ(decoded_str, expected_str); } } diff --git a/cpp/test/encoding/dictionary_decoder_corruption_test.cc b/cpp/test/encoding/dictionary_decoder_corruption_test.cc new file mode 100644 index 000000000..44ea1ec4e --- /dev/null +++ b/cpp/test/encoding/dictionary_decoder_corruption_test.cc @@ -0,0 +1,46 @@ +/* + * 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. + */ +#include + +#include "encoding/dictionary_decoder.h" + +namespace storage { + +class DictionaryDecoderCorruptionTest : public ::testing::Test {}; + +TEST_F(DictionaryDecoderCorruptionTest, NegativeEntryCountReturnsCorrupted) { + // A dictionary page begins with a var-int entry count. A negative count + // (e.g. -1) is corrupt: it must be rejected as E_TSFILE_CORRUPTED rather + // than dereferencing an empty entry index (which used to SIGSEGV). + common::ByteStream stream(64, common::MOD_DICENCODE_OBJ); + common::SerializationUtil::write_var_int(-1, stream); + // Trailing encoded-index data; never reached once the negative count is + // rejected. + common::SerializationUtil::write_var_int(0, stream); + + DictionaryDecoder decoder; + decoder.init(); + + common::PageArena pa; + pa.init(64, common::MOD_DEFAULT); + common::String out; + ASSERT_EQ(decoder.read_String(out, pa, stream), common::E_TSFILE_CORRUPTED); +} + +} // namespace storage