Skip to content

Commit 78357af

Browse files
committed
blog post
1 parent ecad780 commit 78357af

4 files changed

Lines changed: 8 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ CLAUDE.md
77
*.swp
88
*.swo
99
*~
10+
*.tar

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@ Automatic memory protection for sensitive strings
386386
- **Functional programming**: Callback-based access patterns to contain scope
387387
of data access
388388

389+
## Zecrecy Development Log
390+
391+
- [Testing Secure Zeroization in Zig with Custom Memory Allocators](https://eligrubb.com/notes/2025/til-zig-custom-memory-allocator/).
392+
389393
## License
390394

391395
MIT License - see [LICENSE](LICENSE) for details.

src/secret.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ pub fn Secret(comptime T: type) type {
117117
pub fn deinit(secret: *SecretType) void {
118118
secureZero(T, secret.data);
119119
// use rawFree instead of free to support verification of memory zeroization in testing
120+
if (secret.data.len == 0) return;
120121
secret.allocator.rawFree(secret.data, .fromByteUnits(@alignOf(T)), @returnAddress());
121122
secret.data = undefined;
122123
secret.allocator = undefined;
@@ -310,6 +311,7 @@ pub fn SecretUnmanaged(comptime T: type) type {
310311
pub fn deinit(secret: *SecretType, allocator: mem.Allocator) void {
311312
secureZero(T, secret.data);
312313
// use rawFree instead of free to support verification of memory zeroization in testing
314+
if (secret.data.len == 0) return;
313315
allocator.rawFree(secret.data, .fromByteUnits(@alignOf(T)), @returnAddress());
314316
secret.data = undefined;
315317
secret.* = undefined;

src/testing/ZerosOnlyAllocator.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn remap(ctx: *anyopaque, memory: []u8, alignment: mem.Alignment, new_len: usize
5656
fn free(ctx: *anyopaque, buf: []u8, alignment: mem.Alignment, ret_addr: usize) void {
5757
const self: *ZerosOnlyAllocator = @ptrCast(@alignCast(ctx));
5858
for (buf) |byte| {
59-
if (byte != 0) unreachable;
59+
if (byte != 0) @panic("memory not zeroed before freeing");
6060
}
6161
self.child_allocator.rawFree(buf, alignment, ret_addr);
6262
}

0 commit comments

Comments
 (0)