Skip to content

Commit 8c28284

Browse files
committed
Allow RESP empty bulk
1 parent 8b3e259 commit 8c28284

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/ext/readerResp.c

+13-9
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,10 @@ RespRes readRespReplyBulk(RespReaderCtx *ctx, RespReplyBuff *buffInfo) {
197197
return RESP_REPLY_ERR;
198198
}
199199

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

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

234235
ctx->typeState = PROC_BULK_READ_END;
235-
/* fall-through */
236+
break;
237+
}
236238

237-
case PROC_BULK_READ_END:
238-
ctx->typeState = 0;
239-
return RESP_REPLY_OK;
239+
if (ctx->typeState == PROC_BULK_READ_END) {
240+
ctx->typeState = PROC_BULK_READ_INIT;
241+
return RESP_REPLY_OK;
240242
}
241243
}
242244
}
@@ -325,10 +327,12 @@ static RespRes readRespReplyBulkArray(RespReaderCtx *ctx, RespReplyBuff *buffInf
325327
break;
326328
}
327329
ctx->typeArrayState = READ_END; /* fall-through */
330+
break;
331+
}
328332

329-
case READ_END:
330-
ctx->typeArrayState = 0;
331-
return RESP_REPLY_OK;
333+
if (ctx->typeArrayState == READ_END) {
334+
ctx->typeArrayState = READ_INIT;
335+
return RESP_REPLY_OK;
332336
}
333337
}
334338
}

test/test_resp_reader.c

+16-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ static void test_single_status(void **state) {
2727
1, RESP_REPLY_OK, 1);
2828
}
2929

30+
static void test_empty_array(void **state) {
31+
UNUSED(state);
32+
test_resp_reader_common(NULL, STR_AND_SIZE("*0\r\n"),
33+
1, RESP_REPLY_OK, 1);
34+
}
35+
36+
static void test_empty_bulk(void **state) {
37+
UNUSED(state);
38+
test_resp_reader_common(NULL, STR_AND_SIZE("$0\r\n"),
39+
1, RESP_REPLY_OK, 1);
40+
}
41+
3042
static void test_single_int(void **state) {
3143
UNUSED(state);
3244
test_resp_reader_common(NULL, STR_AND_SIZE(":1\r\n"),
@@ -118,8 +130,8 @@ static void test_reply_long_err_trimmed_by_report(void **state) {
118130
static void test_mixture_and_fragmented(void **state) {
119131
UNUSED(state);
120132
RespRes res;
121-
int expReplies = 6;
122-
char bulk[] = "*0\r\n*3\r\n$2\r\n12\r\n$1\r\nA\r\n$3\r\nABC\r\n"
133+
int expReplies = 5;
134+
char bulk[] = "*3\r\n$2\r\n12\r\n$1\r\nA\r\n$3\r\nABC\r\n"
123135
"+OK\r\n$5\r\nmylib\r\n+OK\r\n+OK\r\n";
124136
RespReaderCtx ctx;
125137

@@ -141,6 +153,8 @@ static void test_mixture_and_fragmented(void **state) {
141153
int group_test_resp_reader(void) {
142154
const struct CMUnitTest tests[] = {
143155
cmocka_unit_test(test_single_status),
156+
cmocka_unit_test(test_empty_array),
157+
cmocka_unit_test(test_empty_bulk),
144158
cmocka_unit_test(test_single_int),
145159
cmocka_unit_test(test_array_single_bulk),
146160
cmocka_unit_test(test_array_3_bulks),

0 commit comments

Comments
 (0)