mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Fix asan/msan bug
PiperOrigin-RevId: 780237506
This commit is contained in:
committed by
Copybara-Service
parent
6a5d39eb59
commit
a2bcb2db27
@@ -27,7 +27,7 @@ std::string ByteUtils::ToFourDigitString(ByteArray& bytes) {
|
||||
int multiplier = 1;
|
||||
int hashCode = 0;
|
||||
|
||||
StreamReader stream_reader{bytes};
|
||||
StreamReader stream_reader{&bytes};
|
||||
while (stream_reader.IsAvailable(1)) {
|
||||
auto byte = stream_reader.ReadInt8().value_or(0);
|
||||
hashCode = (hashCode + byte * multiplier) % kHashBasePrime;
|
||||
|
||||
@@ -31,7 +31,7 @@ std::optional<std::uint8_t> StreamReader::ReadBits(int bits) {
|
||||
if (!IsAvailable(1)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
bits_buffer_ = (uint8_t)buffer_.data()[position_++];
|
||||
bits_buffer_ = (uint8_t)buffer_->data()[position_++];
|
||||
bits_unused_ = 8;
|
||||
}
|
||||
if (bits_unused_ < bits) {
|
||||
@@ -177,7 +177,7 @@ ExceptionOr<ByteArray> StreamReader::Read(std::int64_t size) {
|
||||
}
|
||||
|
||||
ByteArray read_bytes{static_cast<size_t>(size)};
|
||||
if (read_bytes.CopyAt(/*offset=*/0, buffer_,
|
||||
if (read_bytes.CopyAt(/*offset=*/0, *buffer_,
|
||||
/*source_offset=*/position_)) {
|
||||
position_ += size;
|
||||
return ExceptionOr<ByteArray>{read_bytes};
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace nearby {
|
||||
// A base {@link InputStream } for reading the contents of a byte array.
|
||||
class StreamReader {
|
||||
public:
|
||||
explicit StreamReader(const ByteArray &buffer) : buffer_{buffer} {}
|
||||
explicit StreamReader(const ByteArray *buffer) : buffer_{buffer} {}
|
||||
StreamReader(const StreamReader &) = delete;
|
||||
StreamReader &operator=(const StreamReader &) = delete;
|
||||
~StreamReader() = default;
|
||||
@@ -46,7 +46,7 @@ class StreamReader {
|
||||
std::optional<ByteArray> ReadBytes(int size);
|
||||
|
||||
bool IsAvailable(int size) const {
|
||||
return buffer_.size() - position_ >= size;
|
||||
return buffer_ != nullptr && (buffer_->size() - position_ >= size);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -54,7 +54,7 @@ class StreamReader {
|
||||
|
||||
uint8_t bits_unused_{0};
|
||||
uint8_t bits_buffer_{0};
|
||||
const ByteArray &buffer_;
|
||||
const ByteArray *buffer_ = nullptr;
|
||||
size_t position_{0};
|
||||
};
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace {
|
||||
TEST(StreamReaderTest, ReadBits) {
|
||||
std::string data{static_cast<char>(0b01011100)};
|
||||
ByteArray byte_array(data);
|
||||
StreamReader stream{byte_array};
|
||||
StreamReader stream{&byte_array};
|
||||
EXPECT_EQ(stream.ReadBits(1), 0);
|
||||
EXPECT_EQ(stream.ReadBits(2), 2);
|
||||
EXPECT_EQ(stream.ReadBits(3), 7);
|
||||
@@ -37,7 +37,7 @@ TEST(StreamReaderTest, ReadBits) {
|
||||
TEST(StreamReaderTest, ReadBitsExceedsByteBoundary) {
|
||||
std::string data = "ab";
|
||||
ByteArray byte_array(data);
|
||||
StreamReader stream{byte_array};
|
||||
StreamReader stream{&byte_array};
|
||||
EXPECT_FALSE(stream.ReadBits(9).has_value());
|
||||
EXPECT_EQ(stream.ReadBits(1), 0);
|
||||
EXPECT_FALSE(stream.ReadInt16().has_value());
|
||||
@@ -46,7 +46,7 @@ TEST(StreamReaderTest, ReadBitsExceedsByteBoundary) {
|
||||
TEST(StreamReaderTest, ReadUintValues) {
|
||||
std::string data = "\xff\xf1\x0f\x0e\x01\x02\x01\x01\x01\x02\x03\x04\x05\x06";
|
||||
ByteArray byte_array(data);
|
||||
StreamReader stream{byte_array};
|
||||
StreamReader stream{&byte_array};
|
||||
EXPECT_EQ(stream.ReadUint16(), 0xfff1);
|
||||
EXPECT_EQ(stream.ReadUint32(), 0x0f0e0102);
|
||||
EXPECT_EQ(stream.ReadUint64(), 0x0101010203040506);
|
||||
@@ -55,7 +55,7 @@ TEST(StreamReaderTest, ReadUintValues) {
|
||||
TEST(StreamReaderTest, ReadIntValues) {
|
||||
std::string data = "\xff\xf1\x0f\x0e\x01\x02\x01\x01\x01\x02\x03\x04\x05\x06";
|
||||
ByteArray byte_array(data);
|
||||
StreamReader stream{byte_array};
|
||||
StreamReader stream{&byte_array};
|
||||
EXPECT_EQ(stream.ReadInt16(), static_cast<int16_t>(0xfff1));
|
||||
EXPECT_EQ(stream.ReadInt32(), static_cast<int32_t>(0x0f0e0102));
|
||||
EXPECT_EQ(stream.ReadInt64(), static_cast<int64_t>(0x0101010203040506));
|
||||
|
||||
Reference in New Issue
Block a user