[Issue 20148] New: void initializated bool can be both true and false

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 20 22:21:12 UTC 2019


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

          Issue ID: 20148
           Summary: void initializated bool can be both true and false
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: safe
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: ag0aep6g at gmail.com

This is a spin-off from issue 19968.

This program can exhibit undefined behavior even `main` is @safe and `f` is
correctly @trusted:

----
void main() @safe
{
    bool b = void;
    f(b);
}
void f(bool cond) @trusted
{
    import core.stdc.stdlib: free, malloc;
    byte b;
    void* p = cond ? &b : malloc(1);
    if(!cond) free(p);
}
----

Typical output:
----
munmap_chunk(): invalid pointer
Error: program killed by signal 6
----

That means `free` is being called on `&b`. That operation has undefined
behavior. But that can only happen if `cond` is both true and false at the same
time.

Surely, an @trusted function should be allowed to assume that a bool is either
true or false, and not both.

--


More information about the Digitalmars-d-bugs mailing list