Skip to content

libpng: use of undefined value error with png_check_sig #9772

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

Closed
naeu opened this issue Sep 15, 2021 · 6 comments
Closed

libpng: use of undefined value error with png_check_sig #9772

naeu opened this issue Sep 15, 2021 · 6 comments
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@naeu
Copy link
Contributor

naeu commented Sep 15, 2021

.../cimport.zig:26019:100: error: use of undefined value here causes undefined behavior
pub inline fn png_check_sig(sig: anytype, n: anytype) @TypeOf(!(png_sig_cmp(sig, @as(c_int, 0), n) != 0)) {
                                                                                                   ^

Test case:

// file: main.zig
// zig build-exe main.zig -lpng -lc
const c = @cImport({
    @cInclude("png.h");
});
pub fn main() void {
    var buf: [100]u8 = undefined;
    _ = c.png_check_sig(buf[0..], 100);
}

Workaround:

fn png_check_sig(sig: [*]const u8, n: usize) c_int {
    return @boolToInt(c.png_sig_cmp(sig, 0, n) == 0);
}

zig version: 0.9.0-dev.959+f011f1393

@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. stage1 The process of building from source via WebAssembly and the C backend. labels Sep 15, 2021
@Vexu Vexu added this to the 0.10.0 milestone Sep 15, 2021
@sskras
Copy link

sskras commented Jan 21, 2022

If I get the error message right, the inline form is functionally equal to this:

fn png_check_sig(sig: anytype, n: anytype)  c_int {
    return !         (c.png_sig_cmp(sig, 0, n) != 0);
}

If we run unified diff between it and the workardound, the difference would be:

-    return !         (c.png_sig_cmp(sig, 0, n) != 0);
+    return @boolToInt(c.png_sig_cmp(sig, 0, n) == 0);

By looking at this and the label:"contributor friendly", as a potential contributor I wonder:
what needs to be done and would it be appropriate for a newbie?

Maybe the @boolToInt should just be inserted between the ! and the first ( in the original code, so it becomes:

    return !@boolToInt(c.png_sig_cmp(sig, 0, n) != 0);

?

@sskras
Copy link

sskras commented Mar 21, 2022

Maybe the @boolToInt should just be inserted between the ! and the first ( in the original code, so it becomes:

    return !@boolToInt(c.png_sig_cmp(sig, 0, n) != 0);

I see I got this very much wrong:

  1. ! will not work with integers;
  2. Even if something is to be "inserted" here, I meant doing this not figuratively but logically, probably on the parser level.

OK, back to the source:

  • C version, the originals:
int png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check);

#define png_check_sig(sig, n) !png_sig_cmp((sig), 0, (n))

The original prototype of png_check_sig(), now obsolete and converted into the aforementioned macro:

int png_check_sig(png_bytep arg0, int arg1);

It looks like both names should return int.

  • Zig version, autotranslated:
pub fn png_sig_cmp(sig: png_const_bytep, start: usize, num_to_check: usize) c_int;

pub inline fn png_check_sig(sig: anytype, n: anytype) @TypeOf(!(png_sig_cmp(sig, @as(c_int, 0), n) != 0)) {
    return !(png_sig_cmp(sig, @as(c_int, 0), n) != 0);
}

I think the original error message can be reduced to:

.../cimport.zig:26019:100: error: use of undefined value here causes undefined behavior

@TypeOf(!(png_sig_cmp(sig, @as(c_int, 0), n) != 0))
                                             ^

Now if I try to print the @typeName separately and use a constants in place of sig and n, Zig says the type is bool instead of c_int.

Still not sure what type should be returned by the translated function.

@david-vanderson
Copy link
Contributor

Looks like this issue got fixed somewhere between 0.10.0-dev.2431+0e6285c8f and 0.10.0-dev.4060+61aaef0b0 (which just happen to be two versions I have lying around)

@tau-dev
Copy link
Contributor

tau-dev commented Oct 6, 2022

So can this be closed?

@Vexu
Copy link
Member

Vexu commented Oct 6, 2022

It can be closed when a test is added.

@Vexu
Copy link
Member

Vexu commented Oct 12, 2022

The reduced version of this issue is trivial and doesn't reproduce on stage2 due to properly implementing #1947

test {
    const a: bool = undefined;
    _ = !a;
}

@Vexu Vexu closed this as completed Oct 12, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.10.0 Oct 12, 2022
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 contributor friendly This issue is limited in scope and/or knowledge of Zig internals. stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

6 participants