Skip to content

Fix#22

Closed
mstdokumaci wants to merge 7 commits intozigcc:mainfrom
mstdokumaci:fix
Closed

Fix#22
mstdokumaci wants to merge 7 commits intozigcc:mainfrom
mstdokumaci:fix

Conversation

@mstdokumaci
Copy link
Copy Markdown
Contributor

No description provided.

@mstdokumaci mstdokumaci closed this Apr 2, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves memory safety in clonePayload by ensuring that partially cloned arrays and maps are properly cleaned up if an error occurs during the cloning process. It also updates pointer-to-integer conversions in tests to use @intFromPtr. Feedback was provided regarding the new tests, noting that FixedBufferAllocator may not reliably detect double-free issues, and suggesting the use of an allocator with double-free detection or address sanitizers for more robust verification.

Comment on lines +3640 to +3662
test "clonePayload map partial fail path frees partially cloned entries" {
var src = Payload.mapPayload(std.heap.page_allocator);
defer src.free(std.heap.page_allocator);

try src.mapPut("k1", try Payload.strToPayload("v1", std.heap.page_allocator));
try src.mapPut("k2", try Payload.strToPayload("very-long-string-to-force-out-of-memory-on-clone-because-the-fixed-buffer-is-too-small-to-allocate-this-large-string-during-the-cloning-operation", std.heap.page_allocator));

var buffer: [@sizeOf(Payload) * 2 + 16]u8 = undefined;
var pool = std.heap.FixedBufferAllocator.init(&buffer);
const clone_alloc = pool.allocator();

const result = src.deepClone(clone_alloc);
if (result) |cloned| {
cloned.free(clone_alloc);
try std.testing.expect(false);
} else |err| {
try std.testing.expect(err == error.OutOfMemory);
}

// Verify post-failure allocator state is valid: next alloc must succeed.
const next_alloc = try clone_alloc.alloc(u8, 16);
clone_alloc.free(next_alloc);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This test may not be sufficient to detect the double-free issue present in the clonePayload implementation for maps. The std.heap.FixedBufferAllocator does not detect double-frees, so it's possible for the double-free to occur without causing the subsequent allocation to fail, leading to a false positive test result.

To reliably catch this kind of memory safety issue, you would typically need to run tests with an allocator that has double-free detection or use tools like the address sanitizer (-fsanitize=address).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant