Skip to content

Commit

Permalink
ZUNIONSTORE overwrites a key after potentially reading it.
Browse files Browse the repository at this point in the history
  • Loading branch information
timnd committed Aug 10, 2017
1 parent 995ba13 commit 004efaf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cmd_sorted_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,15 @@ func (m *Miniredis) cmdZunionstore(c *server.Peer, cmd string, args []string) {

withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)
db.del(destination, true)
deleteDest := true
for _, key := range keys {
if destination == key {
deleteDest = false
}
}
if deleteDest {
db.del(destination, true)
}

sset := sortedSet{}
for i, key := range keys {
Expand Down
14 changes: 14 additions & 0 deletions cmd_sorted_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,20 @@ func TestZunionstore(t *testing.T) {
equals(t, map[string]float64{"field1": 2, "field2": 4}, ss)
}

// Merge destination with itself.
{
s.ZAdd("h3", 1.0, "field1")
s.ZAdd("h3", 3.0, "field3")

res, err := redis.Int(c.Do("ZUNIONSTORE", "h3", 2, "h1", "h3"))
ok(t, err)
equals(t, 3, res)

ss, err := s.SortedSet("h3")
ok(t, err)
equals(t, map[string]float64{"field1": 2, "field2": 2, "field3": 3}, ss)
}

// WEIGHTS
{
res, err := redis.Int(c.Do("ZUNIONSTORE", "weighted", 2, "h1", "h2", "WeIgHtS", "4.5", "12"))
Expand Down

0 comments on commit 004efaf

Please sign in to comment.