From 5c9f49e2123c5df7148939a70b80cd72e4e59646 Mon Sep 17 00:00:00 2001 From: Michael Grunder Date: Tue, 19 May 2020 12:56:02 -0700 Subject: Resp3 verbatim string support (#805) Pull RESP3 verbatim string handling from Redis Fixes #802 --- hiredis.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'hiredis.c') diff --git a/hiredis.c b/hiredis.c index 8e438f2..deb65a9 100644 --- a/hiredis.c +++ b/hiredis.c @@ -107,6 +107,7 @@ void freeReplyObject(void *reply) { case REDIS_REPLY_STATUS: case REDIS_REPLY_STRING: case REDIS_REPLY_DOUBLE: + case REDIS_REPLY_VERB: free(r->str); break; } @@ -121,21 +122,34 @@ static void *createStringObject(const redisReadTask *task, char *str, size_t len if (r == NULL) return NULL; - buf = malloc(len+1); - if (buf == NULL) { - freeReplyObject(r); - return NULL; - } - assert(task->type == REDIS_REPLY_ERROR || task->type == REDIS_REPLY_STATUS || - task->type == REDIS_REPLY_STRING); + task->type == REDIS_REPLY_STRING || + task->type == REDIS_REPLY_VERB); /* Copy string value */ - memcpy(buf,str,len); - buf[len] = '\0'; + if (task->type == REDIS_REPLY_VERB) { + buf = malloc(len-4+1); /* Skip 4 bytes of verbatim type header. */ + if (buf == NULL) { + freeReplyObject(r); + return NULL; + } + memcpy(r->vtype,str,3); + r->vtype[3] = '\0'; + memcpy(buf,str+4,len-4); + buf[len-4] = '\0'; + r->len = len - 4; + } else { + buf = malloc(len+1); + if (buf == NULL) { + freeReplyObject(r); + return NULL; + } + memcpy(buf,str,len); + buf[len] = '\0'; + r->len = len; + } r->str = buf; - r->len = len; if (task->parent) { parent = task->parent->obj; -- cgit v1.2.3