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

Translate-C doesn't understand that pointers can point to "one past the last element" of an array #22924

Open
drfuchs opened this issue Feb 17, 2025 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)

Comments

@drfuchs
Copy link

drfuchs commented Feb 17, 2025

Zig Version

0.14.0-dev.2851+b074fb7dd

Steps to Reproduce and Observed Behavior

The C standard allows pointers to point "one past the last element of an array object" for purposes of legal pointer arithmetic and comparison (though not dereferencing, of course). So, the following C code compiles and runs with no errors, warnings, or undefined behavior:

#include <stdio.h>

int main(void)
{
  const int foo[5] = {2, 3, 5, 7, 11};
  const int* end_foo = &foo[5]; // one past the end

  for (const int* ptr = foo; ptr < end_foo; ptr++)
    printf("%d ", *ptr);
  printf("\n");
  return 0;
}

However, translate-c produces zig code that then compiles with the error:

arybug.zig:328:47: error: index 5 outside array of length 5
    var end_primes: [*c]const c_int = &primes[@as(c_uint, @intCast(@as(c_int, 5)))];
                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expected Behavior

Resulting zig code should compile and run, producing a single line of output:
2 3 5 7 11

@drfuchs drfuchs added the bug Observed behavior contradicts documented or intended behavior label Feb 17, 2025
@alexrp alexrp added the translate-c C to Zig source translation feature (@cImport) 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 translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

No branches or pull requests

2 participants