Skip to content

Commit ca72f52

Browse files
authored
Merge pull request #158 from benmanns/streaming-write-malloc-gc-corruption
Streaming write malloc gc corruption
2 parents 24722ef + af06c34 commit ca72f52

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

‎ext/zstdruby/streaming_compress.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ rb_streaming_compress_flush(VALUE obj)
211211
VALUE drained = no_compress(sc, ZSTD_e_flush);
212212
VALUE out = rb_str_dup(sc->pending);
213213
rb_str_cat(out, RSTRING_PTR(drained), RSTRING_LEN(drained));
214+
/* prevent `drained` from being freed by GC inside rb_str_cat */
215+
RB_GC_GUARD(drained);
214216
rb_str_resize(sc->pending, 0);
215217
return out;
216218
}
@@ -223,6 +225,8 @@ rb_streaming_compress_finish(VALUE obj)
223225
VALUE drained = no_compress(sc, ZSTD_e_end);
224226
VALUE out = rb_str_dup(sc->pending);
225227
rb_str_cat(out, RSTRING_PTR(drained), RSTRING_LEN(drained));
228+
/* prevent `drained` from being freed by GC inside rb_str_cat */
229+
RB_GC_GUARD(drained);
226230
rb_str_resize(sc->pending, 0);
227231
return out;
228232
}

‎ext/zstdruby/zstdruby.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
132132
result = out;
133133
} else {
134134
rb_str_cat(result, RSTRING_PTR(out), RSTRING_LEN(out));
135+
RB_GC_GUARD(out);
135136
}
136137

137138
if (consumed == 0) {

0 commit comments

Comments
 (0)