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

[hlc] Fix SMOD/SDIV overflow exception when INT_MIN / -1 #11917

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/generators/hl2c.ml
Original file line number Diff line number Diff line change
Expand Up @@ -823,15 +823,15 @@ let generate_function ctx f =
| OSDiv (r,a,b) ->
(match rtype r with
| HUI8 | HUI16 | HI32 ->
sexpr "%s = %s == 0 ? 0 : %s / %s" (reg r) (reg b) (reg a) (reg b)
sexpr "%s = (%s == 0 || %s == -1) ? %s * %s : %s / %s" (reg r) (reg b) (reg b) (reg a) (reg b) (reg a) (reg b)
| _ ->
sexpr "%s = %s / %s" (reg r) (reg a) (reg b))
| OUDiv (r,a,b) ->
sexpr "%s = %s == 0 ? 0 : ((unsigned)%s) / ((unsigned)%s)" (reg r) (reg b) (reg a) (reg b)
| OSMod (r,a,b) ->
(match rtype r with
| HUI8 | HUI16 | HI32 | HI64 ->
sexpr "%s = %s == 0 ? 0 : %s %% %s" (reg r) (reg b) (reg a) (reg b)
sexpr "%s = (%s == 0 || %s == -1) ? 0 : %s %% %s" (reg r) (reg b) (reg b) (reg a) (reg b)
| HF32 ->
sexpr "%s = fmodf(%s,%s)" (reg r) (reg a) (reg b)
| HF64 ->
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/src/unitstd/haxe/Int32.unit.hx
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ c == 0xfffffffe;
-min == min; // two's complement overflow,
-2147483643 == 5 + -min; // order of ops and negate
2147483643 == -(5 + min); // static analyzer issue

#if hl
0 == min % 0; // % 0 div by zero exception
0 == Std.int(min / 0);
0 == min % -1; // min % -1 integer overflow exception
min == Std.int(min / -1);
min == min * -1;
0 == min % 1;
0 == max % 0;
0 == Std.int(max / 0);
0 == max % -1;
-max == Std.int(max / -1);
-max == max * -1;
0 == max % 1;
#end
Loading