diff options
author | sundb <sundbcn@gmail.com> | 2023-05-07 15:13:19 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-07 10:13:19 +0300 |
commit | ce5f4ea3a9f4afe1c43079c93d0ff6bf6b04597a (patch) | |
tree | 7aff01dd477d7c3a66f27148671a339ae51b9668 /src | |
parent | b0dd7b324505ebb3b6fac3f3067eba948cf0b09d (diff) | |
download | redis-ce5f4ea3a9f4afe1c43079c93d0ff6bf6b04597a.tar.gz |
Delete empty key if fails after moduleCreateEmptyKey() in module (#12129)
When `RM_ZsetAdd()`/`RM_ZsetIncrby()`/`RM_StreamAdd()` fails, if a new key happens to
be created using `moduleCreateEmptyKey()`, we should clean up the empty key.
## Test
1) Add new module commands(`zset.add` and `zset.incrby`) to cover `RM_ZsetAdd()`/`RM_ZsetIncrby()`.
2) Add a large-memory test to cover `RM_StreamAdd()`.
Diffstat (limited to 'src')
-rw-r--r-- | src/module.c | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/src/module.c b/src/module.c index 4ceef7cef..f6f6e0e7e 100644 --- a/src/module.c +++ b/src/module.c @@ -4695,6 +4695,7 @@ int RM_ZsetAdd(RedisModuleKey *key, double score, RedisModuleString *ele, int *f if (flagsptr) in_flags = moduleZsetAddFlagsToCoreFlags(*flagsptr); if (zsetAdd(key->value,score,ele->ptr,in_flags,&out_flags,NULL) == 0) { if (flagsptr) *flagsptr = 0; + moduleDelKeyIfEmpty(key); return REDISMODULE_ERR; } if (flagsptr) *flagsptr = moduleZsetAddFlagsFromCoreFlags(out_flags); @@ -4723,6 +4724,7 @@ int RM_ZsetIncrby(RedisModuleKey *key, double score, RedisModuleString *ele, int in_flags |= ZADD_IN_INCR; if (zsetAdd(key->value,score,ele->ptr,in_flags,&out_flags,newscore) == 0) { if (flagsptr) *flagsptr = 0; + moduleDelKeyIfEmpty(key); return REDISMODULE_ERR; } if (flagsptr) *flagsptr = moduleZsetAddFlagsFromCoreFlags(out_flags); @@ -5405,6 +5407,7 @@ int RM_StreamAdd(RedisModuleKey *key, int flags, RedisModuleStreamID *id, RedisM /* Either the ID not greater than all existing IDs in the stream, or * the elements are too large to be stored. either way, errno is already * set by streamAppendItem. */ + if (created) moduleDelKeyIfEmpty(key); return REDISMODULE_ERR; } /* Postponed signalKeyAsReady(). Done implicitly by moduleCreateEmptyKey() |