Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@ehaas

Description

@ehaas

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviortranslate-cC to Zig source translation feature (@cImport)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions