From 1de2c268028d19f201a7d0a6dc701774a62fcb4f Mon Sep 17 00:00:00 2001 From: Pierluigi Lenoci Date: Mon, 4 May 2026 15:54:23 +0200 Subject: [PATCH] feat: add RedisModule_FreeStringSafe() NULL-safe macro Add a convenience macro in redismodule.h that wraps RedisModule_FreeString with a NULL check. This avoids the need for modules to guard every FreeString call with an explicit NULL test. Since it is a macro compiled into the module, it works transparently with any Redis server version (no API registration change needed). Closes #10887 Signed-off-by: Pierluigi Lenoci Signed-off-by: Pierluigi Lenoci --- src/redismodule.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/redismodule.h b/src/redismodule.h index fae09c3fb..e29d949d6 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -1492,6 +1492,13 @@ REDISMODULE_API int (*RedisModule_GetKeyMeta)(RedisModuleKeyMetaClassId id, Redi #define RedisModule_IsAOFClient(id) ((id) == UINT64_MAX) +/* NULL-safe wrapper for RedisModule_FreeString. Modules can use this macro + * to avoid explicit NULL checks before freeing optional strings. + * This macro is compiled into the module, so it works with any Redis version. */ +#define RedisModule_FreeStringSafe(ctx, str) do { \ + if ((str) != NULL) RedisModule_FreeString((ctx), (str)); \ +} while (0) + /* This is included inline inside each Redis module. */ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) REDISMODULE_ATTR_UNUSED; static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) {