Skip to content

Commit b60d0f2

Browse files
authored
Merge pull request #137 from Watson1978/fix/skippable-frame-oob-read
Fix out-of-bounds read in Zstd.write_skippable_frame
2 parents d0adf3f + 9cb7f58 commit b60d0f2

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

ext/zstdruby/skippable_frame.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ static VALUE rb_write_skippable_frame(int argc, VALUE *argv, VALUE self)
1717

1818
StringValue(input_value);
1919
StringValue(skip_value);
20-
char* input_data = RSTRING_PTR(input_value);
2120
size_t input_size = RSTRING_LEN(input_value);
2221
char* skip_data = RSTRING_PTR(skip_value);
2322
size_t skip_size = RSTRING_LEN(skip_value);
2423

2524
size_t dst_size = input_size + ZSTD_SKIPPABLEHEADERSIZE + skip_size;
26-
VALUE output = rb_str_new(input_data, dst_size);
25+
VALUE output = rb_str_new(NULL, dst_size);
2726
char* output_data = RSTRING_PTR(output);
2827
size_t output_size = ZSTD_writeSkippableFrame((void*)output_data, dst_size, (const void*)skip_data, skip_size, magic_variant);
2928
if (ZSTD_isError(output_size)) {

spec/zstd-skippable_frame_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,13 @@
3030
end
3131
end
3232

33+
context 'large input (heap-allocated) + skippable frame' do
34+
it 'round-trips without breaking' do
35+
payload = 'A' * 1024
36+
skippable = 'sample data'
37+
frame = Zstd.write_skippable_frame(payload, skippable)
38+
expect(Zstd.read_skippable_frame(frame)).to eq skippable
39+
end
40+
end
3341
end
3442
end

0 commit comments

Comments
 (0)