8000 refactor redis_sock_read_bulk_reply · chenyongze/phpredis@74ba34b · GitHub
[go: up one dir, main page]

Skip to content

Commit 74ba34b

Browse files
yatsukhnenkomichael-grunder
authored andcommitted
refactor redis_sock_read_bulk_reply
Throws error and returns NULL when we read less bytes than expected. This patch probably fixes "protocol error, got '' as reply type byte" error.
1 parent 4aff3f9 commit 74ba34b

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

library.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ redis_sock_read_multibulk_reply_zval(INTERNAL_FUNCTION_PARAMETERS,
463463
/**
464464
* redis_sock_read_bulk_reply
465465
*/
466-
PHP_REDIS_API char *redis_sock_read_bulk_reply(RedisSock *redis_sock, int bytes TSRMLS_DC)
466+
PHP_REDIS_API char *
467+
redis_sock_read_bulk_reply(RedisSock *redis_sock, int bytes TSRMLS_DC)
467468
{
468469
int offset = 0;
469470
size_t got;
@@ -478,17 +479,19 @@ PHP_REDIS_API char *redis_sock_read_bulk_reply(RedisSock *redis_sock, int bytes
478479
while(offset < bytes) {
479480
got = php_stream_read(redis_sock->stream, reply + offset,
480481
bytes-offset);
481-
if (got <= 0) {
482-
/* Error or EOF */
483-
zend_throw_exception(redis_exception_ce,
484-
"socket error on read socket", 0 TSRMLS_CC);
485-
break;
486-
}
482+
if (got == 0) break;
487483
offset += got;
488484
}
485+
if (offset < bytes) {
486+
/* Error or EOF */
487+
zend_throw_exception(redis_exception_ce,
488+
"socket error on read socket", 0 TSRMLS_CC);
489+
efree(reply);
490+
return NULL;
491+
}
489492
php_stream_read(redis_sock->stream, c, 2);
490493

491-
reply[bytes] = 0;
494+
reply[bytes] = '\0';
492495
return reply;
493496
}
494497

0 commit comments

Comments
 (0)
0