I have two tests for a function that passes in an allocator. I free the memory created by the function in the test. One of the tests complains about an Invalid free
, while the other works fine.
Here are the tests:
test "handles malformed utf8" {
const allocator = std.testing.allocator;
// This encodes "Xo’s" with a curly apostrophe
// \u00e2 is an invalid zig string, so we have to escape the backslash
const malformed = "Xo\\u00e2\\u0080\\u0099s";
const result = try fix_insta_json_unicode(allocator, malformed);
defer allocator.free(result);
try std.testing.expectEqualSlices(u8, "Xo’s", result);
}
test "does not remove non-unicode sequence backslash" {
const allocator = std.testing.allocator;
const text_with_backslash = "C:\\Windows\\Path";
const result = try fix_insta_json_unicode(allocator, text_with_backslash);
defer allocator.free(result);
try std.testing.expectEqualSlices(u8, "C:\\Windows\\Path", result);
}
And here’s the source to the fix_insta_json_unicode
function.
If I comment out the defer
line in the first test I get a memory address 0xXXXXXX leaked
error so I think it does need to be there.