@@ -442,7 +442,6 @@ static zend_string *php_brotli_output_handler_load_dict(php_brotli_context *ctx)
442
442
"request(%s) != actual(%s)" ,
443
443
Z_STRVAL_P (available ), ZSTR_VAL (b64 ));
444
444
BROTLI_G (compression_coding ) &= ~PHP_BROTLI_ENCODING_DCB ;
445
- zend_string_free (dict );
446
445
dict = NULL ;
447
446
}
448
447
zend_string_free (b64 );
@@ -451,7 +450,6 @@ static zend_string *php_brotli_output_handler_load_dict(php_brotli_context *ctx)
451
450
php_error_docref (NULL , E_WARNING ,
452
451
"brotli: not found available-dictionary" );
453
452
BROTLI_G (compression_coding ) &= ~PHP_BROTLI_ENCODING_DCB ;
454
- zend_string_free (dict );
455
453
dict = NULL ;
456
454
}
457
455
}
@@ -469,23 +467,15 @@ static int php_brotli_output_handler_context_start(php_brotli_context *ctx)
469
467
long level = BROTLI_G (output_compression_level );
470
468
zend_string * dict = php_brotli_output_handler_load_dict (ctx );
471
469
if (!BROTLI_G (compression_coding )) {
472
- if (dict ) {
473
- zend_string_release (dict );
474
- }
475
470
return FAILURE ;
476
471
}
477
472
478
- int result = php_brotli_context_create_encoder_ex (ctx ,
479
- level ,
480
- BROTLI_DEFAULT_WINDOW ,
481
- BROTLI_MODE_GENERIC ,
482
- dict ,
483
- 0 );
484
- if (dict ) {
485
- zend_string_release (dict );
486
- }
487
-
488
- return result ;
473
+ return php_brotli_context_create_encoder_ex (ctx ,
474
+ level ,
475
+ BROTLI_DEFAULT_WINDOW ,
476
+ BROTLI_MODE_GENERIC ,
477
+ dict ,
478
+ 0 );
489
479
}
490
480
491
481
static int php_brotli_output_handler (void * * handler_context ,
@@ -830,34 +820,44 @@ static size_t php_brotli_decompress_read(php_stream *stream,
830
820
size_t ret = 0 ;
831
821
#else
832
822
static ssize_t php_brotli_decompress_read (php_stream * stream ,
833
- char * buf ,
834
- size_t count )
823
+ char * buf ,
824
+ size_t count )
835
825
{
836
826
ssize_t ret = 0 ;
837
827
#endif
838
828
STREAM_DATA_FROM_STREAM ();
839
829
840
830
/* input */
841
831
uint8_t * input = (uint8_t * )emalloc (PHP_BROTLI_BUFFER_SIZE );
832
+ if (!input ) {
833
+ return 0 ;
834
+ }
835
+
842
836
if (self -> result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT ) {
843
837
if (php_stream_eof (self -> stream )) {
844
838
/* corrupt input */
845
- if (input ) {
846
- efree (input );
847
- }
839
+ efree (input );
848
840
#if PHP_VERSION_ID < 70400
849
841
return 0 ;
850
842
#else
851
843
return -1 ;
852
844
#endif
853
845
}
854
846
self -> ctx .available_in = php_stream_read (self -> stream , input ,
855
- PHP_BROTLI_BUFFER_SIZE );
847
+ PHP_BROTLI_BUFFER_SIZE );
848
+ if (!self -> ctx .available_in ) {
849
+ efree (input );
850
+ return 0 ;
851
+ }
856
852
self -> ctx .next_in = input ;
857
853
}
858
854
859
855
/* output */
860
856
uint8_t * output = (uint8_t * )emalloc (count );
857
+ if (!output ) {
858
+ efree (input );
859
+ return 0 ;
860
+ }
861
861
self -> ctx .available_out = count ;
862
862
self -> ctx .next_out = output ;
863
863
@@ -891,12 +891,8 @@ static ssize_t php_brotli_decompress_read(php_stream *stream,
891
891
}
892
892
}
893
893
894
- if (input ) {
895
- efree (input );
896
- }
897
- if (output ) {
898
- efree (output );
899
- }
894
+ efree (input );
895
+ efree (output );
900
896
901
897
return ret ;
902
898
}
@@ -1521,6 +1517,7 @@ static ZEND_FUNCTION(brotli_compress)
1521
1517
efree (buffer );
1522
1518
smart_string_free (& out );
1523
1519
php_error_docref (NULL , E_WARNING , "failed to compress" );
1520
+ php_brotli_context_close (& ctx );
1524
1521
RETURN_FALSE ;
1525
1522
}
1526
1523
}
0 commit comments