4949#include " config.h"
5050#include " error.h"
5151
52+ // Helper function to parse JSON string using thread-safe CharReaderBuilder
53+ static bool parseJsonString (const std::string &json_str, Json::Value &value,
54+ std::string *error_msg = nullptr ) {
55+ Json::CharReaderBuilder builder;
56+ std::unique_ptr<Json::CharReader> reader (builder.newCharReader ());
57+ std::string errs;
58+
59+ bool success = reader->parse (
60+ json_str.data (), json_str.data () + json_str.size (), &value, &errs);
61+ if (!success && error_msg) {
62+ *error_msg = errs;
63+ }
64+ return success;
65+ }
66+
5267namespace mooncake {
5368#ifdef USE_REDIS
5469struct RedisStoragePlugin : public MetadataStoragePlugin {
@@ -118,7 +133,6 @@ struct RedisStoragePlugin : public MetadataStoragePlugin {
118133 std::lock_guard<std::mutex> lock (access_client_mutex_);
119134 if (!client_) return false ;
120135
121- Json::Reader reader;
122136 redisReply *resp =
123137 (redisReply *)redisCommand (client_, " GET %s" , key.c_str ());
124138 if (!resp) {
@@ -135,7 +149,12 @@ struct RedisStoragePlugin : public MetadataStoragePlugin {
135149
136150 auto json_file = std::string (resp->str );
137151 freeReplyObject (resp);
138- if (!reader.parse (json_file, value)) return false ;
152+
153+ std::string errs;
154+ if (!parseJsonString (json_file, value, &errs)) {
155+ LOG (ERROR) << " RedisStoragePlugin: JSON parse error: " << errs;
156+ return false ;
157+ }
139158 return true ;
140159 }
141160
@@ -374,15 +393,19 @@ struct EtcdStoragePlugin : public MetadataStoragePlugin {
374393 virtual ~EtcdStoragePlugin () {}
375394
376395 virtual bool get (const std::string &key, Json::Value &value) {
377- Json::Reader reader;
378396 auto resp = client_.get (key);
379397 if (!resp.is_ok ()) {
380398 LOG (ERROR) << " EtcdStoragePlugin: unable to get " << key << " from "
381399 << metadata_uri_ << " : " << resp.error_message ();
382400 return false ;
383401 }
384402 auto json_file = resp.value ().as_string ();
385- if (!reader.parse (json_file, value)) return false ;
403+
404+ std::string errs;
405+ if (!parseJsonString (json_file, value, &errs)) {
406+ LOG (ERROR) << " EtcdStoragePlugin: JSON parse error: " << errs;
407+ return false ;
408+ }
386409 return true ;
387410 }
388411
@@ -429,7 +452,6 @@ struct EtcdStoragePlugin : public MetadataStoragePlugin {
429452 virtual ~EtcdStoragePlugin () { EtcdCloseWrapper (); }
430453
431454 virtual bool get (const std::string &key, Json::Value &value) {
432- Json::Reader reader;
433455 char *json_data = nullptr ;
434456 auto ret = EtcdGetWrapper ((char *)key.c_str (), &json_data, &err_msg_);
435457 if (ret) {
@@ -446,7 +468,12 @@ struct EtcdStoragePlugin : public MetadataStoragePlugin {
446468 auto json_file = std::string (json_data);
447469 // free the memory allocated by EtcdGetWrapper
448470 free (json_data);
449- if (!reader.parse (json_file, value)) return false ;
471+
472+ std::string errs;
473+ if (!parseJsonString (json_file, value, &errs)) {
474+ LOG (ERROR) << " EtcdStoragePlugin: JSON parse error: " << errs;
475+ return false ;
476+ }
450477 return true ;
451478 }
452479
@@ -716,16 +743,16 @@ struct SocketHandShakePlugin : public HandShakePlugin {
716743 getNetworkAddress ((struct sockaddr *)&addr);
717744
718745 Json::Value local, peer;
719- Json::Reader reader;
720746
721747 auto [type, json_str] = readString (conn_fd);
722- if (!reader.parse (json_str, peer)) {
723- LOG (ERROR) << " SocketHandShakePlugin: failed to receive "
724- " handshake message, "
725- " malformed json format:"
726- << reader.getFormattedErrorMessages ()
727- << " , json string length: " << json_str.size ()
728- << " , json string content: " << json_str;
748+ std::string errs;
749+ if (!parseJsonString (json_str, peer, &errs)) {
750+ LOG (ERROR)
751+ << " SocketHandShakePlugin: failed to receive "
752+ " handshake message, "
753+ " malformed json format: "
754+ << errs << " , json string length: " << json_str.size ()
755+ << " , json string content: " << json_str;
729756 close (conn_fd);
730757 continue ;
731758 }
@@ -906,7 +933,6 @@ struct SocketHandShakePlugin : public HandShakePlugin {
906933 return ret;
907934 }
908935
909- Json::Reader reader;
910936 auto [type, json_str] = readString (conn_fd);
911937 if (type != HandShakeRequestType::Connection) {
912938 LOG (ERROR)
@@ -915,10 +941,11 @@ struct SocketHandShakePlugin : public HandShakePlugin {
915941 return ERR_SOCKET;
916942 }
917943
918- if (!reader.parse (json_str, peer)) {
944+ std::string errs;
945+ if (!parseJsonString (json_str, peer, &errs)) {
919946 LOG (ERROR) << " SocketHandShakePlugin: failed to receive handshake "
920- " message: "
921- " malformed json format, check tcp connection " ;
947+ " message: malformed json format: "
948+ << errs ;
922949 close (conn_fd);
923950 return ERR_MALFORMED_JSON;
924951 }
@@ -981,7 +1008,6 @@ struct SocketHandShakePlugin : public HandShakePlugin {
9811008 return ret;
9821009 }
9831010
984- Json::Reader reader;
9851011 auto [type, json_str] = readString (conn_fd);
9861012 if (type != HandShakeRequestType::Notify) {
9871013 LOG (ERROR)
@@ -993,10 +1019,11 @@ struct SocketHandShakePlugin : public HandShakePlugin {
9931019 // LOG(INFO) << "SocketHandShakePlugin: received metadata message: "
9941020 // << json_str;
9951021
996- if (!reader.parse (json_str, peer_notify)) {
1022+ std::string errs;
1023+ if (!parseJsonString (json_str, peer_notify, &errs)) {
9971024 LOG (ERROR) << " SocketHandShakePlugin: failed to receive metadata "
9981025 " message, malformed json format: "
999- << reader. getFormattedErrorMessages () ;
1026+ << errs ;
10001027 close (conn_fd);
10011028 return ERR_MALFORMED_JSON;
10021029 }
@@ -1023,7 +1050,6 @@ struct SocketHandShakePlugin : public HandShakePlugin {
10231050 return ret;
10241051 }
10251052
1026- Json::Reader reader;
10271053 auto [type, json_str] = readString (conn_fd);
10281054 if (type != HandShakeRequestType::Metadata) {
10291055 LOG (ERROR)
@@ -1035,10 +1061,11 @@ struct SocketHandShakePlugin : public HandShakePlugin {
10351061 // LOG(INFO) << "SocketHandShakePlugin: received metadata message: "
10361062 // << json_str;
10371063
1038- if (!reader.parse (json_str, peer_metadata)) {
1064+ std::string errs;
1065+ if (!parseJsonString (json_str, peer_metadata, &errs)) {
10391066 LOG (ERROR) << " SocketHandShakePlugin: failed to receive metadata "
10401067 " message, malformed json format: "
1041- << reader. getFormattedErrorMessages () ;
1068+ << errs ;
10421069 close (conn_fd);
10431070 return ERR_MALFORMED_JSON;
10441071 }
0 commit comments