Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 81110bb

Browse files
rokobfranciscojma86
authored andcommitted
Support empty strings and vectors in standard codec (#12974)
* Support empty strings and vectors in standard codec Fixes #41993 Currently an empty string or vector will call through to WriteBytes which asserts that the number of bytes it is being asked to write is strictly positive. Instead we should not call WriteBytes if the length is zero. Similarly, when we read, we don't need to call out if the length is zero. * fix typo in test name * remove unnecessary length check in ReadValue for List * we also don't need this check before calling read as memcpy can handle size 0
1 parent 2302276 commit 81110bb

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

shell/platform/common/cpp/client_wrapper/standard_codec.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@ void StandardCodecSerializer::WriteValue(const EncodableValue& value,
176176
const auto& string_value = value.StringValue();
177177
size_t size = string_value.size();
178178
WriteSize(size, stream);
179-
stream->WriteBytes(reinterpret_cast<const uint8_t*>(string_value.data()),
180-
size);
179+
if (size > 0) {
180+
stream->WriteBytes(
181+
reinterpret_cast<const uint8_t*>(string_value.data()), size);
182+
}
181183
break;
182184
}
183185
case EncodableValue::Type::kByteList:
@@ -259,6 +261,9 @@ void StandardCodecSerializer::WriteVector(
259261
ByteBufferStreamWriter* stream) const {
260262
size_t count = vector.size();
261263
WriteSize(count, stream);
264+
if (count == 0) {
265+
return;
266+
}
262267
uint8_t type_size = static_cast<uint8_t>(sizeof(T));
263268
if (type_size > 1) {
264269
stream->WriteAlignment(type_size);

shell/platform/common/cpp/client_wrapper/standard_message_codec_unittests.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ TEST(StandardMessageCodec, CanEncodeAndDecodeStringWithNonBMPCodePoint) {
9797
CheckEncodeDecode(EncodableValue(u8"h\U0001F602w"), bytes);
9898
}
9999

100+
TEST(StandardMessageCodec, CanEncodeAndDecodeEmptyString) {
101+
std::vector<uint8_t> bytes = {0x07, 0x00};
102+
CheckEncodeDecode(EncodableValue(u8""), bytes);
103+
}
104+
100105
TEST(StandardMessageCodec, CanEncodeAndDecodeList) {
101106
std::vector<uint8_t> bytes = {
102107
0x0c, 0x05, 0x00, 0x07, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x06,
@@ -117,6 +122,11 @@ TEST(StandardMessageCodec, CanEncodeAndDecodeList) {
117122
CheckEncodeDecode(value, bytes);
118123
}
119124

125+
TEST(StandardMessageCodec, CanEncodeAndDecodeEmptyList) {
126+
std::vector<uint8_t> bytes = {0x0c, 0x00};
127+
CheckEncodeDecode(EncodableValue(EncodableList{}), bytes);
128+
}
129+
120130
TEST(StandardMessageCodec, CanEncodeAndDecodeMap) {
121131
std::vector<uint8_t> bytes_prefix = {0x0d, 0x04};
122132
EncodableValue value(EncodableMap{

0 commit comments

Comments
 (0)