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

std.fs.wasi.preopensAlloc requires an arena, but it claims to want a gpa #22911

Open
squeek502 opened this issue Feb 16, 2025 · 0 comments
Open
Labels
bug Observed behavior contradicts documented or intended behavior os-wasi standard library This issue involves writing Zig code for the standard library.

Comments

@squeek502
Copy link
Collaborator

squeek502 commented Feb 16, 2025

Zig Version

0.14.0-dev.3224+5ab511307

Steps to Reproduce and Observed Behavior

std.fs.wasi.preopensAlloc returns a names slice that is a mixture of string literals and heap-allocated strings:

zig/lib/std/fs/wasi.zig

Lines 26 to 54 in ddff1fa

pub fn preopensAlloc(gpa: Allocator) Allocator.Error!Preopens {
var names: std.ArrayListUnmanaged([]const u8) = .empty;
defer names.deinit(gpa);
try names.ensureUnusedCapacity(gpa, 3);
names.appendAssumeCapacity("stdin"); // 0
names.appendAssumeCapacity("stdout"); // 1
names.appendAssumeCapacity("stderr"); // 2
while (true) {
const fd = @as(wasi.fd_t, @intCast(names.items.len));
var prestat: prestat_t = undefined;
switch (wasi.fd_prestat_get(fd, &prestat)) {
.SUCCESS => {},
.OPNOTSUPP, .BADF => return .{ .names = try names.toOwnedSlice(gpa) },
else => @panic("fd_prestat_get: unexpected error"),
}
try names.ensureUnusedCapacity(gpa, 1);
// This length does not include a null byte. Let's keep it this way to
// gently encourage WASI implementations to behave properly.
const name_len = prestat.u.dir.pr_name_len;
const name = try gpa.alloc(u8, name_len);
errdefer gpa.free(name);
switch (wasi.fd_prestat_dir_name(fd, name.ptr, name.len)) {
.SUCCESS => {},
else => @panic("fd_prestat_dir_name: unexpected error"),
}
names.appendAssumeCapacity(name);
}

This means that there is no safe way to fully free the returned value (since freeing the string literals is illegal behavior), so an arena must be used in order to avoid leaking the heap-allocated slices. However, the Allocator parameter is erroneously named gpa.

Note: All existing usage sites of this function do use an arena when calling this function.

Expected Behavior

Should be updated to work with a gpa or documented to require an arena.

@squeek502 squeek502 added bug Observed behavior contradicts documented or intended behavior os-wasi labels Feb 16, 2025
@alexrp alexrp added the standard library This issue involves writing Zig code for the standard library. label Feb 17, 2025
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 os-wasi standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

2 participants