zunionInterDiffGenericCommand use ztrycalloc to avoid OOM panic (#13052)

In low memory situations, sending a big number of arguments (sets)
may cause OOM panic. Use ztrycalloc, like we do on LCS and XAUTOCLAIM,
and fail gracefully.

This change affects the following commands: ZUNION, ZINTER, ZDIFF,
ZUNIONSTORE, ZINTERSTORE, ZDIFFSTORE, ZINTERCARD.
This commit is contained in:
Binbin 2024-02-15 16:49:10 +08:00 committed by GitHub
parent 32f44da510
commit 063de675e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 1 deletions

View File

@ -2663,8 +2663,14 @@ void zunionInterDiffGenericCommand(client *c, robj *dstkey, int numkeysIndex, in
return;
}
/* Try to allocate the src table, and abort on insufficient memory. */
src = ztrycalloc(sizeof(zsetopsrc) * setnum);
if (src == NULL) {
addReplyError(c, "Insufficient memory, failed allocating transient memory, too many args.");
return;
}
/* read keys to be used for input */
src = zcalloc(sizeof(zsetopsrc) * setnum);
for (i = 0, j = numkeysIndex+1; i < setnum; i++, j++) {
robj *obj = lookupKeyRead(c->db, c->argv[j]);
if (obj != NULL) {