Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc4dbc4

Browse files
committed
Refactor redis_sock_read_bulk_reply
1 parent c0793e8 commit bc4dbc4

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

library.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -468,35 +468,35 @@ redis_sock_read_multibulk_reply_zval(INTERNAL_FUNCTION_PARAMETERS,
468468
PHP_REDIS_API char *
469469
redis_sock_read_bulk_reply(RedisSock *redis_sock, int bytes TSRMLS_DC)
470470
{
471-
int offset = 0;
472-
char *reply, c[2];
471+
int offset = 0, nbytes;
472+
char *reply;
473473
size_t got;
474474

475475
if (-1 == bytes || -1 == redis_check_eof(redis_sock, 0 TSRMLS_CC)) {
476476
return NULL;
477477
}
478478

479+
nbytes = bytes + 2;
479480
/* Allocate memory for string */
480-
reply = emalloc(bytes+1);
481+
reply = emalloc(nbytes);
481482

482483
/* Consume bulk string */
483-
while(offset < bytes) {
484-
got = php_stream_read(redis_sock->stream, reply + offset, bytes-offset);
485-
if (got == 0) break;
484+
while (offset < nbytes) {
485+
got = php_stream_read(redis_sock->stream, reply + offset, nbytes - offset);
486+
if (got == 0 && php_stream_eof(redis_sock->stream)) break;
486487
offset += got;
487488
}
488489

489490
/* Protect against reading too few bytes */
490-
if (offset < bytes) {
491+
if (offset < nbytes) {
491492
/* Error or EOF */
492493
zend_throw_exception(redis_exception_ce,
493494
"socket error on read socket", 0 TSRMLS_CC);
494495
efree(reply);
495496
return NULL;
496497
}
497498

498-
/* Consume \r\n and null terminate reply string */
499-
php_stream_read(redis_sock->stream, c, 2);
499+
/* Null terminate reply string */
500500
reply[bytes] = '\0';
501501

502502
return reply;

0 commit comments

Comments
 (0)