Skip to content

Commit ca8b4c9

Browse files
committedApr 13, 2022
Use read_mbulk_header helper where possible
1 parent 3645755 commit ca8b4c9

File tree

1 file changed

+39
-103
lines changed

1 file changed

+39
-103
lines changed
 

‎library.c

+39-103
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,29 @@ redis_error_throw(RedisSock *redis_sock)
286286
}
287287
}
288288

289+
static int
290+
read_mbulk_header(RedisSock *redis_sock, int *nelem)
291+
{
292+
char line[4096];
293+
size_t len;
294+
295+
/* Throws exception on failure */
296+
if (redis_sock_gets(redis_sock, line, sizeof(line) - 1, &len) < 0) {
297+
return FAILURE;
298+
}
299+
300+
if (*line != TYPE_MULTIBULK) {
301+
if (*line == TYPE_ERR) {
302+
redis_sock_set_err(redis_sock, line + 1, len - 1);
303+
}
304+
return FAILURE;
305+
}
306+
307+
*nelem = atoi(line + 1);
308+
309+
return SUCCESS;
310+
}
311+
289312
PHP_REDIS_API int
290313
redis_check_eof(RedisSock *redis_sock, zend_bool no_retry, zend_bool no_throw)
291314
{
@@ -561,22 +584,13 @@ PHP_REDIS_API zval *
561584
redis_sock_read_multibulk_reply_zval(INTERNAL_FUNCTION_PARAMETERS,
562585
RedisSock *redis_sock, zval *z_tab)
563586
{
564-
char inbuf[4096];
565587
int numElems;
566-
size_t len;
567588

568-
ZVAL_NULL(z_tab);
569-
if (redis_sock_gets(redis_sock, inbuf, sizeof(inbuf) - 1, &len) < 0) {
570-
return NULL;
571-
}
572-
573-
if(inbuf[0] != '*') {
589+
if (read_mbulk_header(redis_sock, &numElems) < 0) {
590+
ZVAL_NULL(z_tab);
574591
return NULL;
575592
}
576-
numElems = atoi(inbuf+1);
577-
578593
array_init(z_tab);
579-
580594
redis_mbulk_reply_loop(redis_sock, z_tab, numElems, UNSERIALIZE_ALL);
581595

582596
return z_tab;
@@ -1461,59 +1475,21 @@ static void array_zip_values_and_scores(RedisSock *redis_sock, zval *z_tab,
14611475
ZVAL_ZVAL(z_tab, &z_ret, 0, 0);
14621476
}
14631477

1464-
static int
1465-
read_mbulk_header(RedisSock *redis_sock, int *nelem)
1466-
{
1467-
char line[4096];
1468-
size_t len;
1469-
1470-
/* Throws exception on failure */
1471-
if (redis_sock_gets(redis_sock, line, sizeof(line)-1, &len) < 0)
1472-
return -1;
1473-
1474-
if (line[0] != '*') {
1475-
if (IS_ATOMIC(redis_sock)) {
1476-
if (line[0] == '-') {
1477-
redis_sock_set_err(redis_sock, line+1, len-1);
1478-
}
1479-
}
1480-
return -1;
1481-
}
1482-
1483-
*nelem = atoi(line+1);
1484-
1485-
return 0;
1486-
}
14871478

14881479
static int
14891480
redis_mbulk_reply_zipped(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
14901481
zval *z_tab, int unserialize, int decode)
14911482
{
1492-
char inbuf[4096];
14931483
int numElems;
1494-
size_t len;
14951484

1496-
if (redis_sock_gets(redis_sock, inbuf, sizeof(inbuf) - 1, &len) < 0) {
1485+
if (read_mbulk_header(redis_sock, &numElems) < 0) {
14971486
if (IS_ATOMIC(redis_sock)) {
14981487
RETVAL_FALSE;
14991488
} else {
15001489
add_next_index_bool(z_tab, 0);
15011490
}
15021491
return FAILURE;
15031492
}
1504-
1505-
if(inbuf[0] != '*') {
1506-
if (IS_ATOMIC(redis_sock)) {
1507-
RETVAL_FALSE;
1508-
} else {
1509-
add_next_index_bool(z_tab, 0);
1510-
}
1511-
if (*inbuf == TYPE_ERR) {
1512-
redis_sock_set_err(redis_sock, inbuf + 1, len - 1);
1513-
}
1514-
return -1;
1515-
}
1516-
numElems = atoi(inbuf+1);
15171493
zval z_multi_result;
15181494
array_init(&z_multi_result); /* pre-allocate array for multi's results. */
15191495

@@ -1552,6 +1528,11 @@ redis_geosearch_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
15521528
zend_string *zkey;
15531529

15541530
if (read_mbulk_header(redis_sock, &numElems) < 0) {
1531+
if (IS_ATOMIC(redis_sock)) {
1532+
RETVAL_FALSE;
1533+
} else {
1534+
add_next_index_bool(z_tab, 0);
1535+
}
15551536
return FAILURE;
15561537
}
15571538

@@ -2614,28 +2595,16 @@ PHP_REDIS_API int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS,
26142595
void *ctx)
26152596
{
26162597
zval z_multi_result;
2617-
char inbuf[4096];
26182598
int numElems;
2619-
size_t len;
26202599

2621-
if (redis_sock_gets(redis_sock, inbuf, sizeof(inbuf) - 1, &len) < 0) {
2622-
return -1;
2623-
}
2624-
2625-
if(inbuf[0] != '*') {
2600+
if (read_mbulk_header(redis_sock, &numElems) < 0) {
26262601
if (IS_ATOMIC(redis_sock)) {
2627-
if (inbuf[0] == '-') {
2628-
redis_sock_set_err(redis_sock, inbuf+1, len);
2629-
}
26302602
RETVAL_FALSE;
26312603
} else {
26322604
add_next_index_bool(z_tab, 0);
26332605
}
2634-
return -1;
2606+
return FAILURE;
26352607
}
2636-
2637-
numElems = atoi(inbuf+1);
2638-
26392608
if (numElems == -1 && redis_sock->null_mbulk_as_null) {
26402609
ZVAL_NULL(&z_multi_result);
26412610
} else {
@@ -2657,31 +2626,16 @@ PHP_REDIS_API int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS,
26572626
PHP_REDIS_API int
26582627
redis_mbulk_reply_raw(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx)
26592628
{
2660-
char inbuf[4096];
26612629
int numElems;
2662-
size_t len;
26632630

2664-
if (redis_sock_gets(redis_sock, inbuf, sizeof(inbuf) - 1, &len) < 0) {
2631+
if (read_mbulk_header(redis_sock, &numElems) < 0) {
26652632
if (IS_ATOMIC(redis_sock)) {
26662633
RETVAL_FALSE;
26672634
} else {
26682635
add_next_index_bool(z_tab, 0);
26692636
}
26702637
return FAILURE;
26712638
}
2672-
2673-
if(inbuf[0] != '*') {
2674-
if (IS_ATOMIC(redis_sock)) {
2675-
if (inbuf[0] == '-') {
2676-
redis_sock_set_err(redis_sock, inbuf+1, len);
2677-
}
2678-
RETVAL_FALSE;
2679-
} else {
2680-
add_next_index_bool(z_tab, 0);
2681-
}
2682-
return -1;
2683-
}
2684-
numElems = atoi(inbuf+1);
26852639
zval z_multi_result;
26862640
array_init(&z_multi_result); /* pre-allocate array for multi's results. */
26872641

@@ -2693,33 +2647,24 @@ redis_mbulk_reply_raw(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval
26932647
add_next_index_zval(z_tab, &z_multi_result);
26942648
}
26952649

2696-
return 0;
2650+
return SUCCESS;
26972651
}
26982652

26992653
PHP_REDIS_API int
27002654
redis_mbulk_reply_double(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx)
27012655
{
2702-
char inbuf[4096], *line;
2656+
char *line;
27032657
int i, numElems, len;
2704-
size_t buf_len;
27052658
zval z_multi_result;
27062659

2707-
if (redis_sock_gets(redis_sock, inbuf, sizeof(inbuf) - 1, &buf_len) < 0) {
2708-
return FAILURE;
2709-
}
2710-
2711-
if (*inbuf != TYPE_MULTIBULK) {
2660+
if (read_mbulk_header(redis_sock, &numElems) < 0) {
27122661
if (IS_ATOMIC(redis_sock)) {
2713-
if (*inbuf == TYPE_ERR) {
2714-
redis_sock_set_err(redis_sock, inbuf + 1, buf_len);
2715-
}
27162662
RETVAL_FALSE;
27172663
} else {
27182664
add_next_index_bool(z_tab, 0);
27192665
}
27202666
return FAILURE;
27212667
}
2722-
numElems = atoi(inbuf + 1);
27232668

27242669
array_init(&z_multi_result);
27252670
for (i = 0; i < numElems; ++i) {
@@ -2825,29 +2770,20 @@ redis_mbulk_reply_zipped_raw_variant(RedisSock *redis_sock, zval *zret, int coun
28252770
* keys with their returned values */
28262771
PHP_REDIS_API int redis_mbulk_reply_assoc(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx)
28272772
{
2828-
char inbuf[4096], *response;
2773+
char *response;
28292774
int response_len;
28302775
int i, numElems;
2831-
size_t len;
28322776

28332777
zval *z_keys = ctx;
28342778

2835-
if (redis_sock_gets(redis_sock, inbuf, sizeof(inbuf) - 1, &len) < 0) {
2836-
goto failure;
2837-
}
2838-
2839-
if (*inbuf != TYPE_MULTIBULK) {
2779+
if (read_mbulk_header(redis_sock, &numElems) < 0) {
28402780
if (IS_ATOMIC(redis_sock)) {
28412781
RETVAL_FALSE;
28422782
} else {
28432783
add_next_index_bool(z_tab, 0);
28442784
}
2845-
if (*inbuf == TYPE_ERR) {
2846-
redis_sock_set_err(redis_sock, inbuf + 1, len - 1);
2847-
}
28482785
goto failure;
28492786
}
2850-
numElems = atoi(inbuf+1);
28512787
zval z_multi_result;
28522788
array_init(&z_multi_result); /* pre-allocate array for multi's results. */
28532789

0 commit comments

Comments
 (0)
Please sign in to comment.