Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 14 additions & 4 deletions shell/platform/common/cpp/client_wrapper/standard_codec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ EncodableValue StandardCodecSerializer::ReadValue(
case EncodedType::kString: {
size_t size = ReadSize(stream);
std::string string_value;
string_value.resize(size);
stream->ReadBytes(reinterpret_cast<uint8_t*>(&string_value[0]), size);
if (size > 0) {
string_value.resize(size);
stream->ReadBytes(reinterpret_cast<uint8_t*>(&string_value[0]), size);
}
return EncodableValue(string_value);
}
case EncodedType::kUInt8List:
Expand All @@ -126,6 +128,9 @@ EncodableValue StandardCodecSerializer::ReadValue(
case EncodedType::kList: {
size_t length = ReadSize(stream);
EncodableList list_value;
if (length == 0) {
return EncodableValue(list_value);
}
list_value.reserve(length);
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this causing a failure too? I think reserve(0) is just a no-op, and the for loop would be no-op as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I was just being over cautious.

for (size_t i = 0; i < length; ++i) {
list_value.push_back(ReadValue(stream));
Expand Down Expand Up @@ -176,8 +181,10 @@ void StandardCodecSerializer::WriteValue(const EncodableValue& value,
const auto& string_value = value.StringValue();
size_t size = string_value.size();
WriteSize(size, stream);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be included in the if statement below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No we always want to write the size because the format specifies type|size|contents and the reading code needs to read that zero to be able to decode an empty string.

Copy link
Contributor

Choose a reason for hiding this comment

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

makes sense, thanks!

stream->WriteBytes(reinterpret_cast<const uint8_t*>(string_value.data()),
size);
if (size > 0) {
stream->WriteBytes(
reinterpret_cast<const uint8_t*>(string_value.data()), size);
}
break;
}
case EncodableValue::Type::kByteList:
Expand Down Expand Up @@ -259,6 +266,9 @@ void StandardCodecSerializer::WriteVector(
ByteBufferStreamWriter* stream) const {
size_t count = vector.size();
WriteSize(count, stream);
if (count == 0) {
return;
}
uint8_t type_size = static_cast<uint8_t>(sizeof(T));
if (type_size > 1) {
stream->WriteAlignment(type_size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ TEST(StandardMessageCodec, CanEncodeAndDecodeStringWithNonBMPCodePoint) {
CheckEncodeDecode(EncodableValue(u8"h\U0001F602w"), bytes);
}

TEST(StandardMessageCodec, CanEncodeAndDecodeEmptyString) {
std::vector<uint8_t> bytes = {0x07, 0x00};
CheckEncodeDecode(EncodableValue(u8""), bytes);
}

TEST(StandardMessageCodec, CanEncodeAndDecodeList) {
std::vector<uint8_t> bytes = {
0x0c, 0x05, 0x00, 0x07, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x06,
Expand All @@ -117,6 +122,11 @@ TEST(StandardMessageCodec, CanEncodeAndDecodeList) {
CheckEncodeDecode(value, bytes);
}

TEST(StandardMessageCodec, CanEncodeAndDecodeEmptyList) {
std::vector<uint8_t> bytes = {0x0c, 0x00};
CheckEncodeDecode(EncodableValue(EncodableList{}), bytes);
}

TEST(StandardMessageCodec, CanEncodeAndDecodeMap) {
std::vector<uint8_t> bytes_prefix = {0x0d, 0x04};
EncodableValue value(EncodableMap{
Expand Down