Skip to content

Commit

Permalink
improve errors and fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat committed Sep 19, 2024
1 parent 1d739f4 commit 2f01aa7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
44 changes: 20 additions & 24 deletions silkworm/db/snapshot_decompressor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,35 +258,31 @@ TEST_CASE("Decompressor::open invalid files", "[silkworm][node][seg][decompresso
Decompressor decoder{tmp_file.path()};
CHECK_THROWS_MATCHES(decoder.open(), std::runtime_error, Message("compressed file is too short: 31"));
}
SECTION("cannot build pattern tree: highest_depth reached zero") {
TemporaryFile tmp_file;
tmp_file.write(*silkworm::from_hex("0x000000000000000C000000000000000400000000000000150309000000000000"));
Decompressor decoder{tmp_file.path()};
CHECK_THROWS_MATCHES(decoder.open(), std::runtime_error, Message("cannot build pattern tree: highest_depth reached zero"));
SECTION("invalid pattern_dict_length for compressed file size: 32") {
TemporaryFile tmp_file1;
tmp_file1.write(*silkworm::from_hex("0x000000000000000C000000000000000400000000000000150309000000000000"));
Decompressor decoder1{tmp_file1.path()};
CHECK_THROWS_MATCHES(decoder1.open(), std::runtime_error, Message("invalid pattern_dict_length for compressed file size: 32"));
TemporaryFile tmp_file2;
tmp_file2.write(*silkworm::from_hex("0x0000000000000000000000000000000000000000000000010000000000000000"));
Decompressor decoder2{tmp_file2.path()};
CHECK_THROWS_MATCHES(decoder2.open(), std::runtime_error, Message("invalid pattern_dict_length for compressed file size: 32"));
}
SECTION("pattern dict is invalid: data skip failed at 22") {
SECTION("invalid pattern_dict_length for compressed file size: 34") {
TemporaryFile tmp_file;
tmp_file.write(*silkworm::from_hex("0x000000000000000C00000000000000040000000000000016000000000000000003ff"));
Decompressor decoder{tmp_file.path()};
CHECK_THROWS_MATCHES(decoder.open(), std::runtime_error, Message("pattern dict is invalid: data skip failed at 11"));
}
SECTION("pattern dict is invalid: length read failed at 1") {
TemporaryFile tmp_file;
tmp_file.write(*silkworm::from_hex("0x0000000000000000000000000000000000000000000000010000000000000000"));
Decompressor decoder{tmp_file.path()};
CHECK_THROWS_MATCHES(decoder.open(), std::runtime_error, Message("pattern dict is invalid: length read failed at 1"));
CHECK_THROWS_MATCHES(decoder.open(), std::runtime_error, Message("invalid pattern_dict_length for compressed file size: 34"));
}
SECTION("cannot build position tree: highest_depth reached zero") {
TemporaryFile tmp_file;
tmp_file.write(*silkworm::from_hex("0x000000000000000C0000000000000004000000000000000000000000000000160309"));
Decompressor decoder{tmp_file.path()};
CHECK_THROWS_MATCHES(decoder.open(), std::runtime_error, Message("cannot build position tree: highest_depth reached zero"));
}
SECTION("position dict is invalid: position read failed at 22") {
TemporaryFile tmp_file;
tmp_file.write(*silkworm::from_hex("0x000000000000000C00000000000000040000000000000000000000000000001603ff"));
Decompressor decoder{tmp_file.path()};
CHECK_THROWS_MATCHES(decoder.open(), std::runtime_error, Message("position dict is invalid: position read failed at 22"));
SECTION("invalid position_dict_length for compressed file size: 34") {
TemporaryFile tmp_file1;
tmp_file1.write(*silkworm::from_hex("0x000000000000000C0000000000000004000000000000000000000000000000160309"));
Decompressor decoder1{tmp_file1.path()};
CHECK_THROWS_MATCHES(decoder1.open(), std::runtime_error, Message("invalid position_dict_length for compressed file size: 34"));
TemporaryFile tmp_file2;
tmp_file2.write(*silkworm::from_hex("0x000000000000000C00000000000000040000000000000000000000000000001603ff"));
Decompressor decoder2{tmp_file2.path()};
CHECK_THROWS_MATCHES(decoder2.open(), std::runtime_error, Message("invalid position_dict_length for compressed file size: 34"));
}
}

Expand Down
4 changes: 2 additions & 2 deletions silkworm/db/snapshots/seg/decompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ void Decompressor::open() {
const auto pattern_dict_length = endian::load_big_u64(address + kWordsCountSize + kEmptyWordsCountSize);
SILK_TRACE << "Decompress pattern dictionary length: " << pattern_dict_length;
if (pattern_dict_length > compressed_file_size - kMinimumFileSize) {
throw std::runtime_error("compressed file is too short: " + std::to_string(compressed_file_size));
throw std::runtime_error("invalid pattern_dict_length for compressed file size: " + std::to_string(compressed_file_size));
}

const std::size_t patterns_dict_offset{kWordsCountSize + kEmptyWordsCountSize + kDictionaryLengthSize};
Expand All @@ -374,7 +374,7 @@ void Decompressor::open() {
const auto position_dict_length = endian::load_big_u64(address + patterns_dict_offset + pattern_dict_length);
SILK_TRACE << "Decompress position dictionary length: " << position_dict_length;
if (position_dict_length > compressed_file_size - pattern_dict_length - kMinimumFileSize) {
throw std::runtime_error("compressed file is too short: " + std::to_string(compressed_file_size));
throw std::runtime_error("invalid position_dict_length for compressed file size: " + std::to_string(compressed_file_size));
}

const std::size_t positions_dict_offset{patterns_dict_offset + pattern_dict_length + kDictionaryLengthSize};
Expand Down

0 comments on commit 2f01aa7

Please sign in to comment.