From d697daa7a5600ca82a0098bc7c857bc7167bb35e Mon Sep 17 00:00:00 2001 From: Wang Yuan Date: Tue, 18 Jan 2022 21:55:20 +0800 Subject: [PATCH] Use const char pointer in redismodule.h as far as possible (#10064) When I used C++ to develop a redis module. i used `string.data()` as the second parameter `ele` of `RedisModule_DigestAddStringBuffer`, but there is a warning, since we never change the `ele`, i think we should use `const char` for it. This PR adds const to just a handful of module APIs that required it, all not very widely used. The implication is a breaking change in terms of compilation error that's easy to resolve, and no ABI impact. The affected APIs are around Digest, Info injection, and Cluster bus messages. --- src/cluster.c | 4 ++-- src/cluster.h | 2 +- src/debug.c | 11 +++++------ src/module.c | 18 +++++++++--------- src/modules/hellocluster.c | 2 +- src/redismodule.h | 18 +++++++++--------- src/server.h | 4 ++-- tests/modules/datatype2.c | 2 +- 8 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 87a965d96..adc1629c8 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -3112,7 +3112,7 @@ void clusterSendUpdate(clusterLink *link, clusterNode *node) { * * If link is NULL, then the message is broadcasted to the whole cluster. */ void clusterSendModule(clusterLink *link, uint64_t module_id, uint8_t type, - unsigned char *payload, uint32_t len) { + const char *payload, uint32_t len) { unsigned char *heapbuf; clusterMsg buf[1]; clusterMsg *hdr = (clusterMsg*) buf; @@ -3151,7 +3151,7 @@ void clusterSendModule(clusterLink *link, uint64_t module_id, uint8_t type, * * The function returns C_OK if the target is valid, otherwise C_ERR is * returned. */ -int clusterSendModuleMessageToTarget(const char *target, uint64_t module_id, uint8_t type, unsigned char *payload, uint32_t len) { +int clusterSendModuleMessageToTarget(const char *target, uint64_t module_id, uint8_t type, const char *payload, uint32_t len) { clusterNode *node = NULL; if (target != NULL) { diff --git a/src/cluster.h b/src/cluster.h index 8b76ed19e..ae63a9d84 100644 --- a/src/cluster.h +++ b/src/cluster.h @@ -350,7 +350,7 @@ void clusterRedirectClient(client *c, clusterNode *n, int hashslot, int error_co void migrateCloseTimedoutSockets(void); int verifyClusterConfigWithData(void); unsigned long getClusterConnectionsCount(void); -int clusterSendModuleMessageToTarget(const char *target, uint64_t module_id, uint8_t type, unsigned char *payload, uint32_t len); +int clusterSendModuleMessageToTarget(const char *target, uint64_t module_id, uint8_t type, const char *payload, uint32_t len); void clusterPropagatePublish(robj *channel, robj *message); void clusterPropagatePublishShard(robj *channel, robj *message); unsigned int keyHashSlot(char *key, int keylen); diff --git a/src/debug.c b/src/debug.c index c8a6fc186..2da2c5d50 100644 --- a/src/debug.c +++ b/src/debug.c @@ -79,13 +79,13 @@ void logStackTrace(void *eip, int uplevel); * "add" digests relative to unordered elements. * * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */ -void xorDigest(unsigned char *digest, void *ptr, size_t len) { +void xorDigest(unsigned char *digest, const void *ptr, size_t len) { SHA1_CTX ctx; - unsigned char hash[20], *s = ptr; + unsigned char hash[20]; int j; SHA1Init(&ctx); - SHA1Update(&ctx,s,len); + SHA1Update(&ctx,ptr,len); SHA1Final(hash,&ctx); for (j = 0; j < 20; j++) @@ -112,11 +112,10 @@ void xorStringObjectDigest(unsigned char *digest, robj *o) { * Also note that mixdigest("foo") followed by mixdigest("bar") * will lead to a different digest compared to "fo", "obar". */ -void mixDigest(unsigned char *digest, void *ptr, size_t len) { +void mixDigest(unsigned char *digest, const void *ptr, size_t len) { SHA1_CTX ctx; - char *s = ptr; - xorDigest(digest,s,len); + xorDigest(digest,ptr,len); SHA1Init(&ctx); SHA1Update(&ctx,digest,20); SHA1Final(digest,&ctx); diff --git a/src/module.c b/src/module.c index 30179eb2c..be4c549c8 100644 --- a/src/module.c +++ b/src/module.c @@ -5816,7 +5816,7 @@ ssize_t rdbSaveModulesAux(rio *rdb, int when) { * EndSequence(); * */ -void RM_DigestAddStringBuffer(RedisModuleDigest *md, unsigned char *ele, size_t len) { +void RM_DigestAddStringBuffer(RedisModuleDigest *md, const char *ele, size_t len) { mixDigest(md->o,ele,len); } @@ -6985,7 +6985,7 @@ void RM_RegisterClusterMessageReceiver(RedisModuleCtx *ctx, uint8_t type, RedisM * The function returns REDISMODULE_OK if the message was successfully sent, * otherwise if the node is not connected or such node ID does not map to any * known cluster node, REDISMODULE_ERR is returned. */ -int RM_SendClusterMessage(RedisModuleCtx *ctx, char *target_id, uint8_t type, unsigned char *msg, uint32_t len) { +int RM_SendClusterMessage(RedisModuleCtx *ctx, const char *target_id, uint8_t type, const char *msg, uint32_t len) { if (!server.cluster_enabled) return REDISMODULE_ERR; uint64_t module_id = moduleTypeEncodeId(ctx->module->name,0); if (clusterSendModuleMessageToTarget(target_id,module_id,type,msg,len) == C_OK) @@ -8112,7 +8112,7 @@ int RM_InfoEndDictField(RedisModuleInfoCtx *ctx); * be prefixed by `_` and must only include A-Z,a-z,0-9. * NULL or empty string indicates the default section (only ``) is used. * When return value is REDISMODULE_ERR, the section should and will be skipped. */ -int RM_InfoAddSection(RedisModuleInfoCtx *ctx, char *name) { +int RM_InfoAddSection(RedisModuleInfoCtx *ctx, const char *name) { sds full_name = sdsdup(ctx->module->name); if (name != NULL && strlen(name) > 0) full_name = sdscatfmt(full_name, "_%s", name); @@ -8143,7 +8143,7 @@ int RM_InfoAddSection(RedisModuleInfoCtx *ctx, char *name) { /* Starts a dict field, similar to the ones in INFO KEYSPACE. Use normal * RedisModule_InfoAddField* functions to add the items to this field, and * terminate with RedisModule_InfoEndDictField. */ -int RM_InfoBeginDictField(RedisModuleInfoCtx *ctx, char *name) { +int RM_InfoBeginDictField(RedisModuleInfoCtx *ctx, const char *name) { if (!ctx->in_section) return REDISMODULE_ERR; /* Implicitly end dicts, instead of returning an error which is likely un checked. */ @@ -8175,7 +8175,7 @@ int RM_InfoEndDictField(RedisModuleInfoCtx *ctx) { /* Used by RedisModuleInfoFunc to add info fields. * Each field will be automatically prefixed by `_`. * Field names or values must not include `\r\n` or `:`. */ -int RM_InfoAddFieldString(RedisModuleInfoCtx *ctx, char *field, RedisModuleString *value) { +int RM_InfoAddFieldString(RedisModuleInfoCtx *ctx, const char *field, RedisModuleString *value) { if (!ctx->in_section) return REDISMODULE_ERR; if (ctx->in_dict_field) { @@ -8194,7 +8194,7 @@ int RM_InfoAddFieldString(RedisModuleInfoCtx *ctx, char *field, RedisModuleStrin } /* See RedisModule_InfoAddFieldString(). */ -int RM_InfoAddFieldCString(RedisModuleInfoCtx *ctx, char *field, char *value) { +int RM_InfoAddFieldCString(RedisModuleInfoCtx *ctx, const char *field, const char *value) { if (!ctx->in_section) return REDISMODULE_ERR; if (ctx->in_dict_field) { @@ -8213,7 +8213,7 @@ int RM_InfoAddFieldCString(RedisModuleInfoCtx *ctx, char *field, char *value) { } /* See RedisModule_InfoAddFieldString(). */ -int RM_InfoAddFieldDouble(RedisModuleInfoCtx *ctx, char *field, double value) { +int RM_InfoAddFieldDouble(RedisModuleInfoCtx *ctx, const char *field, double value) { if (!ctx->in_section) return REDISMODULE_ERR; if (ctx->in_dict_field) { @@ -8232,7 +8232,7 @@ int RM_InfoAddFieldDouble(RedisModuleInfoCtx *ctx, char *field, double value) { } /* See RedisModule_InfoAddFieldString(). */ -int RM_InfoAddFieldLongLong(RedisModuleInfoCtx *ctx, char *field, long long value) { +int RM_InfoAddFieldLongLong(RedisModuleInfoCtx *ctx, const char *field, long long value) { if (!ctx->in_section) return REDISMODULE_ERR; if (ctx->in_dict_field) { @@ -8251,7 +8251,7 @@ int RM_InfoAddFieldLongLong(RedisModuleInfoCtx *ctx, char *field, long long valu } /* See RedisModule_InfoAddFieldString(). */ -int RM_InfoAddFieldULongLong(RedisModuleInfoCtx *ctx, char *field, unsigned long long value) { +int RM_InfoAddFieldULongLong(RedisModuleInfoCtx *ctx, const char *field, unsigned long long value) { if (!ctx->in_section) return REDISMODULE_ERR; if (ctx->in_dict_field) { diff --git a/src/modules/hellocluster.c b/src/modules/hellocluster.c index ac04f9560..8f822e31e 100644 --- a/src/modules/hellocluster.c +++ b/src/modules/hellocluster.c @@ -45,7 +45,7 @@ int PingallCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, i REDISMODULE_NOT_USED(argv); REDISMODULE_NOT_USED(argc); - RedisModule_SendClusterMessage(ctx,NULL,MSGTYPE_PING,(unsigned char*)"Hey",3); + RedisModule_SendClusterMessage(ctx,NULL,MSGTYPE_PING,"Hey",3); return RedisModule_ReplyWithSimpleString(ctx, "OK"); } diff --git a/src/redismodule.h b/src/redismodule.h index a31ff376a..7136bbf6e 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -853,7 +853,7 @@ REDISMODULE_API const RedisModuleString * (*RedisModule_GetKeyNameFromOptCtx)(Re REDISMODULE_API const RedisModuleString * (*RedisModule_GetToKeyNameFromOptCtx)(RedisModuleKeyOptCtx *ctx) REDISMODULE_ATTR; REDISMODULE_API long long (*RedisModule_Milliseconds)(void) REDISMODULE_ATTR; REDISMODULE_API uint64_t (*RedisModule_MonotonicMicroseconds)(void) REDISMODULE_ATTR; -REDISMODULE_API void (*RedisModule_DigestAddStringBuffer)(RedisModuleDigest *md, unsigned char *ele, size_t len) REDISMODULE_ATTR; +REDISMODULE_API void (*RedisModule_DigestAddStringBuffer)(RedisModuleDigest *md, const char *ele, size_t len) REDISMODULE_ATTR; REDISMODULE_API void (*RedisModule_DigestAddLongLong)(RedisModuleDigest *md, long long ele) REDISMODULE_ATTR; REDISMODULE_API void (*RedisModule_DigestEndSequence)(RedisModuleDigest *md) REDISMODULE_ATTR; REDISMODULE_API int (*RedisModule_GetDbIdFromDigest)(RedisModuleDigest *dig) REDISMODULE_ATTR; @@ -881,14 +881,14 @@ REDISMODULE_API RedisModuleString * (*RedisModule_DictPrev)(RedisModuleCtx *ctx, REDISMODULE_API int (*RedisModule_DictCompareC)(RedisModuleDictIter *di, const char *op, void *key, size_t keylen) REDISMODULE_ATTR; REDISMODULE_API int (*RedisModule_DictCompare)(RedisModuleDictIter *di, const char *op, RedisModuleString *key) REDISMODULE_ATTR; REDISMODULE_API int (*RedisModule_RegisterInfoFunc)(RedisModuleCtx *ctx, RedisModuleInfoFunc cb) REDISMODULE_ATTR; -REDISMODULE_API int (*RedisModule_InfoAddSection)(RedisModuleInfoCtx *ctx, char *name) REDISMODULE_ATTR; -REDISMODULE_API int (*RedisModule_InfoBeginDictField)(RedisModuleInfoCtx *ctx, char *name) REDISMODULE_ATTR; +REDISMODULE_API int (*RedisModule_InfoAddSection)(RedisModuleInfoCtx *ctx, const char *name) REDISMODULE_ATTR; +REDISMODULE_API int (*RedisModule_InfoBeginDictField)(RedisModuleInfoCtx *ctx, const char *name) REDISMODULE_ATTR; REDISMODULE_API int (*RedisModule_InfoEndDictField)(RedisModuleInfoCtx *ctx) REDISMODULE_ATTR; -REDISMODULE_API int (*RedisModule_InfoAddFieldString)(RedisModuleInfoCtx *ctx, char *field, RedisModuleString *value) REDISMODULE_ATTR; -REDISMODULE_API int (*RedisModule_InfoAddFieldCString)(RedisModuleInfoCtx *ctx, char *field, char *value) REDISMODULE_ATTR; -REDISMODULE_API int (*RedisModule_InfoAddFieldDouble)(RedisModuleInfoCtx *ctx, char *field, double value) REDISMODULE_ATTR; -REDISMODULE_API int (*RedisModule_InfoAddFieldLongLong)(RedisModuleInfoCtx *ctx, char *field, long long value) REDISMODULE_ATTR; -REDISMODULE_API int (*RedisModule_InfoAddFieldULongLong)(RedisModuleInfoCtx *ctx, char *field, unsigned long long value) REDISMODULE_ATTR; +REDISMODULE_API int (*RedisModule_InfoAddFieldString)(RedisModuleInfoCtx *ctx, const char *field, RedisModuleString *value) REDISMODULE_ATTR; +REDISMODULE_API int (*RedisModule_InfoAddFieldCString)(RedisModuleInfoCtx *ctx, const char *field,const char *value) REDISMODULE_ATTR; +REDISMODULE_API int (*RedisModule_InfoAddFieldDouble)(RedisModuleInfoCtx *ctx, const char *field, double value) REDISMODULE_ATTR; +REDISMODULE_API int (*RedisModule_InfoAddFieldLongLong)(RedisModuleInfoCtx *ctx, const char *field, long long value) REDISMODULE_ATTR; +REDISMODULE_API int (*RedisModule_InfoAddFieldULongLong)(RedisModuleInfoCtx *ctx, const char *field, unsigned long long value) REDISMODULE_ATTR; REDISMODULE_API RedisModuleServerInfoData * (*RedisModule_GetServerInfo)(RedisModuleCtx *ctx, const char *section) REDISMODULE_ATTR; REDISMODULE_API void (*RedisModule_FreeServerInfo)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data) REDISMODULE_ATTR; REDISMODULE_API RedisModuleString * (*RedisModule_ServerInfoGetField)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data, const char* field) REDISMODULE_ATTR; @@ -940,7 +940,7 @@ REDISMODULE_API int (*RedisModule_NotifyKeyspaceEvent)(RedisModuleCtx *ctx, int REDISMODULE_API int (*RedisModule_GetNotifyKeyspaceEvents)() REDISMODULE_ATTR; REDISMODULE_API int (*RedisModule_BlockedClientDisconnected)(RedisModuleCtx *ctx) REDISMODULE_ATTR; REDISMODULE_API void (*RedisModule_RegisterClusterMessageReceiver)(RedisModuleCtx *ctx, uint8_t type, RedisModuleClusterMessageReceiver callback) REDISMODULE_ATTR; -REDISMODULE_API int (*RedisModule_SendClusterMessage)(RedisModuleCtx *ctx, char *target_id, uint8_t type, unsigned char *msg, uint32_t len) REDISMODULE_ATTR; +REDISMODULE_API int (*RedisModule_SendClusterMessage)(RedisModuleCtx *ctx, const char *target_id, uint8_t type, const char *msg, uint32_t len) REDISMODULE_ATTR; REDISMODULE_API int (*RedisModule_GetClusterNodeInfo)(RedisModuleCtx *ctx, const char *id, char *ip, char *master_id, int *port, int *flags) REDISMODULE_ATTR; REDISMODULE_API char ** (*RedisModule_GetClusterNodesList)(RedisModuleCtx *ctx, size_t *numnodes) REDISMODULE_ATTR; REDISMODULE_API void (*RedisModule_FreeClusterNodesList)(char **ids) REDISMODULE_ATTR; diff --git a/src/server.h b/src/server.h index 8c8cfcf9b..3978201a7 100644 --- a/src/server.h +++ b/src/server.h @@ -3367,8 +3367,8 @@ void applyWatchdogPeriod(); void watchdogScheduleSignal(int period); void serverLogHexDump(int level, char *descr, void *value, size_t len); int memtest_preserving_test(unsigned long *m, size_t bytes, int passes); -void mixDigest(unsigned char *digest, void *ptr, size_t len); -void xorDigest(unsigned char *digest, void *ptr, size_t len); +void mixDigest(unsigned char *digest, const void *ptr, size_t len); +void xorDigest(unsigned char *digest, const void *ptr, size_t len); int populateSingleCommand(struct redisCommand *c, char *strflags); void commandAddSubcommand(struct redisCommand *parent, struct redisCommand *subcommand); void populateCommandMovableKeys(struct redisCommand *cmd); diff --git a/tests/modules/datatype2.c b/tests/modules/datatype2.c index 8ab1b2629..bc0dc3dfe 100644 --- a/tests/modules/datatype2.c +++ b/tests/modules/datatype2.c @@ -627,7 +627,7 @@ void MemAllocDigest(RedisModuleDigest *md, void *value) { struct MemBlock *block = mem; while (block) { - RedisModule_DigestAddStringBuffer(md, (unsigned char *)block->block, BLOCK_SIZE); + RedisModule_DigestAddStringBuffer(md, (const char *)block->block, BLOCK_SIZE); block = block->next; } }