未验证 提交 ba6cc8c9 编写于 作者: S stuartmorgan 提交者: GitHub

Fix type mismatches in C++ standard codec (#9112)

There were some implicit casts in the standard codec implementation that
didn't show up on Linux, but do on Windows.
上级 75da3d42
...@@ -109,7 +109,7 @@ EncodableValue StandardCodecSerializer::ReadValue( ...@@ -109,7 +109,7 @@ EncodableValue StandardCodecSerializer::ReadValue(
} }
case EncodedType::kLargeInt: case EncodedType::kLargeInt:
case EncodedType::kString: { case EncodedType::kString: {
int32_t size = ReadSize(stream); size_t size = ReadSize(stream);
std::string string_value; std::string string_value;
string_value.resize(size); string_value.resize(size);
stream->ReadBytes(reinterpret_cast<uint8_t*>(&string_value[0]), size); stream->ReadBytes(reinterpret_cast<uint8_t*>(&string_value[0]), size);
...@@ -124,18 +124,18 @@ EncodableValue StandardCodecSerializer::ReadValue( ...@@ -124,18 +124,18 @@ EncodableValue StandardCodecSerializer::ReadValue(
case EncodedType::kFloat64List: case EncodedType::kFloat64List:
return ReadVector<double>(stream); return ReadVector<double>(stream);
case EncodedType::kList: { case EncodedType::kList: {
int32_t length = ReadSize(stream); size_t length = ReadSize(stream);
EncodableList list_value; EncodableList list_value;
list_value.reserve(length); list_value.reserve(length);
for (int32_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
list_value.push_back(ReadValue(stream)); list_value.push_back(ReadValue(stream));
} }
return EncodableValue(list_value); return EncodableValue(list_value);
} }
case EncodedType::kMap: { case EncodedType::kMap: {
int32_t length = ReadSize(stream); size_t length = ReadSize(stream);
EncodableMap map_value; EncodableMap map_value;
for (int32_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
EncodableValue key = ReadValue(stream); EncodableValue key = ReadValue(stream);
EncodableValue value = ReadValue(stream); EncodableValue value = ReadValue(stream);
map_value.emplace(std::move(key), std::move(value)); map_value.emplace(std::move(key), std::move(value));
...@@ -208,8 +208,7 @@ void StandardCodecSerializer::WriteValue(const EncodableValue& value, ...@@ -208,8 +208,7 @@ void StandardCodecSerializer::WriteValue(const EncodableValue& value,
} }
} }
uint32_t StandardCodecSerializer::ReadSize( size_t StandardCodecSerializer::ReadSize(ByteBufferStreamReader* stream) const {
ByteBufferStreamReader* stream) const {
uint8_t byte = stream->ReadByte(); uint8_t byte = stream->ReadByte();
if (byte < 254) { if (byte < 254) {
return byte; return byte;
...@@ -224,7 +223,7 @@ uint32_t StandardCodecSerializer::ReadSize( ...@@ -224,7 +223,7 @@ uint32_t StandardCodecSerializer::ReadSize(
} }
} }
void StandardCodecSerializer::WriteSize(uint32_t size, void StandardCodecSerializer::WriteSize(size_t size,
ByteBufferStreamWriter* stream) const { ByteBufferStreamWriter* stream) const {
if (size < 254) { if (size < 254) {
stream->WriteByte(static_cast<uint8_t>(size)); stream->WriteByte(static_cast<uint8_t>(size));
...@@ -234,17 +233,18 @@ void StandardCodecSerializer::WriteSize(uint32_t size, ...@@ -234,17 +233,18 @@ void StandardCodecSerializer::WriteSize(uint32_t size,
stream->WriteBytes(reinterpret_cast<uint8_t*>(&value), 2); stream->WriteBytes(reinterpret_cast<uint8_t*>(&value), 2);
} else { } else {
stream->WriteByte(255); stream->WriteByte(255);
stream->WriteBytes(reinterpret_cast<uint8_t*>(&size), 4); uint32_t value = static_cast<uint32_t>(size);
stream->WriteBytes(reinterpret_cast<uint8_t*>(&value), 4);
} }
} }
template <typename T> template <typename T>
EncodableValue StandardCodecSerializer::ReadVector( EncodableValue StandardCodecSerializer::ReadVector(
ByteBufferStreamReader* stream) const { ByteBufferStreamReader* stream) const {
int32_t count = ReadSize(stream); size_t count = ReadSize(stream);
std::vector<T> vector; std::vector<T> vector;
vector.resize(count); vector.resize(count);
size_t type_size = sizeof(T); uint8_t type_size = static_cast<uint8_t>(sizeof(T));
if (type_size > 1) { if (type_size > 1) {
stream->ReadAlignment(type_size); stream->ReadAlignment(type_size);
} }
...@@ -259,7 +259,7 @@ void StandardCodecSerializer::WriteVector( ...@@ -259,7 +259,7 @@ void StandardCodecSerializer::WriteVector(
ByteBufferStreamWriter* stream) const { ByteBufferStreamWriter* stream) const {
size_t count = vector.size(); size_t count = vector.size();
WriteSize(count, stream); WriteSize(count, stream);
size_t type_size = sizeof(T); uint8_t type_size = static_cast<uint8_t>(sizeof(T));
if (type_size > 1) { if (type_size > 1) {
stream->WriteAlignment(type_size); stream->WriteAlignment(type_size);
} }
......
...@@ -30,10 +30,10 @@ class StandardCodecSerializer { ...@@ -30,10 +30,10 @@ class StandardCodecSerializer {
protected: protected:
// Reads the variable-length size from the current position in |stream|. // Reads the variable-length size from the current position in |stream|.
uint32_t ReadSize(ByteBufferStreamReader* stream) const; size_t ReadSize(ByteBufferStreamReader* stream) const;
// Writes the variable-length size encoding to |stream|. // Writes the variable-length size encoding to |stream|.
void WriteSize(uint32_t size, ByteBufferStreamWriter* stream) const; void WriteSize(size_t size, ByteBufferStreamWriter* stream) const;
// Reads a fixed-type list whose values are of type T from the current // Reads a fixed-type list whose values are of type T from the current
// position in |stream|, and returns it as the corresponding EncodableValue. // position in |stream|, and returns it as the corresponding EncodableValue.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册