August 20, 2019 [Issue 20148] New: void initializated bool can be both true and false | ||||
---|---|---|---|---|
| ||||
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@puremagic.com Reporter: ag0aep6g@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. -- |
Copyright © 1999-2021 by the D Language Foundation