Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RESP parser - Allow Redis return empty array #45

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading