ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.76k stars 2.54k forks source link

`translate-c`: negative pointer to void #18543

Open Rexicon226 opened 9 months ago

Rexicon226 commented 9 months ago

Take this C for example:

#include <stdio.h>

int main() {
    void *x = (void *)-1;
    printf("ptr: %p\n", x);
}

when compiled with gcc the result is:

ptr: 0xffffffffffffffff

however, when compiled with translate-c

out.zig:295:55: error: type 'usize' cannot represent integer value '-1'
    var x: ?*anyopaque = @as(?*anyopaque, @ptrFromInt(-@as(c_int, 1)));

the solution would be to use -% wrapping negation for the -1 I guess.

Rexicon226 commented 9 months ago

If you wonder about real world uses, I stumbled upon this in the most unlikely place ever. mmap. Their MAP_FAILED is literally a -1 void pointer😭

here