Bug 18117 – ldiv_t struct in core.stdc.stdlib -- int vs c_long expectations

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-12-24T00:24:14Z
Last change time
2022-03-26T22:40:06Z
Keywords
pull
Assigned to
No Owner
Creator
Keith Kanios
See also
https://issues.dlang.org/show_bug.cgi?id=22715

Comments

Comment #0 by keith — 2017-12-24T00:24:14Z
The ldiv_t struct in core.stdc.stdlib is seemingly incorrect: https://github.com/dlang/druntime/blob/master/src/core/stdc/stdlib.d#L53 Based on various Standard C Library documentation sources (e.g. http://www.cplusplus.com/reference/cstdlib/ldiv_t/), along with other functions in core.stdc.stdlib (such as atol), it would seem that the ldiv_t quot and rem members should be of c_long type instead of int. Actual: struct ldiv_t { int quot, rem; } Expected: struct ldiv_t { c_long quot, rem; } Reasoning: calling ldiv with a platform defined 64-bit c_long numerator of at least 2^32 and a denominator of 1 should return the numerator as the quotient and 0 as the remainder. With the current int type, the quotient may suffer 32-bit wrap-around, e.g. (2^32 / 1) turns into 0 instead of 2^32. Severity: While the actual C library implementations and generated D compiler code will give the expected result most of the time on modern platforms, this could lead to very subtle bugs.
Comment #1 by dlang-bot — 2022-03-26T19:12:11Z
@huglovefan created dlang/druntime pull request #3789 "fix Issue 18117 - ldiv_t struct in core.stdc.stdlib -- int vs c_long …" fixing this issue: - fix Issue 18117 - ldiv_t struct in core.stdc.stdlib -- int vs c_long expectations classic c_long mismatch https://github.com/dlang/druntime/pull/3789
Comment #2 by dlang-bot — 2022-03-26T22:40:06Z
dlang/druntime pull request #3789 "fix Issue 18117 - ldiv_t struct in core.stdc.stdlib -- int vs c_long …" was merged into master: - 35a4bcf385242ce82736db740cae54e0db87a237 by human: fix Issue 18117 - ldiv_t struct in core.stdc.stdlib -- int vs c_long expectations classic c_long mismatch https://github.com/dlang/druntime/pull/3789