Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend ExtractUVarInt to support customizable kMaxVarintLen64 #1757

Closed
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
5 changes: 2 additions & 3 deletions src/stirling/utils/binary_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
namespace px {
namespace stirling {

constexpr int kMaxVarintLen64 = 10;

/**
* Provides functions to extract bytes from a bytes buffer.
Expand Down Expand Up @@ -75,7 +74,7 @@ class BinaryDecoder {
// Extract UVarInt encoded value and return result as uint64_t. The details of this encoding's
// specification can be see in the following link:
// https://cs.opensource.google/go/go/+/refs/tags/go1.20.5:src/encoding/binary/varint.go;l=7-25
StatusOr<uint64_t> ExtractUVarInt() {
StatusOr<uint64_t> ExtractUVarInt(int kMaxVarintLen64 = 10, bool cap_final_byte_val = true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k prefixed variables with camelCase are constants. Since this is a function parameter, it should be snake case and not have a prefix.

We largely follow Google's C++ style guide as mentioned here.

uint64_t x = 0;
uint bits = 0;
int i = 0;
Expand All @@ -85,7 +84,7 @@ class BinaryDecoder {
buf_.remove_prefix(1);

if (b < 0x80) {
if (i == kMaxVarintLen64 - 1 && b > 1) {
if (cap_final_byte_val && i == kMaxVarintLen64 - 1 && b > 1) {
return error::ResourceUnavailable("Insufficient number of bytes.");
}
return x | uint64_t(b) << bits;
Expand Down
37 changes: 37 additions & 0 deletions src/stirling/utils/binary_decoder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,43 @@ TEST(BinaryDecoderTest, ExtractUVarInt) {
}
}

TEST(BinaryDecoderTest, ExtractUVarIntLimit) {
std::vector<std::tuple<std::vector<uint8_t>, uint64_t, uint64_t>> uVarIntsWithLimits = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uVarIntsWithLimits should be snake case according to our style guide.

{{0x80, 0x01, 0x00}, 2, 128},
{{0xff, 0x7f, 0x00}, 2, 16383},
{{0xff, 0xff, 0x7f, 0xff}, 3, 2097151},
{{0xC0, 0xC0, 0x04}, 3, 73792},
{{0x80, 0x80, 0x80, 0x01, 0x00}, 4, 2097152},
{{0xff, 0xff, 0xff, 0x7f}, 4, 268435455},
{{0xff, 0xff, 0xff, 0x7f, 0xff}, 4, 268435455},
};

for (auto& p : uVarIntsWithLimits) {
auto data = std::get<0>(p);
auto limit = std::get<1>(p);
auto expected_val = std::get<2>(p);
std::string_view s(reinterpret_cast<char*>(data.data()), data.size());
BinaryDecoder bin_decoder(s);
ASSERT_OK_AND_EQ(bin_decoder.ExtractUVarInt(limit, false), expected_val);
}

std::vector<std::tuple<std::vector<uint8_t>, uint64_t>> uVarIntsWithLimitsOverflow = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same casing change as the earlier comment.

{{0x80}, 1},
{{0x80, 0x80, 0x80, 0x02}, 3},
{{0x80, 0x80, 0x80, 0x80, 0x80}, 4},
{{0x80, 0x80, 0x80, 0x80, 0x02}, 4},
{{0x81, 0x82, 0x83, 0x84, 0x05}, 4}
};

for (auto& p : uVarIntsWithLimitsOverflow) {
auto data = std::get<0>(p);
auto limit = std::get<1>(p);
std::string_view s(reinterpret_cast<char*>(data.data()), data.size());
BinaryDecoder bin_decoder(s);
ASSERT_NOT_OK(bin_decoder.ExtractUVarInt(limit, false));
}
}

TEST(BinaryDecoderTest, ExtractUVarIntOverflow) {
std::vector<std::vector<uint8_t>> uVarInts = {
{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02},
Expand Down
Loading