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

[lcm_gen] Fix bounds checking in legacy API #22527

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions tools/lcm_gen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class @@STRUCT_NAME@@ {
int64_t encode(void* buf, int64_t offset, int64_t maxlen) const {
uint8_t* const _begin = static_cast<uint8_t*>(buf);
uint8_t* const _start = _begin + offset;
uint8_t* const _end = _begin + maxlen;
uint8_t* const _end = _start + maxlen;
uint8_t* _cursor = _start;
return this->_encode<with_hash>(&_cursor, _end) ? (_cursor - _start) : -1;
}
Expand All @@ -384,7 +384,7 @@ class @@STRUCT_NAME@@ {
int64_t decode(const void* buf, int64_t offset, int64_t maxlen) {
const uint8_t* const _begin = static_cast<const uint8_t*>(buf);
const uint8_t* const _start = _begin + offset;
const uint8_t* const _end = _begin + maxlen;
const uint8_t* const _end = _start + maxlen;
const uint8_t* _cursor = _start;
return this->_decode<with_hash>(&_cursor, _end) ? (_cursor - _start) : -1;
}
Expand Down
34 changes: 34 additions & 0 deletions tools/lcm_gen/test/functional_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,40 @@ GTEST_TEST(PapaTest, LimaRoundTrip) {
EXPECT_EQ(send.encode(data.data(), 0, data.size()), -1);
}

// Check passing a non-zero offset to encode and decode.
GTEST_TEST(PapaTest, LimaNonZeroOffset) {
papa::lima send{};
send.golf = true;
send.bravo = 22;
send.delta = 2.25;
send.foxtrot = 22.125;
send.india8 = 22;
send.india16 = 22222;
send.india32 = 22222222;
send.india64 = 222222222222222222;

// Encode, but starting at a non-zero offset into the destination buffer.
const int64_t num_bytes = send.getEncodedSize();
const int offset = 3;
std::vector<uint8_t> data(static_cast<size_t>(offset + num_bytes), uint8_t{});
const int64_t used_bytes = send.encode(data.data(), offset, num_bytes);
ASSERT_EQ(used_bytes, num_bytes);

// Decode, but starting from a non-zero offset the input buffer.
papa::lima receive{};
const int64_t read_bytes =
receive.decode(data.data(), offset, ssize(data) - offset);
ASSERT_EQ(read_bytes, num_bytes);
EXPECT_EQ(receive.golf, true);
EXPECT_EQ(receive.bravo, 22);
EXPECT_EQ(receive.delta, 2.25);
EXPECT_EQ(receive.foxtrot, 22.125);
EXPECT_EQ(receive.india8, 22);
EXPECT_EQ(receive.india16, 22222);
EXPECT_EQ(receive.india32, 22222222);
EXPECT_EQ(receive.india64, 222222222222222222);
}

// With the old lcm-gen, partially-fixed-size arrays are typed as std::vector so
// need manual size bookkeeping.
template <class T>
Expand Down
4 changes: 2 additions & 2 deletions tools/lcm_gen/test/goal/lima.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class lima {
int64_t encode(void* buf, int64_t offset, int64_t maxlen) const {
uint8_t* const _begin = static_cast<uint8_t*>(buf);
uint8_t* const _start = _begin + offset;
uint8_t* const _end = _begin + maxlen;
uint8_t* const _end = _start + maxlen;
uint8_t* _cursor = _start;
return this->_encode<with_hash>(&_cursor, _end) ? (_cursor - _start) : -1;
}
Expand All @@ -61,7 +61,7 @@ class lima {
int64_t decode(const void* buf, int64_t offset, int64_t maxlen) {
const uint8_t* const _begin = static_cast<const uint8_t*>(buf);
const uint8_t* const _start = _begin + offset;
const uint8_t* const _end = _begin + maxlen;
const uint8_t* const _end = _start + maxlen;
const uint8_t* _cursor = _start;
return this->_decode<with_hash>(&_cursor, _end) ? (_cursor - _start) : -1;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/lcm_gen/test/goal/mike.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class mike {
int64_t encode(void* buf, int64_t offset, int64_t maxlen) const {
uint8_t* const _begin = static_cast<uint8_t*>(buf);
uint8_t* const _start = _begin + offset;
uint8_t* const _end = _begin + maxlen;
uint8_t* const _end = _start + maxlen;
uint8_t* _cursor = _start;
return this->_encode<with_hash>(&_cursor, _end) ? (_cursor - _start) : -1;
}
Expand All @@ -80,7 +80,7 @@ class mike {
int64_t decode(const void* buf, int64_t offset, int64_t maxlen) {
const uint8_t* const _begin = static_cast<const uint8_t*>(buf);
const uint8_t* const _start = _begin + offset;
const uint8_t* const _end = _begin + maxlen;
const uint8_t* const _end = _start + maxlen;
const uint8_t* _cursor = _start;
return this->_decode<with_hash>(&_cursor, _end) ? (_cursor - _start) : -1;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/lcm_gen/test/goal/november.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class november {
int64_t encode(void* buf, int64_t offset, int64_t maxlen) const {
uint8_t* const _begin = static_cast<uint8_t*>(buf);
uint8_t* const _start = _begin + offset;
uint8_t* const _end = _begin + maxlen;
uint8_t* const _end = _start + maxlen;
uint8_t* _cursor = _start;
return this->_encode<with_hash>(&_cursor, _end) ? (_cursor - _start) : -1;
}
Expand All @@ -46,7 +46,7 @@ class november {
int64_t decode(const void* buf, int64_t offset, int64_t maxlen) {
const uint8_t* const _begin = static_cast<const uint8_t*>(buf);
const uint8_t* const _start = _begin + offset;
const uint8_t* const _end = _begin + maxlen;
const uint8_t* const _end = _start + maxlen;
const uint8_t* _cursor = _start;
return this->_decode<with_hash>(&_cursor, _end) ? (_cursor - _start) : -1;
}
Expand Down