Skip to content

Commit e5039cd

Browse files
committed
Fix potential memory leak if zstd_compress fails
This PR fixes a potential memory leak in the compression path. In the current implementation, if zstd_compress returns an error, rb_raise is called immediately. If rb_raise will be called, the subsequent ZSTD_freeCCtx(ctx) is never reached. This change ensures that ZSTD_freeCCtx(ctx) is called before raising the exception, preventing the context leak. Seems zstd_compress errors are rare in practice, this fix ensures safety for those edge cases.
1 parent 8ae07a1 commit e5039cd

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

ext/zstdruby/zstdruby.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ static VALUE rb_compress(int argc, VALUE *argv, VALUE self)
3030
char* output_data = RSTRING_PTR(output);
3131

3232
size_t const ret = zstd_compress(ctx, output_data, max_compressed_size, input_data, input_size, false);
33+
ZSTD_freeCCtx(ctx);
3334
if (ZSTD_isError(ret)) {
3435
rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(ret));
3536
}
3637
rb_str_resize(output, ret);
3738

38-
ZSTD_freeCCtx(ctx);
3939
return output;
4040
}
4141

0 commit comments

Comments
 (0)