[rcore] GetCurrentMonitor(), fix for integer overflow in distance calculation#5842
Conversation
|
@Vasilis-Narain I would prefer to avoid the usage of |
GetCurrentMonitor(), fix for integer overflow in distance calculation
|
@raysan5 i tried to think of various convoluted ways to check for overflow to explicitly wrap since wrapping in this case is acceptable behaviour... Edit: Thinking some more this could theoretically still choose the wrong monitor if |
|
@Vasilis-Narain thanks for the further review, looks good enough to me! |
Problem
GetCurrentMonitor()inrcore_desktop_glfw.ccomputesint dist = dx*dx + dy*dywheredx,dyare pixel offsets from window centre to monitor corner. On my setup (multi-monitor WSL2 through Windows 11) the squared sum exceedsINT_MAX, causing signed integer overflow UB. Today GCC wraps silently, but UBSan-instrumented builds (eg. Zig or clang with-fsanitize=undefined) fail.Environment
Repro
Originally caught by Zig 0.16's UBSan trapping on the bundled raylib build:
Reproduced standalone by adding a
printfnext to the overflow site and runningexamples/core/core_basic_window:prod(computed as(long long)dx*dx + (long long)dy*dy) exceedsINT_MAX, confirming the truncation.Fix
Promote
distandclosestDisttolong long. No behavior change for monitors withinINT_MAXdistance and correctly handles cases beyond it without UB.