Skip to content

Commit

Permalink
Fix RESP parser - Allow Redis return empty array (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
moticless authored Apr 21, 2024
1 parent 6c54c27 commit 581221f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/ext/readerResp.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ RespRes readRespReplyBulk(RespReaderCtx *ctx, RespReplyBuff *buffInfo) {
return RESP_REPLY_ERR;
}

/* If empty bulk */
if (ctx->bulkLen == 0) {
snprintf(ctx->errorMsg, sizeof(ctx->errorMsg), "Response Bulk must be bigger than zero");
return RESP_REPLY_ERR;
ctx->typeState = PROC_BULK_READ_END;
break;
}

ctx->typeState = PROC_BULK_READ;
Expand Down Expand Up @@ -232,11 +233,12 @@ RespRes readRespReplyBulk(RespReaderCtx *ctx, RespReplyBuff *buffInfo) {
}

ctx->typeState = PROC_BULK_READ_END;
/* fall-through */
break;
}

case PROC_BULK_READ_END:
ctx->typeState = 0;
return RESP_REPLY_OK;
if (ctx->typeState == PROC_BULK_READ_END) {
ctx->typeState = PROC_BULK_READ_INIT;
return RESP_REPLY_OK;
}
}
}
Expand Down Expand Up @@ -299,9 +301,10 @@ static RespRes readRespReplyBulkArray(RespReaderCtx *ctx, RespReplyBuff *buffInf
return RESP_REPLY_ERR;
}

/* if empty array then jump to READ_END of array */
if (ctx->numBulksArray == 0) {
snprintf(ctx->errorMsg, sizeof(ctx->errorMsg), "Bulk Array must be bigger than zero");
return RESP_REPLY_ERR;
ctx->typeArrayState = READ_END;
break;
}

ctx->typeArrayState = READ_NEXT_BULK_HDR;
Expand All @@ -324,10 +327,12 @@ static RespRes readRespReplyBulkArray(RespReaderCtx *ctx, RespReplyBuff *buffInf
break;
}
ctx->typeArrayState = READ_END; /* fall-through */
break;
}

case READ_END:
ctx->typeArrayState = 0;
return RESP_REPLY_OK;
if (ctx->typeArrayState == READ_END) {
ctx->typeArrayState = READ_INIT;
return RESP_REPLY_OK;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/test_resp_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ static void test_single_status(void **state) {
1, RESP_REPLY_OK, 1);
}

static void test_empty_array(void **state) {
UNUSED(state);
test_resp_reader_common(NULL, STR_AND_SIZE("*0\r\n"),
1, RESP_REPLY_OK, 1);
}

static void test_empty_bulk(void **state) {
UNUSED(state);
test_resp_reader_common(NULL, STR_AND_SIZE("$0\r\n"),
1, RESP_REPLY_OK, 1);
}

static void test_single_int(void **state) {
UNUSED(state);
test_resp_reader_common(NULL, STR_AND_SIZE(":1\r\n"),
Expand Down Expand Up @@ -141,6 +153,8 @@ static void test_mixture_and_fragmented(void **state) {
int group_test_resp_reader(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_single_status),
cmocka_unit_test(test_empty_array),
cmocka_unit_test(test_empty_bulk),
cmocka_unit_test(test_single_int),
cmocka_unit_test(test_array_single_bulk),
cmocka_unit_test(test_array_3_bulks),
Expand Down

0 comments on commit 581221f

Please sign in to comment.