Replace usage of wrongtypeerr with helper (#7633)
* Replace usage of wrongtypeerr with helper
This commit is contained in:
parent
ddcbb628a1
commit
f2db379fa3
@ -480,12 +480,12 @@ int getBitfieldTypeFromArgument(client *c, robj *o, int *sign, int *bits) {
|
|||||||
robj *lookupStringForBitCommand(client *c, size_t maxbit) {
|
robj *lookupStringForBitCommand(client *c, size_t maxbit) {
|
||||||
size_t byte = maxbit >> 3;
|
size_t byte = maxbit >> 3;
|
||||||
robj *o = lookupKeyWrite(c->db,c->argv[1]);
|
robj *o = lookupKeyWrite(c->db,c->argv[1]);
|
||||||
|
if (checkType(c,o,OBJ_STRING)) return NULL;
|
||||||
|
|
||||||
if (o == NULL) {
|
if (o == NULL) {
|
||||||
o = createObject(OBJ_STRING,sdsnewlen(NULL, byte+1));
|
o = createObject(OBJ_STRING,sdsnewlen(NULL, byte+1));
|
||||||
dbAdd(c->db,c->argv[1],o);
|
dbAdd(c->db,c->argv[1],o);
|
||||||
} else {
|
} else {
|
||||||
if (checkType(c,o,OBJ_STRING)) return NULL;
|
|
||||||
o = dbUnshareStringValue(c->db,c->argv[1],o);
|
o = dbUnshareStringValue(c->db,c->argv[1],o);
|
||||||
o->ptr = sdsgrowzero(o->ptr,byte+1);
|
o->ptr = sdsgrowzero(o->ptr,byte+1);
|
||||||
}
|
}
|
||||||
|
@ -702,7 +702,7 @@ void geohashCommand(client *c) {
|
|||||||
|
|
||||||
/* Look up the requested zset */
|
/* Look up the requested zset */
|
||||||
robj *zobj = lookupKeyRead(c->db, c->argv[1]);
|
robj *zobj = lookupKeyRead(c->db, c->argv[1]);
|
||||||
if (zobj && checkType(c, zobj, OBJ_ZSET)) return;
|
if (checkType(c, zobj, OBJ_ZSET)) return;
|
||||||
|
|
||||||
/* Geohash elements one after the other, using a null bulk reply for
|
/* Geohash elements one after the other, using a null bulk reply for
|
||||||
* missing elements. */
|
* missing elements. */
|
||||||
@ -763,7 +763,7 @@ void geoposCommand(client *c) {
|
|||||||
|
|
||||||
/* Look up the requested zset */
|
/* Look up the requested zset */
|
||||||
robj *zobj = lookupKeyRead(c->db, c->argv[1]);
|
robj *zobj = lookupKeyRead(c->db, c->argv[1]);
|
||||||
if (zobj && checkType(c, zobj, OBJ_ZSET)) return;
|
if (checkType(c, zobj, OBJ_ZSET)) return;
|
||||||
|
|
||||||
/* Report elements one after the other, using a null bulk reply for
|
/* Report elements one after the other, using a null bulk reply for
|
||||||
* missing elements. */
|
* missing elements. */
|
||||||
|
@ -402,7 +402,8 @@ robj *resetRefCount(robj *obj) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int checkType(client *c, robj *o, int type) {
|
int checkType(client *c, robj *o, int type) {
|
||||||
if (o->type != type) {
|
/* A NULL is considered an empty key */
|
||||||
|
if (o && o->type != type) {
|
||||||
addReply(c,shared.wrongtypeerr);
|
addReply(c,shared.wrongtypeerr);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
12
src/t_hash.c
12
src/t_hash.c
@ -450,14 +450,11 @@ sds hashTypeCurrentObjectNewSds(hashTypeIterator *hi, int what) {
|
|||||||
|
|
||||||
robj *hashTypeLookupWriteOrCreate(client *c, robj *key) {
|
robj *hashTypeLookupWriteOrCreate(client *c, robj *key) {
|
||||||
robj *o = lookupKeyWrite(c->db,key);
|
robj *o = lookupKeyWrite(c->db,key);
|
||||||
|
if (checkType(c,o,OBJ_HASH)) return NULL;
|
||||||
|
|
||||||
if (o == NULL) {
|
if (o == NULL) {
|
||||||
o = createHashObject();
|
o = createHashObject();
|
||||||
dbAdd(c->db,key,o);
|
dbAdd(c->db,key,o);
|
||||||
} else {
|
|
||||||
if (o->type != OBJ_HASH) {
|
|
||||||
addReply(c,shared.wrongtypeerr);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
@ -692,10 +689,7 @@ void hmgetCommand(client *c) {
|
|||||||
/* Don't abort when the key cannot be found. Non-existing keys are empty
|
/* Don't abort when the key cannot be found. Non-existing keys are empty
|
||||||
* hashes, where HMGET should respond with a series of null bulks. */
|
* hashes, where HMGET should respond with a series of null bulks. */
|
||||||
o = lookupKeyRead(c->db, c->argv[1]);
|
o = lookupKeyRead(c->db, c->argv[1]);
|
||||||
if (o != NULL && o->type != OBJ_HASH) {
|
if (checkType(c,o,OBJ_HASH)) return;
|
||||||
addReply(c, shared.wrongtypeerr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
addReplyArrayLen(c, c->argc-2);
|
addReplyArrayLen(c, c->argc-2);
|
||||||
for (i = 2; i < c->argc; i++) {
|
for (i = 2; i < c->argc; i++) {
|
||||||
|
13
src/t_list.c
13
src/t_list.c
@ -198,8 +198,7 @@ void pushGenericCommand(client *c, int where) {
|
|||||||
int j, pushed = 0;
|
int j, pushed = 0;
|
||||||
robj *lobj = lookupKeyWrite(c->db,c->argv[1]);
|
robj *lobj = lookupKeyWrite(c->db,c->argv[1]);
|
||||||
|
|
||||||
if (lobj && lobj->type != OBJ_LIST) {
|
if (checkType(c,lobj,OBJ_LIST)) {
|
||||||
addReply(c,shared.wrongtypeerr);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -691,7 +690,7 @@ void rpoplpushCommand(client *c) {
|
|||||||
robj *dobj = lookupKeyWrite(c->db,c->argv[2]);
|
robj *dobj = lookupKeyWrite(c->db,c->argv[2]);
|
||||||
robj *touchedkey = c->argv[1];
|
robj *touchedkey = c->argv[1];
|
||||||
|
|
||||||
if (dobj && checkType(c,dobj,OBJ_LIST)) return;
|
if (checkType(c,dobj,OBJ_LIST)) return;
|
||||||
value = listTypePop(sobj,LIST_TAIL);
|
value = listTypePop(sobj,LIST_TAIL);
|
||||||
/* We saved touched key, and protect it, since rpoplpushHandlePush
|
/* We saved touched key, and protect it, since rpoplpushHandlePush
|
||||||
* may change the client command argument vector (it does not
|
* may change the client command argument vector (it does not
|
||||||
@ -803,8 +802,7 @@ void blockingPopGenericCommand(client *c, int where) {
|
|||||||
for (j = 1; j < c->argc-1; j++) {
|
for (j = 1; j < c->argc-1; j++) {
|
||||||
o = lookupKeyWrite(c->db,c->argv[j]);
|
o = lookupKeyWrite(c->db,c->argv[j]);
|
||||||
if (o != NULL) {
|
if (o != NULL) {
|
||||||
if (o->type != OBJ_LIST) {
|
if (checkType(c,o,OBJ_LIST)) {
|
||||||
addReply(c,shared.wrongtypeerr);
|
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
if (listTypeLength(o) != 0) {
|
if (listTypeLength(o) != 0) {
|
||||||
@ -863,6 +861,7 @@ void brpoplpushCommand(client *c) {
|
|||||||
!= C_OK) return;
|
!= C_OK) return;
|
||||||
|
|
||||||
robj *key = lookupKeyWrite(c->db, c->argv[1]);
|
robj *key = lookupKeyWrite(c->db, c->argv[1]);
|
||||||
|
if (checkType(c,key,OBJ_LIST)) return;
|
||||||
|
|
||||||
if (key == NULL) {
|
if (key == NULL) {
|
||||||
if (c->flags & CLIENT_MULTI) {
|
if (c->flags & CLIENT_MULTI) {
|
||||||
@ -873,9 +872,6 @@ void brpoplpushCommand(client *c) {
|
|||||||
/* The list is empty and the client blocks. */
|
/* The list is empty and the client blocks. */
|
||||||
blockForKeys(c,BLOCKED_LIST,c->argv + 1,1,timeout,c->argv[2],NULL);
|
blockForKeys(c,BLOCKED_LIST,c->argv + 1,1,timeout,c->argv[2],NULL);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (key->type != OBJ_LIST) {
|
|
||||||
addReply(c, shared.wrongtypeerr);
|
|
||||||
} else {
|
} else {
|
||||||
/* The list exists and has elements, so
|
/* The list exists and has elements, so
|
||||||
* the regular rpoplpushCommand is executed. */
|
* the regular rpoplpushCommand is executed. */
|
||||||
@ -883,4 +879,3 @@ void brpoplpushCommand(client *c) {
|
|||||||
rpoplpushCommand(c);
|
rpoplpushCommand(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@ -266,14 +266,11 @@ void saddCommand(client *c) {
|
|||||||
int j, added = 0;
|
int j, added = 0;
|
||||||
|
|
||||||
set = lookupKeyWrite(c->db,c->argv[1]);
|
set = lookupKeyWrite(c->db,c->argv[1]);
|
||||||
|
if (checkType(c,set,OBJ_SET)) return;
|
||||||
|
|
||||||
if (set == NULL) {
|
if (set == NULL) {
|
||||||
set = setTypeCreate(c->argv[2]->ptr);
|
set = setTypeCreate(c->argv[2]->ptr);
|
||||||
dbAdd(c->db,c->argv[1],set);
|
dbAdd(c->db,c->argv[1],set);
|
||||||
} else {
|
|
||||||
if (set->type != OBJ_SET) {
|
|
||||||
addReply(c,shared.wrongtypeerr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 2; j < c->argc; j++) {
|
for (j = 2; j < c->argc; j++) {
|
||||||
@ -330,7 +327,7 @@ void smoveCommand(client *c) {
|
|||||||
/* If the source key has the wrong type, or the destination key
|
/* If the source key has the wrong type, or the destination key
|
||||||
* is set and has the wrong type, return with an error. */
|
* is set and has the wrong type, return with an error. */
|
||||||
if (checkType(c,srcset,OBJ_SET) ||
|
if (checkType(c,srcset,OBJ_SET) ||
|
||||||
(dstset && checkType(c,dstset,OBJ_SET))) return;
|
checkType(c,dstset,OBJ_SET)) return;
|
||||||
|
|
||||||
/* If srcset and dstset are equal, SMOVE is a no-op */
|
/* If srcset and dstset are equal, SMOVE is a no-op */
|
||||||
if (srcset == dstset) {
|
if (srcset == dstset) {
|
||||||
|
@ -1108,14 +1108,10 @@ size_t streamReplyWithRangeFromConsumerPEL(client *c, stream *s, streamID *start
|
|||||||
* The function creates a key setting it to an empty stream if needed. */
|
* The function creates a key setting it to an empty stream if needed. */
|
||||||
robj *streamTypeLookupWriteOrCreate(client *c, robj *key) {
|
robj *streamTypeLookupWriteOrCreate(client *c, robj *key) {
|
||||||
robj *o = lookupKeyWrite(c->db,key);
|
robj *o = lookupKeyWrite(c->db,key);
|
||||||
|
if (checkType(c,o,OBJ_STREAM)) return NULL;
|
||||||
if (o == NULL) {
|
if (o == NULL) {
|
||||||
o = createStreamObject();
|
o = createStreamObject();
|
||||||
dbAdd(c->db,key,o);
|
dbAdd(c->db,key,o);
|
||||||
} else {
|
|
||||||
if (o->type != OBJ_STREAM) {
|
|
||||||
addReply(c,shared.wrongtypeerr);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
@ -1460,7 +1456,7 @@ void xreadCommand(client *c) {
|
|||||||
int id_idx = i - streams_arg - streams_count;
|
int id_idx = i - streams_arg - streams_count;
|
||||||
robj *key = c->argv[i-streams_count];
|
robj *key = c->argv[i-streams_count];
|
||||||
robj *o = lookupKeyRead(c->db,key);
|
robj *o = lookupKeyRead(c->db,key);
|
||||||
if (o && checkType(c,o,OBJ_STREAM)) goto cleanup;
|
if (checkType(c,o,OBJ_STREAM)) goto cleanup;
|
||||||
streamCG *group = NULL;
|
streamCG *group = NULL;
|
||||||
|
|
||||||
/* If a group was specified, than we need to be sure that the
|
/* If a group was specified, than we need to be sure that the
|
||||||
@ -2022,7 +2018,7 @@ void xpendingCommand(client *c) {
|
|||||||
robj *o = lookupKeyRead(c->db,c->argv[1]);
|
robj *o = lookupKeyRead(c->db,c->argv[1]);
|
||||||
streamCG *group;
|
streamCG *group;
|
||||||
|
|
||||||
if (o && checkType(c,o,OBJ_STREAM)) return;
|
if (checkType(c,o,OBJ_STREAM)) return;
|
||||||
if (o == NULL ||
|
if (o == NULL ||
|
||||||
(group = streamLookupCG(o->ptr,groupname->ptr)) == NULL)
|
(group = streamLookupCG(o->ptr,groupname->ptr)) == NULL)
|
||||||
{
|
{
|
||||||
|
@ -167,14 +167,13 @@ int getGenericCommand(client *c) {
|
|||||||
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp])) == NULL)
|
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp])) == NULL)
|
||||||
return C_OK;
|
return C_OK;
|
||||||
|
|
||||||
if (o->type != OBJ_STRING) {
|
if (checkType(c,o,OBJ_STRING)) {
|
||||||
addReply(c,shared.wrongtypeerr);
|
|
||||||
return C_ERR;
|
return C_ERR;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
addReplyBulk(c,o);
|
addReplyBulk(c,o);
|
||||||
return C_OK;
|
return C_OK;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void getCommand(client *c) {
|
void getCommand(client *c) {
|
||||||
getGenericCommand(c);
|
getGenericCommand(c);
|
||||||
@ -348,7 +347,7 @@ void incrDecrCommand(client *c, long long incr) {
|
|||||||
robj *o, *new;
|
robj *o, *new;
|
||||||
|
|
||||||
o = lookupKeyWrite(c->db,c->argv[1]);
|
o = lookupKeyWrite(c->db,c->argv[1]);
|
||||||
if (o != NULL && checkType(c,o,OBJ_STRING)) return;
|
if (checkType(c,o,OBJ_STRING)) return;
|
||||||
if (getLongLongFromObjectOrReply(c,o,&value,NULL) != C_OK) return;
|
if (getLongLongFromObjectOrReply(c,o,&value,NULL) != C_OK) return;
|
||||||
|
|
||||||
oldvalue = value;
|
oldvalue = value;
|
||||||
@ -408,7 +407,7 @@ void incrbyfloatCommand(client *c) {
|
|||||||
robj *o, *new, *aux1, *aux2;
|
robj *o, *new, *aux1, *aux2;
|
||||||
|
|
||||||
o = lookupKeyWrite(c->db,c->argv[1]);
|
o = lookupKeyWrite(c->db,c->argv[1]);
|
||||||
if (o != NULL && checkType(c,o,OBJ_STRING)) return;
|
if (checkType(c,o,OBJ_STRING)) return;
|
||||||
if (getLongDoubleFromObjectOrReply(c,o,&value,NULL) != C_OK ||
|
if (getLongDoubleFromObjectOrReply(c,o,&value,NULL) != C_OK ||
|
||||||
getLongDoubleFromObjectOrReply(c,c->argv[2],&incr,NULL) != C_OK)
|
getLongDoubleFromObjectOrReply(c,c->argv[2],&incr,NULL) != C_OK)
|
||||||
return;
|
return;
|
||||||
|
14
src/t_zset.c
14
src/t_zset.c
@ -1598,6 +1598,7 @@ void zaddGenericCommand(client *c, int flags) {
|
|||||||
|
|
||||||
/* Lookup the key and create the sorted set if does not exist. */
|
/* Lookup the key and create the sorted set if does not exist. */
|
||||||
zobj = lookupKeyWrite(c->db,key);
|
zobj = lookupKeyWrite(c->db,key);
|
||||||
|
if (checkType(c,zobj,OBJ_ZSET)) goto cleanup;
|
||||||
if (zobj == NULL) {
|
if (zobj == NULL) {
|
||||||
if (xx) goto reply_to_client; /* No key + XX option: nothing to do. */
|
if (xx) goto reply_to_client; /* No key + XX option: nothing to do. */
|
||||||
if (server.zset_max_ziplist_entries == 0 ||
|
if (server.zset_max_ziplist_entries == 0 ||
|
||||||
@ -1608,11 +1609,6 @@ void zaddGenericCommand(client *c, int flags) {
|
|||||||
zobj = createZsetZiplistObject();
|
zobj = createZsetZiplistObject();
|
||||||
}
|
}
|
||||||
dbAdd(c->db,key,zobj);
|
dbAdd(c->db,key,zobj);
|
||||||
} else {
|
|
||||||
if (zobj->type != OBJ_ZSET) {
|
|
||||||
addReply(c,shared.wrongtypeerr);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < elements; j++) {
|
for (j = 0; j < elements; j++) {
|
||||||
@ -3087,7 +3083,7 @@ void zmscoreCommand(client *c) {
|
|||||||
robj *zobj;
|
robj *zobj;
|
||||||
double score;
|
double score;
|
||||||
zobj = lookupKeyRead(c->db,key);
|
zobj = lookupKeyRead(c->db,key);
|
||||||
if (zobj != NULL && checkType(c,zobj,OBJ_ZSET)) return;
|
if (checkType(c,zobj,OBJ_ZSET)) return;
|
||||||
|
|
||||||
addReplyArrayLen(c,c->argc - 2);
|
addReplyArrayLen(c,c->argc - 2);
|
||||||
for (int j = 2; j < c->argc; j++) {
|
for (int j = 2; j < c->argc; j++) {
|
||||||
@ -3280,11 +3276,8 @@ void blockingGenericZpopCommand(client *c, int where) {
|
|||||||
|
|
||||||
for (j = 1; j < c->argc-1; j++) {
|
for (j = 1; j < c->argc-1; j++) {
|
||||||
o = lookupKeyWrite(c->db,c->argv[j]);
|
o = lookupKeyWrite(c->db,c->argv[j]);
|
||||||
|
if (checkType(c,o,OBJ_ZSET)) return;
|
||||||
if (o != NULL) {
|
if (o != NULL) {
|
||||||
if (o->type != OBJ_ZSET) {
|
|
||||||
addReply(c,shared.wrongtypeerr);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
if (zsetLength(o) != 0) {
|
if (zsetLength(o) != 0) {
|
||||||
/* Non empty zset, this is like a normal ZPOP[MIN|MAX]. */
|
/* Non empty zset, this is like a normal ZPOP[MIN|MAX]. */
|
||||||
genericZpopCommand(c,&c->argv[j],1,where,1,NULL);
|
genericZpopCommand(c,&c->argv[j],1,where,1,NULL);
|
||||||
@ -3296,7 +3289,6 @@ void blockingGenericZpopCommand(client *c, int where) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* If we are inside a MULTI/EXEC and the zset is empty the only thing
|
/* If we are inside a MULTI/EXEC and the zset is empty the only thing
|
||||||
* we can do is treating it as a timeout (even with timeout 0). */
|
* we can do is treating it as a timeout (even with timeout 0). */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user