Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions cpp/src/encoding/dictionary_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand All @@ -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<size_t>(code) >= entry_index_.size()) {
return common::E_TSFILE_CORRUPTED;
}
str = entry_index_[code];
return ret;
}

bool has_next(common::ByteStream& buffer) {
Expand All @@ -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(
Expand Down
32 changes: 23 additions & 9 deletions cpp/test/encoding/dictionary_codec_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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) {
Expand All @@ -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));
}
Expand All @@ -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));
}
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
46 changes: 46 additions & 0 deletions cpp/test/encoding/dictionary_decoder_corruption_test.cc
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>

#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
Loading