Skip to content

Commit

Permalink
Fix for issue redis#45
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed Jun 9, 2011
1 parent 159a83a commit 5f5b3d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 7 additions & 5 deletions hiredis.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void freeReplyObject(void *reply) {
case REDIS_REPLY_INTEGER:
break; /* Nothing to free */
case REDIS_REPLY_ARRAY:
if (r->elements > 0 && r->element != NULL) {
if (r->element != NULL) {
for (j = 0; j < r->elements; j++)
if (r->element[j] != NULL)
freeReplyObject(r->element[j]);
Expand Down Expand Up @@ -133,10 +133,12 @@ static void *createArrayObject(const redisReadTask *task, int elements) {
if (r == NULL)
return NULL;

r->element = calloc(elements,sizeof(redisReply*));
if (r->element == NULL) {
freeReplyObject(r);
return NULL;
if (elements > 0) {
r->element = calloc(elements,sizeof(redisReply*));
if (r->element == NULL) {
freeReplyObject(r);
return NULL;
}
}

r->elements = elements;
Expand Down
12 changes: 12 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ static void test_reply_reader(void) {
assert(ret == REDIS_ERR);
ret = redisReaderGetReply(reader,&reply);
test_cond(ret == REDIS_ERR && reply == NULL);
redisReaderFree(reader);

/* Regression test for issue #45 on GitHub. */
test("Don't do empty allocation for empty multi bulk: ");
reader = redisReaderCreate();
redisReaderFeed(reader,(char*)"*0\r\n",4);
ret = redisReaderGetReply(reader,&reply);
test_cond(ret == REDIS_OK &&
((redisReply*)reply)->type == REDIS_REPLY_ARRAY &&
((redisReply*)reply)->elements == 0);
freeReplyObject(reply);
redisReaderFree(reader);
}

static void *test_create_string(const redisReadTask *task, char *str, size_t len) {
Expand Down

0 comments on commit 5f5b3d9

Please sign in to comment.