Skip to content

Commit

Permalink
Allow RESP empty bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
moticless committed Apr 21, 2024
1 parent 8b3e259 commit 8c28284
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
22 changes: 13 additions & 9 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 @@ -325,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
18 changes: 16 additions & 2 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 @@ -118,8 +130,8 @@ static void test_reply_long_err_trimmed_by_report(void **state) {
static void test_mixture_and_fragmented(void **state) {
UNUSED(state);
RespRes res;
int expReplies = 6;
char bulk[] = "*0\r\n*3\r\n$2\r\n12\r\n$1\r\nA\r\n$3\r\nABC\r\n"
int expReplies = 5;
char bulk[] = "*3\r\n$2\r\n12\r\n$1\r\nA\r\n$3\r\nABC\r\n"
"+OK\r\n$5\r\nmylib\r\n+OK\r\n+OK\r\n";
RespReaderCtx ctx;

Expand All @@ -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 8c28284

Please sign in to comment.