From 79da3e6637d39a976b0219a00e8fc47311f1ef7c Mon Sep 17 00:00:00 2001 From: Andy Dunstall Date: Sun, 7 May 2023 11:34:36 +0100 Subject: [PATCH] fix: fix parsing nested arrays (#1189) Signed-off-by: Andrew Dunstall --- src/facade/redis_parser.cc | 6 +++--- src/facade/redis_parser_test.cc | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/facade/redis_parser.cc b/src/facade/redis_parser.cc index 4480966c9..c3754c234 100644 --- a/src/facade/redis_parser.cc +++ b/src/facade/redis_parser.cc @@ -276,9 +276,6 @@ auto RedisParser::ConsumeArrayLen(Buffer str) -> Result { return OK; } - DVLOG(1) << "PushStack: (" << len << ", " << cached_expr_ << ")"; - parse_stack_.emplace_back(len, cached_expr_); - if (state_ == PARSE_ARG_S) { DCHECK(!server_mode_); @@ -292,6 +289,9 @@ auto RedisParser::ConsumeArrayLen(Buffer str) -> Result { state_ = PARSE_ARG_S; } + DVLOG(1) << "PushStack: (" << len << ", " << cached_expr_ << ")"; + parse_stack_.emplace_back(len, cached_expr_); + return OK; } diff --git a/src/facade/redis_parser_test.cc b/src/facade/redis_parser_test.cc index 2f2ef254e..473142991 100644 --- a/src/facade/redis_parser_test.cc +++ b/src/facade/redis_parser_test.cc @@ -197,4 +197,16 @@ TEST_F(RedisParserTest, NILs) { ASSERT_EQ(RedisParser::OK, Parse("_\r\n")); } +TEST_F(RedisParserTest, NestedArray) { + parser_.SetClientMode(); + + // [[['foo'],['bar']],['car']] + ASSERT_EQ(RedisParser::OK, + Parse("*2\r\n*2\r\n*1\r\n$3\r\nfoo\r\n*1\r\n$3\r\nbar\r\n*1\r\n$3\r\ncar\r\n")); + + ASSERT_THAT(args_, ElementsAre(ArrArg(2), ArrArg(1))); + ASSERT_THAT(args_[0].GetVec(), ElementsAre(ArrArg(1), ArrArg(1))); + ASSERT_THAT(args_[1].GetVec(), ElementsAre("car")); +} + } // namespace facade