@@ -818,6 +818,9 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
818818 redis_sock -> pipeline_head = NULL ;
819819 redis_sock -> pipeline_current = NULL ;
820820
821+ redis_sock -> err = NULL ;
822+ redis_sock -> err_len = 0 ;
823+
821824 return redis_sock ;
822825}
823826
@@ -972,6 +975,37 @@ PHPAPI void redis_send_discard(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_so
972975 RETURN_FALSE ;
973976}
974977
978+ /**
979+ * redis_sock_set_err
980+ */
981+ PHPAPI int redis_sock_set_err (RedisSock * redis_sock , const char * msg , int msg_len ) {
982+ // Allocate/Reallocate our last error member
983+ if (msg != NULL && msg_len > 0 ) {
984+ if (redis_sock -> err == NULL ) {
985+ redis_sock -> err = emalloc (msg_len + 1 );
986+ } else if (msg_len > redis_sock -> err_len ) {
987+ redis_sock -> err = erealloc (redis_sock -> err , msg_len + 1 );
988+ }
989+
990+ // Copy in our new error message, set new length, and null terminate
991+ memcpy (redis_sock -> err , msg , msg_len );
992+ redis_sock -> err [msg_len ] = '\0' ;
993+ redis_sock -> err_len = msg_len ;
994+ } else {
995+ // Free our last error
996+ if (redis_sock -> err != NULL ) {
997+ efree (redis_sock -> err );
998+ }
999+
1000+ // Set to null, with zero length
1001+ redis_sock -> err = NULL ;
1002+ redis_sock -> err_len = 0 ;
1003+ }
1004+
1005+ // Success
1006+ return 0 ;
1007+ }
1008+
9751009/**
9761010 * redis_sock_read_multibulk_reply
9771011 */
@@ -1170,6 +1204,9 @@ PHPAPI void redis_free_socket(RedisSock *redis_sock)
11701204 if (redis_sock -> prefix ) {
11711205 efree (redis_sock -> prefix );
11721206 }
1207+ if (redis_sock -> err ) {
1208+ efree (redis_sock -> err );
1209+ }
11731210 efree (redis_sock -> host );
11741211 efree (redis_sock );
11751212}
@@ -1310,7 +1347,7 @@ redis_key_prefix(RedisSock *redis_sock, char **key, int *key_len TSRMLS_DC) {
13101347 */
13111348
13121349PHPAPI int
1313- redis_sock_gets (RedisSock * redis_sock , char * buf , int buf_size , int * line_size ) {
1350+ redis_sock_gets (RedisSock * redis_sock , char * buf , int buf_size , size_t * line_size ) {
13141351 // Handle EOF
13151352 if (-1 == redis_check_eof (redis_sock TSRMLS_CC )) {
13161353 return -1 ;
@@ -1328,6 +1365,11 @@ redis_sock_gets(RedisSock *redis_sock, char *buf, int buf_size, int *line_size)
13281365 zend_throw_exception (redis_exception_ce , "read error on connection" , 0 TSRMLS_CC );
13291366 }
13301367
1368+ // We don't need \r\n
1369+ * line_size -= 2 ;
1370+ buf [* line_size ]= '\0' ;
1371+
1372+
13311373 // Success!
13321374 return 0 ;
13331375}
@@ -1370,7 +1412,7 @@ PHPAPI int
13701412redis_read_variant_line (RedisSock * redis_sock , REDIS_REPLY_TYPE reply_type , zval * * z_ret ) {
13711413 // Buffer to read our single line reply
13721414 char inbuf [1024 ];
1373- int line_size ;
1415+ size_t line_size ;
13741416
13751417 // Attempt to read our single line reply
13761418 if (redis_sock_gets (redis_sock , inbuf , sizeof (inbuf ), & line_size ) < 0 ) {
@@ -1383,6 +1425,9 @@ redis_read_variant_line(RedisSock *redis_sock, REDIS_REPLY_TYPE reply_type, zval
13831425 zend_throw_exception (redis_exception_ce , "SYNC with master in progress" , 0 TSRMLS_CC );
13841426 }
13851427
1428+ // Set our last error
1429+ redis_sock_set_err (redis_sock , inbuf , line_size );
1430+
13861431 // Set our response to FALSE
13871432 ZVAL_FALSE (* z_ret );
13881433 } else {
0 commit comments