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

can't get errno when libc lstat returns -1 #11878

Closed
tw4452852 opened this issue Jun 17, 2022 · 5 comments
Closed

can't get errno when libc lstat returns -1 #11878

tw4452852 opened this issue Jun 17, 2022 · 5 comments
Labels
bug Observed behavior contradicts documented or intended behavior os-linux standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@tw4452852
Copy link
Contributor

tw4452852 commented Jun 17, 2022

Zig Version

0.9.1

Steps to Reproduce

const std = @import("std");
const c = @cImport({
    @cInclude("sys/stat.h");
});

pub fn main() anyerror!void {
    var stbuf: c.struct_stat = undefined;
    const ret = c.lstat("/no/exist/path", &stbuf);
    std.debug.print("ret = {}, c.errno = {}\n", .{ ret, std.c._errno().* });
}

Expected Behavior

The output should be: ret = -1, c.errno = 2.

Actual Behavior

But the actual output is ret = -1, c.errno = 0. The errno is not set correctly.

@tw4452852 tw4452852 added the bug Observed behavior contradicts documented or intended behavior label Jun 17, 2022
@Biolunar
Copy link
Contributor

I’ve just stumbled upon this bug aswell when I was using Zig to compile C++ code on Linux. It works correctly with musl as the target, but is broken with glibc. I’m on Zig version 0.10.0-dev.2674+d980c6a38

@tw4452852
Copy link
Contributor Author

tw4452852 commented Jun 26, 2022

I did some investigations on this issue and I think I found the culprit: zig has its own implementation of lstat here, so it has its own errno, but std.c._errno() gets external libc's errno which is different from it.

So this explains why we didn't get the correct errno when lstat.

@andrewrk andrewrk added os-linux standard library This issue involves writing Zig code for the standard library. labels Jun 28, 2022
@andrewrk andrewrk added this to the 0.10.0 milestone Jun 28, 2022
@konstin
Copy link

konstin commented Jul 13, 2022

This breaks using libgit2 with zig cc for me, since it relies on the lstat errno when creating the directory for cloning a repository (https://github.com/libgit2/libgit2/blob/1327dbcf2a4273a8ba6fd978db5f0882530af94d/src/util/futils.c#L527-L535)

@vrischmann
Copy link
Contributor

This breaks using libgit2 with zig cc for me, since it relies on the lstat errno when creating the directory for cloning a repository (https://github.com/libgit2/libgit2/blob/1327dbcf2a4273a8ba6fd978db5f0882530af94d/src/util/futils.c#L527-L535)

Pretty sure I'm experiencing the same problem with sqlite which uses ltstat/errno to validate the path of a database to open.

@rootbeer
Copy link
Contributor

This has been fixed. The given sample program passes (emits ret = -1, c.errno = 2) on a recent Zig (0.12.0-dev.3338+c85dd5c6d). Probably a dup of #17034 and fixed by the changes made for that in #17702.

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-linux standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

6 participants