Skip to content

Commit

Permalink
Fix Encode Leak
Browse files Browse the repository at this point in the history
This patch copies the slice that we encoded to a properly sized slice so
we can free the entire temporary one.
  • Loading branch information
gsquire committed Mar 28, 2024
1 parent 8cc10c7 commit 45c0be8
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions snappy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,11 @@ pub fn encode(allocator: Allocator, src: []u8) ![]u8 {
allocator.free(p);
}

return dst[0..d];
var output = try allocator.alloc(u8, d);
mem.copy(u8, output, dst[0..d]);
allocator.free(dst);

return output;
}

/// Return the maximum length of a snappy block, given the uncompressed length.
Expand Down Expand Up @@ -473,9 +477,19 @@ test "decoding variable integers" {
try testing.expect(case2.bytesRead == 3);
}

test "simple encode" {
const allocator = testing.allocator;

var input: [4]u8 = [_]u8{ 't', 'h', 'i', 's' };
var i: []u8 = &input;
var output = try encode(allocator, i);
defer allocator.free(output);

try testing.expectEqualSlices(u8, output, "\x04\x0cthis");
}

test "simple decode" {
// TODO: Use the testing allocator?
const allocator = std.heap.page_allocator;
const allocator = testing.allocator;

const decoded = try decode(allocator, "\x19\x1coh snap,\x05\x06,py is cool!\x0a");
defer allocator.free(decoded);
Expand Down

0 comments on commit 45c0be8

Please sign in to comment.