Skip to content

fix: memory leak #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 37 additions & 28 deletions brotli.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ static zend_string *php_brotli_output_handler_load_dict(php_brotli_context *ctx)
"request(%s) != actual(%s)",
Z_STRVAL_P(available), ZSTR_VAL(b64));
BROTLI_G(compression_coding) &= ~PHP_BROTLI_ENCODING_DCB;
zend_string_free(dict);
dict = NULL;
}
zend_string_free(b64);
Expand All @@ -451,7 +450,6 @@ static zend_string *php_brotli_output_handler_load_dict(php_brotli_context *ctx)
php_error_docref(NULL, E_WARNING,
"brotli: not found available-dictionary");
BROTLI_G(compression_coding) &= ~PHP_BROTLI_ENCODING_DCB;
zend_string_free(dict);
dict = NULL;
}
}
Expand All @@ -469,23 +467,15 @@ static int php_brotli_output_handler_context_start(php_brotli_context *ctx)
long level = BROTLI_G(output_compression_level);
zend_string *dict = php_brotli_output_handler_load_dict(ctx);
if (!BROTLI_G(compression_coding)) {
if (dict) {
zend_string_release(dict);
}
return FAILURE;
}

int result = php_brotli_context_create_encoder_ex(ctx,
level,
BROTLI_DEFAULT_WINDOW,
BROTLI_MODE_GENERIC,
dict,
0);
if (dict) {
zend_string_release(dict);
}

return result;
return php_brotli_context_create_encoder_ex(ctx,
level,
BROTLI_DEFAULT_WINDOW,
BROTLI_MODE_GENERIC,
dict,
0);
}

static int php_brotli_output_handler(void **handler_context,
Expand Down Expand Up @@ -830,34 +820,56 @@ static size_t php_brotli_decompress_read(php_stream *stream,
size_t ret = 0;
#else
static ssize_t php_brotli_decompress_read(php_stream *stream,
char *buf,
size_t count)
char *buf,
size_t count)
{
ssize_t ret = 0;
#endif
STREAM_DATA_FROM_STREAM();

/* input */
uint8_t *input = (uint8_t *)emalloc(PHP_BROTLI_BUFFER_SIZE);
if (!input) {
#if PHP_VERSION_ID < 70400
return 0;
#else
return -1;
#endif
}

if (self->result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
if (php_stream_eof(self->stream)) {
/* corrupt input */
if (input) {
efree(input);
}
efree(input);
#if PHP_VERSION_ID < 70400
return 0;
#else
return -1;
#endif
}
self->ctx.available_in = php_stream_read(self->stream, input,
PHP_BROTLI_BUFFER_SIZE );
PHP_BROTLI_BUFFER_SIZE);
if (!self->ctx.available_in) {
efree(input);
#if PHP_VERSION_ID < 70400
return 0;
#else
return -1;
#endif
}
self->ctx.next_in = input;
}

/* output */
uint8_t *output = (uint8_t *)emalloc(count);
if (!output) {
efree(input);
#if PHP_VERSION_ID < 70400
return 0;
#else
return -1;
#endif
}
self->ctx.available_out = count;
self->ctx.next_out = output;

Expand Down Expand Up @@ -891,12 +903,8 @@ static ssize_t php_brotli_decompress_read(php_stream *stream,
}
}

if (input) {
efree(input);
}
if (output) {
efree(output);
}
efree(input);
efree(output);

return ret;
}
Expand Down Expand Up @@ -1521,6 +1529,7 @@ static ZEND_FUNCTION(brotli_compress)
efree(buffer);
smart_string_free(&out);
php_error_docref(NULL, E_WARNING, "failed to compress");
php_brotli_context_close(&ctx);
RETURN_FALSE;
}
}
Expand Down
Loading