Skip to content

Commit 30fd4ff

Browse files
committed
fix: memory leak
1 parent 1b26100 commit 30fd4ff

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

brotli.c

Lines changed: 37 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,56 @@ 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+
#if PHP_VERSION_ID < 70400
834+
return 0;
835+
#else
836+
return -1;
837+
#endif
838+
}
839+
842840
if (self->result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
843841
if (php_stream_eof(self->stream)) {
844842
/* corrupt input */
845-
if (input) {
846-
efree(input);
847-
}
843+
efree(input);
848844
#if PHP_VERSION_ID < 70400
849845
return 0;
850846
#else
851847
return -1;
852848
#endif
853849
}
854850
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+
}
856860
self->ctx.next_in = input;
857861
}
858862

859863
/* output */
860864
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+
}
861873
self->ctx.available_out = count;
862874
self->ctx.next_out = output;
863875

@@ -891,12 +903,8 @@ static ssize_t php_brotli_decompress_read(php_stream *stream,
891903
}
892904
}
893905

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

901909
return ret;
902910
}
@@ -1521,6 +1529,7 @@ static ZEND_FUNCTION(brotli_compress)
15211529
efree(buffer);
15221530
smart_string_free(&out);
15231531
php_error_docref(NULL, E_WARNING, "failed to compress");
1532+
php_brotli_context_close(&ctx);
15241533
RETURN_FALSE;
15251534
}
15261535
}

0 commit comments

Comments
 (0)