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 <pierluigi.lenoci@gmail.com>
Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>
This commit is contained in:
Pierluigi Lenoci 2026-05-04 15:54:23 +02:00
parent 2432f55278
commit 1de2c26802
No known key found for this signature in database

View file

@ -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) {