@@ -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,56 @@ 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
+ #if PHP_VERSION_ID < 70400
834
+ return 0 ;
835
+ #else
836
+ return -1 ;
837
+ #endif
838
+ }
839
+
842
840
if (self -> result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT ) {
843
841
if (php_stream_eof (self -> stream )) {
844
842
/* corrupt input */
845
- if (input ) {
846
- efree (input );
847
- }
843
+ efree (input );
848
844
#if PHP_VERSION_ID < 70400
849
845
return 0 ;
850
846
#else
851
847
return -1 ;
852
848
#endif
853
849
}
854
850
self -> ctx .available_in = php_stream_read (self -> stream , input ,
855
- PHP_BROTLI_BUFFER_SIZE );
851
+ PHP_BROTLI_BUFFER_SIZE );
852
+ if (!self -> ctx .available_in ) {
853
+ efree (input );
854
+ #if PHP_VERSION_ID < 70400
855
+ return 0 ;
856
+ #else
857
+ return -1 ;
858
+ #endif
859
+ }
856
860
self -> ctx .next_in = input ;
857
861
}
858
862
859
863
/* output */
860
864
uint8_t * output = (uint8_t * )emalloc (count );
865
+ if (!output ) {
866
+ efree (input );
867
+ #if PHP_VERSION_ID < 70400
868
+ return 0 ;
869
+ #else
870
+ return -1 ;
871
+ #endif
872
+ }
861
873
self -> ctx .available_out = count ;
862
874
self -> ctx .next_out = output ;
863
875
@@ -891,12 +903,8 @@ static ssize_t php_brotli_decompress_read(php_stream *stream,
891
903
}
892
904
}
893
905
894
- if (input ) {
895
- efree (input );
896
- }
897
- if (output ) {
898
- efree (output );
899
- }
906
+ efree (input );
907
+ efree (output );
900
908
901
909
return ret ;
902
910
}
@@ -1521,6 +1529,7 @@ static ZEND_FUNCTION(brotli_compress)
1521
1529
efree (buffer );
1522
1530
smart_string_free (& out );
1523
1531
php_error_docref (NULL , E_WARNING , "failed to compress" );
1532
+ php_brotli_context_close (& ctx );
1524
1533
RETURN_FALSE ;
1525
1534
}
1526
1535
}
0 commit comments