c++credishiredis

Use of \r \n in value passed to `RedisModule_ReplyWithSimpleString` redis


I have a simple module that implements new type in redis storage. This type holds simple string. User can save such string set-myType key value User can get string get-myType key

I faced with problem that if value contains \r or \n in a value I can't reply to a user. It fails with Bad simple string value

I investigated code of redis_module API and found that this error refers to hiredis during such check

for (int i = 0; i < len; i++) {
        if (p[i] == '\r' || p[i] == '\n') {
            __redisReaderSetError(r,REDIS_ERR_PROTOCOL,
                     "Bad simple string value");
        return REDIS_ERR;
    }
}

Redis itself add such symbols to my reply during ReplyWithSimpleString

int RM_ReplyWithSimpleString(RedisModuleCtx *ctx, const char *msg) {
    client *c = moduleGetReplyClient(ctx);
    if (c == NULL) return REDISMODULE_OK;
    addReplyProto(c,"+",1);
    addReplyProto(c,msg,strlen(msg));
    addReplyProto(c,"\r\n",2);  // HERE
    return REDISMODULE_OK;
}

Common redis command get key handles such symbols just fine.

My question: is there any possibility to store value with \n or \r and successfully reply with it to user via ReplyWithSimpleString or any other API.


Solution

  • Since your value might contain '\r' and '\n', you should call RedisModule_ReplyWithStringBuffer. This method returns a Bulk String Reply instead of Simple String Reply, and it's binary safe.