Skip to content

Commit 43e1e0f

Browse files
authored
refactoring (phpredis#1155)
Fix memory leak in `redis_long_response` when LONG_MAX overflow. Add return -1 in `redis_read_reply_type` when `php_stream_getc` returns EOF. Code formatting in `PHP_METHOD(Redis, exec)`.
1 parent d2e203a commit 43e1e0f

File tree

4 files changed

+12
-23
lines changed

4 files changed

+12
-23
lines changed

common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,9 @@ typedef enum _PUBSUB_TYPE {
434434
#define REDIS_OPT_PREFIX 2
435435
#define REDIS_OPT_READ_TIMEOUT 3
436436
#define REDIS_OPT_SCAN 4
437+
#define REDIS_OPT_FAILOVER 5
437438

438439
/* cluster options */
439-
#define REDIS_OPT_FAILOVER 5
440440
#define REDIS_FAILOVER_NONE 0
441441
#define REDIS_FAILOVER_ERROR 1
442442
#define REDIS_FAILOVER_DISTRIBUTE 2

library.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,6 @@ PHP_REDIS_API void redis_long_response(INTERNAL_FUNCTION_PARAMETERS,
11531153
if(ret > LONG_MAX) { /* overflow */
11541154
add_next_index_stringl(z_tab, response + 1, response_len - 1);
11551155
} else {
1156-
efree(response);
11571156
add_next_index_long(z_tab, (long)ret);
11581157
}
11591158
} else {
@@ -1162,16 +1161,15 @@ PHP_REDIS_API void redis_long_response(INTERNAL_FUNCTION_PARAMETERS,
11621161
} else {
11631162
RETVAL_LONG((long)ret);
11641163
}
1165-
efree(response);
11661164
}
11671165
} else {
1168-
efree(response);
11691166
IF_NOT_ATOMIC() {
11701167
add_next_index_null(z_tab);
11711168
} else {
1172-
RETURN_FALSE;
1169+
RETVAL_FALSE;
11731170
}
11741171
}
1172+
efree(response);
11751173
}
11761174

11771175
/* Helper method to convert [key, value, key, value] into [key => value,
@@ -1614,7 +1612,7 @@ PHP_REDIS_API int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC)
16141612
redis_sock->watching = 0;
16151613

16161614
/* Stil valid? */
1617-
if(redis_sock->stream && !redis_sock->persistent) {
1615+
if (!redis_sock->persistent) {
16181616
php_stream_close(redis_sock->stream);
16191617
}
16201618
redis_sock->stream = NULL;
@@ -2104,6 +2102,7 @@ redis_read_reply_type(RedisSock *redis_sock, REDIS_REPLY_TYPE *reply_type,
21042102
if((*reply_type = php_stream_getc(redis_sock->stream)) == EOF) {
21052103
zend_throw_exception(redis_exception_ce, "socket error on read socket",
21062104
0 TSRMLS_CC);
2105+
return -1;
21072106
}
21082107

21092108
// If this is a BULK, MULTI BULK, or simply an INTEGER response, we can

redis.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838

3939
#include "library.h"
4040

41-
#define R_SUB_CALLBACK_CLASS_TYPE 1
42-
#define R_SUB_CALLBACK_FT_TYPE 2
43-
#define R_SUB_CLOSURE_TYPE 3
44-
4541
#ifdef PHP_SESSION
4642
extern ps_module ps_mod_redis;
4743
extern ps_module ps_mod_redis_cluster;
@@ -2366,7 +2362,7 @@ PHP_METHOD(Redis, exec)
23662362
{
23672363
RedisSock *redis_sock;
23682364
char *cmd;
2369-
int cmd_len;
2365+
int cmd_len, ret;
23702366
zval *object;
23712367

23722368
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(),
@@ -2381,19 +2377,15 @@ PHP_METHOD(Redis, exec)
23812377
SOCKET_WRITE_COMMAND(redis_sock, cmd, cmd_len)
23822378
efree(cmd);
23832379

2384-
if(redis_sock_read_multibulk_multi_reply(
2385-
INTERNAL_FUNCTION_PARAM_PASSTHRU,
2386-
redis_sock) < 0)
2387-
{
2388-
zval_dtor(return_value);
2389-
free_reply_callbacks(redis_sock);
2390-
redis_sock->mode = ATOMIC;
2391-
redis_sock->watching = 0;
2392-
RETURN_FALSE;
2393-
}
2380+
ret = redis_sock_read_multibulk_multi_reply(
2381+
INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock);
23942382
free_reply_callbacks(redis_sock);
23952383
redis_sock->mode = ATOMIC;
23962384
redis_sock->watching = 0;
2385+
if (ret < 0) {
2386+
zval_dtor(return_value);
2387+
RETURN_FALSE;
2388+
}
23972389
}
23982390

23992391
IF_PIPELINE() {

redis_array.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#endif
99
#include "common.h"
1010

11-
void redis_destructor_redis_array(zend_resource * rsrc TSRMLS_DC);
12-
1311
PHP_METHOD(RedisArray, __construct);
1412
PHP_METHOD(RedisArray, __call);
1513
PHP_METHOD(RedisArray, _hosts);

0 commit comments

Comments
 (0)