Skip to content

Commit 473a9fd

Browse files
adiholdenromange
authored andcommitted
server(bug): fix json mget crash on invalid path (#4940)
* server: fix json mget crash on invalid path --------- Signed-off-by: adi_holden <[email protected]>
1 parent 7115c63 commit 473a9fd

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/server/json_family.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,8 @@ std::vector<std::optional<std::string>> OpJsonMGet(const WrappedJsonPath& json_p
12691269
json_val, std::move(cb), CallbackResultOptions::DefaultEvaluateOptions());
12701270

12711271
if (eval_result.IsV1()) {
1272+
if (eval_result.Empty())
1273+
return nullopt;
12721274
return eval_result.AsV1();
12731275
}
12741276

src/server/json_family_test.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "server/json_family.h"
66

7+
#include <absl/flags/flag.h>
78
#include <absl/strings/str_replace.h>
89

910
#include "base/gtest.h"
@@ -16,6 +17,8 @@ using namespace testing;
1617
using namespace std;
1718
using namespace util;
1819

20+
ABSL_DECLARE_FLAG(bool, jsonpathv2);
21+
1922
namespace dfly {
2023

2124
class JsonFamilyTest : public BaseFamilyTest {
@@ -2547,6 +2550,14 @@ TEST_F(JsonFamilyTest, MGetLegacy) {
25472550
ASSERT_EQ(RespExpr::ARRAY, resp.type);
25482551
EXPECT_THAT(resp.GetVec(), ElementsAre(R"("Israel")", R"("Germany")", ArgType(RespExpr::NIL)));
25492552

2553+
resp = Run({"JSON.MGET", "json1", "json2", ".[0]"});
2554+
if (auto jsonpathv2 = absl::GetFlag(FLAGS_jsonpathv2); jsonpathv2) {
2555+
ASSERT_EQ(RespExpr::ARRAY, resp.type);
2556+
EXPECT_THAT(resp.GetVec(), ElementsAre(ArgType(RespExpr::NIL), ArgType(RespExpr::NIL)));
2557+
} else {
2558+
EXPECT_THAT(resp, ErrArg("ERR syntax error"));
2559+
}
2560+
25502561
resp = Run({"JSON.SET", "json3", ".", json[2]});
25512562
ASSERT_THAT(resp, "OK");
25522563

0 commit comments

Comments
 (0)