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: incorrect handling of negative divisor in remainder operator #10176

Closed
1 task done
ehaas opened this issue Nov 19, 2021 · 0 comments · Fixed by #10179
Closed
1 task done

translate-c: incorrect handling of negative divisor in remainder operator #10176

ehaas opened this issue Nov 19, 2021 · 0 comments · Fixed by #10179
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@ehaas
Copy link
Sponsor Contributor

ehaas commented Nov 19, 2021

Remember to search before filing a new report

  • I searched for this bug and did not find it in the issue tracker, and furthermore, the title I used above will make this new bug report turn up in the search results for my query.

Zig Version

0.9.0-dev.1679+6cf8a49bb

Steps to Reproduce

Run zig translate-c on the following code:

#include <stdlib.h>
int main(void) {
   int x = 5 % -2;
   if (x != 1) abort();
}

And then zig run on the output

Expected Behavior

From the C standard: If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a. So in this case x should equal 1 and the program should exit normally.

Actual Behavior

Compile error:

./test.zig:1120:20: error: negative denominator
    var x: c_int = @rem(@as(c_int, 5), -@as(c_int, 2));

Also, if the divisor is a runtime value instead of a comptime-known one, the result is a panic at runtime.

@ehaas ehaas added the bug Observed behavior contradicts documented or intended behavior label Nov 19, 2021
@andrewrk andrewrk added the translate-c C to Zig source translation feature (@cImport) label Nov 20, 2021
@andrewrk andrewrk added this to the 0.9.0 milestone Nov 20, 2021
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

Successfully merging a pull request may close this issue.

2 participants