From be0b1939b6300fb17d8ef16496b6b8ae3f5167e8 Mon Sep 17 00:00:00 2001 From: 2549141519 <2549141519@qq.com> Date: Tue, 2 Jul 2024 15:07:28 +0800 Subject: [PATCH] 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 --- trpc/config/trpc_conf_compatible.cc | 5 +++++ trpc/config/trpc_conf_compatible_test.cc | 7 +++++++ trpc/runtime/iomodel/reactor/fiber/writing_buffer_list.cc | 2 +- trpc/server/rpc/unary_rpc_method_handler.h | 4 ++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/trpc/config/trpc_conf_compatible.cc b/trpc/config/trpc_conf_compatible.cc index 9031da29..da11e786 100644 --- a/trpc/config/trpc_conf_compatible.cc +++ b/trpc/config/trpc_conf_compatible.cc @@ -246,6 +246,11 @@ bool TransformConfig(const std::string& from, Json::Value* json_value) { TRPC_FMT_ERROR("Json parse error. content:{}, error:{}", from, err_msg); return false; } + // It will cause coredump if str `from` is not json-format. + if (!json_value->isArray() && !json_value->isObject()) { + TRPC_FMT_ERROR("Json is not array or object. content:{}", from); + return false; + } } catch (std::exception& ex) { TRPC_FMT_ERROR("parse string to json ex:{} ", ex.what()); return false; diff --git a/trpc/config/trpc_conf_compatible_test.cc b/trpc/config/trpc_conf_compatible_test.cc index d7c9e81c..45d01370 100644 --- a/trpc/config/trpc_conf_compatible_test.cc +++ b/trpc/config/trpc_conf_compatible_test.cc @@ -190,4 +190,11 @@ TEST_F(TrpcConfCompatibleTest, TransformConfig_JSON_Fail_WithIntegerOverflow) { ASSERT_FALSE(trpc::config::GetInt64(json_value, "int_overflow", &v)); } +TEST_F(TrpcConfCompatibleTest, FindValueFromJson_json_string) { + std::string from = R"(1)"; + Json::Value json_value; + bool trans_result = trpc::config::TransformConfig(from, &json_value); + ASSERT_FALSE(trans_result); +} + } // namespace trpc::testing diff --git a/trpc/runtime/iomodel/reactor/fiber/writing_buffer_list.cc b/trpc/runtime/iomodel/reactor/fiber/writing_buffer_list.cc index 037c693a..2fe06f51 100644 --- a/trpc/runtime/iomodel/reactor/fiber/writing_buffer_list.cc +++ b/trpc/runtime/iomodel/reactor/fiber/writing_buffer_list.cc @@ -117,7 +117,7 @@ ssize_t WritingBufferList::FlushTo(IoHandler* io, ConnectionHandler* conn_handle // We did write something out. Remove those buffers and update the result accordingly. auto flushed = static_cast(rc); bool drained = false; - if (size_.fetch_sub(flushed, std::memory_order_acquire) < max_capacity) { + if (size_.fetch_sub(flushed, std::memory_order_acq_rel) - flushed < max_capacity) { writable_cv_.notify_one(); } diff --git a/trpc/server/rpc/unary_rpc_method_handler.h b/trpc/server/rpc/unary_rpc_method_handler.h index d840b0b3..bf794f64 100644 --- a/trpc/server/rpc/unary_rpc_method_handler.h +++ b/trpc/server/rpc/unary_rpc_method_handler.h @@ -200,7 +200,7 @@ class UnaryRpcMethodHandler : public RpcMethodHandlerInterface { private: bool Serialize(serialization::Serialization* serialization, void* rsp, NoncontiguousBuffer& buff) { serialization::DataType type; - if constexpr (std::is_convertible_v) { + if constexpr (std::is_convertible_v) { type = serialization::kPbMessage; } else if constexpr (std::is_convertible_v) { type = serialization::kRapidJson; @@ -219,7 +219,7 @@ class UnaryRpcMethodHandler : public RpcMethodHandlerInterface { bool Deserialize(serialization::Serialization* serialization, NoncontiguousBuffer* req_data, void* req) { serialization::DataType type; - if constexpr (std::is_convertible_v) { + if constexpr (std::is_convertible_v) { type = serialization::kPbMessage; } else if constexpr (std::is_convertible_v) { type = serialization::kRapidJson;