Skip to content

Commit 8414871

Browse files
committed
fix: memory leak
1 parent 1b26100 commit 8414871

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

brotli.c

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,6 @@ static zend_string *php_brotli_output_handler_load_dict(php_brotli_context *ctx)
442442
"request(%s) != actual(%s)",
443443
Z_STRVAL_P(available), ZSTR_VAL(b64));
444444
BROTLI_G(compression_coding) &= ~PHP_BROTLI_ENCODING_DCB;
445-
zend_string_free(dict);
446445
dict = NULL;
447446
}
448447
zend_string_free(b64);
@@ -451,7 +450,6 @@ static zend_string *php_brotli_output_handler_load_dict(php_brotli_context *ctx)
451450
php_error_docref(NULL, E_WARNING,
452451
"brotli: not found available-dictionary");
453452
BROTLI_G(compression_coding) &= ~PHP_BROTLI_ENCODING_DCB;
454-
zend_string_free(dict);
455453
dict = NULL;
456454
}
457455
}
@@ -469,23 +467,15 @@ static int php_brotli_output_handler_context_start(php_brotli_context *ctx)
469467
long level = BROTLI_G(output_compression_level);
470468
zend_string *dict = php_brotli_output_handler_load_dict(ctx);
471469
if (!BROTLI_G(compression_coding)) {
472-
if (dict) {
473-
zend_string_release(dict);
474-
}
475470
return FAILURE;
476471
}
477472

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);
489479
}
490480

491481
static int php_brotli_output_handler(void **handler_context,
@@ -830,34 +820,44 @@ static size_t php_brotli_decompress_read(php_stream *stream,
830820
size_t ret = 0;
831821
#else
832822
static ssize_t php_brotli_decompress_read(php_stream *stream,
833-
char *buf,
834-
size_t count)
823+
char *buf,
824+
size_t count)
835825
{
836826
ssize_t ret = 0;
837827
#endif
838828
STREAM_DATA_FROM_STREAM();
839829

840830
/* input */
841831
uint8_t *input = (uint8_t *)emalloc(PHP_BROTLI_BUFFER_SIZE);
832+
if (!input) {
833+
return 0;
834+
}
835+
842836
if (self->result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
843837
if (php_stream_eof(self->stream)) {
844838
/* corrupt input */
845-
if (input) {
846-
efree(input);
847-
}
839+
efree(input);
848840
#if PHP_VERSION_ID < 70400
849841
return 0;
850842
#else
851843
return -1;
852844
#endif
853845
}
854846
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+
}
856852
self->ctx.next_in = input;
857853
}
858854

859855
/* output */
860856
uint8_t *output = (uint8_t *)emalloc(count);
857+
if (!output) {
858+
efree(input);
859+
return 0;
860+
}
861861
self->ctx.available_out = count;
862862
self->ctx.next_out = output;
863863

@@ -891,12 +891,8 @@ static ssize_t php_brotli_decompress_read(php_stream *stream,
891891
}
892892
}
893893

894-
if (input) {
895-
efree(input);
896-
}
897-
if (output) {
898-
efree(output);
899-
}
894+
efree(input);
895+
efree(output);
900896

901897
return ret;
902898
}
@@ -1521,6 +1517,7 @@ static ZEND_FUNCTION(brotli_compress)
15211517
efree(buffer);
15221518
smart_string_free(&out);
15231519
php_error_docref(NULL, E_WARNING, "failed to compress");
1520+
php_brotli_context_close(&ctx);
15241521
RETURN_FALSE;
15251522
}
15261523
}

0 commit comments

Comments
 (0)