Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using StackFallbackAllocator.get() multiple times is a footgun. #16344

Closed
IntegratedQuantum opened this issue Jul 7, 2023 · 0 comments · Fixed by #18275
Closed

Using StackFallbackAllocator.get() multiple times is a footgun. #16344

IntegratedQuantum opened this issue Jul 7, 2023 · 0 comments · Fixed by #18275
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@IntegratedQuantum
Copy link
Contributor

Zig Version

0.11.0-dev.3910+689f3163a

Steps to Reproduce and Observed Behavior

Calling StackFallbackAllocator.get() (re-)initializes the internal FixedBufferAllocator.
This means that calling StackFallbackAllocator.get() multiple times causes problematic behavior:

const std = @import("std");

pub fn main() !void {
	var allocator = std.heap.stackFallback(4096, std.heap.page_allocator);
	const a = try allocator.get().alloc(u8, 1);
	const b = try allocator.get().alloc(u8, 1);
	std.debug.assert(a.ptr != b.ptr);
}

This causes an assertion failure, meaning that both allocations returned the same pointer.
In actual code this is hard to debug because it leads to weird behavior.
For example in my code it messed up line wrapping and swallowed characters in text rendering.

Expected Behavior

The other allocators allow calling .allocator() multiple times without causing any trouble.
I think this should also apply to StackFallbackAllocator.get().

I think the internal FixedBufferAllocator should be initialized only once, preferably in the heap.stackFallback(..) function.

Note: This would be a breaking change, that's why I'm hesitant to implement this myself. Existing code might be relying on StackFallbackAllocator.get() to initialize the FixedBufferAllocator

@IntegratedQuantum IntegratedQuantum added the bug Observed behavior contradicts documented or intended behavior label Jul 7, 2023
@andrewrk andrewrk added the standard library This issue involves writing Zig code for the standard library. label Jul 22, 2023
@andrewrk andrewrk added this to the 0.14.0 milestone Jul 22, 2023
Vexu added a commit to Vexu/zig that referenced this issue Dec 14, 2023
@andrewrk andrewrk modified the milestones: 0.15.0, 0.12.0 Jan 10, 2024
bilaliscarioth pushed a commit to bilaliscarioth/zig that referenced this issue Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants