Skip to content

Commit ccdfd89

Browse files
authored
fix some problems(json parser/pb lite/connection block) (#153)
- fix TransformConfig return true convert not valid json-string to json-object - complete pb-lite seriazation support - fix connection block problem when WritingBufferList at high payload
1 parent 61c88a8 commit ccdfd89

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

trpc/config/trpc_conf_compatible.cc

+5
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ bool TransformConfig(const std::string& from, Json::Value* json_value) {
246246
TRPC_FMT_ERROR("Json parse error. content:{}, error:{}", from, err_msg);
247247
return false;
248248
}
249+
// It will cause coredump if str `from` is not json-format.
250+
if (!json_value->isArray() && !json_value->isObject()) {
251+
TRPC_FMT_ERROR("Json is not array or object. content:{}", from);
252+
return false;
253+
}
249254
} catch (std::exception& ex) {
250255
TRPC_FMT_ERROR("parse string to json ex:{} ", ex.what());
251256
return false;

trpc/config/trpc_conf_compatible_test.cc

+7
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,11 @@ TEST_F(TrpcConfCompatibleTest, TransformConfig_JSON_Fail_WithIntegerOverflow) {
190190
ASSERT_FALSE(trpc::config::GetInt64(json_value, "int_overflow", &v));
191191
}
192192

193+
TEST_F(TrpcConfCompatibleTest, FindValueFromJson_json_string) {
194+
std::string from = R"(1)";
195+
Json::Value json_value;
196+
bool trans_result = trpc::config::TransformConfig(from, &json_value);
197+
ASSERT_FALSE(trans_result);
198+
}
199+
193200
} // namespace trpc::testing

trpc/runtime/iomodel/reactor/fiber/writing_buffer_list.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ ssize_t WritingBufferList::FlushTo(IoHandler* io, ConnectionHandler* conn_handle
117117
// We did write something out. Remove those buffers and update the result accordingly.
118118
auto flushed = static_cast<std::size_t>(rc);
119119
bool drained = false;
120-
if (size_.fetch_sub(flushed, std::memory_order_acquire) < max_capacity) {
120+
if (size_.fetch_sub(flushed, std::memory_order_acq_rel) - flushed < max_capacity) {
121121
writable_cv_.notify_one();
122122
}
123123

trpc/server/rpc/unary_rpc_method_handler.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class UnaryRpcMethodHandler : public RpcMethodHandlerInterface {
200200
private:
201201
bool Serialize(serialization::Serialization* serialization, void* rsp, NoncontiguousBuffer& buff) {
202202
serialization::DataType type;
203-
if constexpr (std::is_convertible_v<RequestType*, google::protobuf::Message*>) {
203+
if constexpr (std::is_convertible_v<RequestType*, google::protobuf::MessageLite*>) {
204204
type = serialization::kPbMessage;
205205
} else if constexpr (std::is_convertible_v<RequestType*, rapidjson::Document*>) {
206206
type = serialization::kRapidJson;
@@ -219,7 +219,7 @@ class UnaryRpcMethodHandler : public RpcMethodHandlerInterface {
219219

220220
bool Deserialize(serialization::Serialization* serialization, NoncontiguousBuffer* req_data, void* req) {
221221
serialization::DataType type;
222-
if constexpr (std::is_convertible_v<RequestType*, google::protobuf::Message*>) {
222+
if constexpr (std::is_convertible_v<RequestType*, google::protobuf::MessageLite*>) {
223223
type = serialization::kPbMessage;
224224
} else if constexpr (std::is_convertible_v<RequestType*, rapidjson::Document*>) {
225225
type = serialization::kRapidJson;

0 commit comments

Comments
 (0)