[Issue 21504] New: Incorrect eponymous overload called by codegen

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Dec 26 16:28:55 UTC 2020


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

          Issue ID: 21504
           Summary: Incorrect eponymous overload called by codegen
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: safe, wrong-code
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: dlang-bugzilla at thecybershadow.net

See the following program. According to semantics (return value), we call one
overload, but codegen actually calls another:

//////////////////////////// test.d ///////////////////////////
@safe:

template toHex()
{
    char[] toHex(in ubyte[] data, char[] buf) pure
    {
        assert(false, "Completely irrelevant overload");
    }

    char[n*2] toHex(size_t n)(in ubyte[n] data) pure
    {
        char[n*2] buf;
        return buf;
    }

    string toHex(in ubyte[] data) pure
    {
        assert(false, "Should not be called"); // Is called
    }
}

void main()
{
    ubyte[40] hmacBytes;

    // Semantics thinks that this will call the second overload
    // (returning a static array),
    // but codegen actually calls the third overload!
    auto hmac = hmacBytes.toHex();

    // Yes, according to the type of the return value,
    // we called the second overload:
    static assert(is(typeof(hmac) == char[80]));
}
///////////////////////////////////////////////////////////////

This seems to further confuse the compiler, and cause invalid slices to be
passed to further consumers of the return value, leading to segfaults / memory
corruption.

--


More information about the Digitalmars-d-bugs mailing list