Commit 705dad38 authored by Pieter Noordhuis's avatar Pieter Noordhuis
Browse files

make sortCommand aware that lookupKeyByPattern always increased the refcount of the returned value

parent a3f3af86
...@@ -6524,13 +6524,18 @@ static robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) { ...@@ -6524,13 +6524,18 @@ static robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) {
initStaticStringObject(keyobj,((char*)&keyname)+(sizeof(long)*2)); initStaticStringObject(keyobj,((char*)&keyname)+(sizeof(long)*2));
o = lookupKeyRead(db,&keyobj); o = lookupKeyRead(db,&keyobj);
/* Retrieve value from hash by the field name */
if (o != NULL && fieldlen > 0) { if (o != NULL && fieldlen > 0) {
/* Retrieve value from hash by the field name. This operation
* already increases the refcount of the returned object. */
if (o->type != REDIS_HASH || fieldname.len < 1) { if (o->type != REDIS_HASH || fieldname.len < 1) {
return NULL; return NULL;
} }
initStaticStringObject(fieldobj,((char*)&fieldname)+(sizeof(long)*2)); initStaticStringObject(fieldobj,((char*)&fieldname)+(sizeof(long)*2));
o = hashGet(o, &fieldobj); o = hashGet(o, &fieldobj);
} else {
/* Every object that this function returns needs to have its refcount
* increased. sortCommand decreases it again. */
incrRefCount(o);
} }
return o; return o;
...@@ -6701,15 +6706,17 @@ static void sortCommand(redisClient *c) { ...@@ -6701,15 +6706,17 @@ static void sortCommand(redisClient *c) {
if (sortby) { if (sortby) {
/* lookup value to sort by */ /* lookup value to sort by */
byval = lookupKeyByPattern(c->db,sortby,vector[j].obj); byval = lookupKeyByPattern(c->db,sortby,vector[j].obj);
if (!byval || byval->type != REDIS_STRING) continue; if (!byval) continue;
if (byval->type != REDIS_STRING) {
decrRefCount(byval);
continue;
}
} else { } else {
/* use object itself to sort by */ /* use object itself to sort by */
byval = vector[j].obj; byval = vector[j].obj;
} }
if (alpha) { if (alpha) {
/* getDecodedObject increments refcount, so the corresponding
* decrRefCount will clean up values coming from a zipmap. */
vector[j].u.cmpobj = getDecodedObject(byval); vector[j].u.cmpobj = getDecodedObject(byval);
} else { } else {
if (byval->encoding == REDIS_ENCODING_RAW) { if (byval->encoding == REDIS_ENCODING_RAW) {
...@@ -6722,15 +6729,15 @@ static void sortCommand(redisClient *c) { ...@@ -6722,15 +6729,15 @@ static void sortCommand(redisClient *c) {
} else { } else {
redisAssert(1 != 1); redisAssert(1 != 1);
} }
}
/* clean up immediately if this value came from a zipmap */ /* when the object was retrieved using lookupKeyByPattern,
if (byval->refcount == 0) { * its refcount needs to be decreased. */
byval->refcount = 1; if (sortby) {
decrRefCount(byval); decrRefCount(byval);
} }
} }
} }
}
/* We are ready to sort the vector... perform a bit of sanity check /* We are ready to sort the vector... perform a bit of sanity check
* on the LIMIT option too. We'll use a partial version of quicksort. */ * on the LIMIT option too. We'll use a partial version of quicksort. */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment