www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18117] New: ldiv_t struct in core.stdc.stdlib -- int vs

https://issues.dlang.org/show_bug.cgi?id=18117

          Issue ID: 18117
           Summary: ldiv_t struct in core.stdc.stdlib -- int vs c_long
                    expectations
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: minor
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: keith kanios.net

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.

--
Dec 23 2017