Skip to content

Commit

Permalink
fix:Segmentation fault in zremrangebyrank (#2896)
Browse files Browse the repository at this point in the history
Co-authored-by: chejinge <chejinge@360.cn>
  • Loading branch information
chejinge and brother-jin authored Sep 13, 2024
1 parent 7622e91 commit 66f71c5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/pika_zset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,8 @@ void ZRemrangebyrankCmd::DoInitial() {
return;
}
key_ = argv_[1];
min_ = argv_[2];
max_ = argv_[3];
if (pstd::string2int(argv_[2].data(), argv_[2].size(), &start_rank_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
Expand Down Expand Up @@ -1384,7 +1386,7 @@ void ZRemrangebyrankCmd::DoThroughDB() {

void ZRemrangebyrankCmd::DoUpdateCache() {
if (s_.ok()) {
db_->cache()->ZRemrangebyrank(key_, min_, max_, ele_deleted_);
db_->cache()->ZRemrangebyrank(key_, min_, max_, ele_deleted_, db_);
}
}

Expand Down
70 changes: 70 additions & 0 deletions tests/integration/zset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,76 @@ var _ = Describe("Zset Commands", func() {
}}))
})

It("should perform Case 1: ZRemRangeByRank", func() {
client.Del(ctx, "zset1")

vals, err := client.ZRange(ctx, "zset1", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(BeEmpty())

err = client.ZAdd(ctx, "zset1", redis.Z{Score: 1, Member: "m1"}, redis.Z{Score: 2, Member: "m2"}, redis.Z{Score: 3, Member: "m3"}).Err()
Expect(err).NotTo(HaveOccurred())

vals, err = client.ZRange(ctx, "zset1", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"m1", "m2", "m3"}))

zRemRangeByRank := client.ZRemRangeByRank(ctx, "zset1", 0, 1)
Expect(zRemRangeByRank.Err()).NotTo(HaveOccurred())
Expect(zRemRangeByRank.Val()).To(Equal(int64(2)))

vals, err = client.ZRange(ctx, "zset1", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"m3"}))
})

It("should perform Case 2: ZRemRangeByRank", func() {
client.Del(ctx, "zset1")

err := client.ZAdd(ctx, "zset1", redis.Z{Score: 3, Member: "m3"}, redis.Z{Score: 4, Member: "m4"}).Err()
Expect(err).NotTo(HaveOccurred())

vals, err := client.ZRange(ctx, "zset1", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"m3", "m4"}))

zRemRangeByRank := client.ZRemRangeByRank(ctx, "zset1", 0, 1)
Expect(zRemRangeByRank.Err()).NotTo(HaveOccurred())
Expect(zRemRangeByRank.Val()).To(Equal(int64(2)))

vals, err = client.ZRange(ctx, "zset1", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(BeEmpty())
})

It("should perform Case 3: ZRemRangeByRank", func() {
client.Del(ctx, "zset1")

err := client.ZAdd(ctx, "zset1", redis.Z{Score: 2, Member: "m2"}, redis.Z{Score: 3, Member: "m1"},
redis.Z{Score: 3, Member: "m3"}, redis.Z{Score: 4, Member: "m4"}).Err()
Expect(err).NotTo(HaveOccurred())

vals, err := client.ZRangeWithScores(ctx, "zset1", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{
{Score: 2, Member: "m2"},
{Score: 3, Member: "m1"},
{Score: 3, Member: "m3"},
{Score: 4, Member: "m4"},
}))

zRemRangeByRank := client.ZRemRangeByRank(ctx, "zset1", 0, 1)
Expect(zRemRangeByRank.Err()).NotTo(HaveOccurred())
Expect(zRemRangeByRank.Val()).To(Equal(int64(2)))

vals, err = client.ZRangeWithScores(ctx, "zset1", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{
{Score: 3, Member: "m3"},
{Score: 4, Member: "m4"},
}))
})

It("should ZRemRangeByScore", func() {
err := client.ZAdd(ctx, "zset", redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit 66f71c5

Please sign in to comment.