From 063de675e0e20a0d060ffc6f7f5f7b074043344a Mon Sep 17 00:00:00 2001 From: Binbin Date: Thu, 15 Feb 2024 16:49:10 +0800 Subject: [PATCH] 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. --- src/t_zset.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/t_zset.c b/src/t_zset.c index eb1477457..19a8c865b 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -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) {