[Issue 19968] @safe code can create invalid bools resulting in memory corruption

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jun 18 16:19:58 UTC 2019


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

--- Comment #10 from Tim <tim.dlang at t-online.de> ---
(In reply to Dlang Bot from comment #9)
> @WalterBright created dlang/dmd pull request #10055 "fix Issue 19968 - @safe
> code can create invalid bools resulting in me…" fixing this issue:
> 
> - fix Issue 19968 - @safe code can create invalid bools resulting in memory
> corruption
> 
> https://github.com/dlang/dmd/pull/10055

The pull request only fixes the specific example. Here is a new test case, that
is still affected:

import std.stdio;

static int[5] data;
static int[251] data2;

void test(bool b) @safe
{
        data[3 + b]++;
}

void main() @safe
{
        bool b = void;
        writeln(data, data2);
        test(b);
        writeln(data, data2);   
}

In this case value range propagation determines, that the expression 3 + b is
always in the range of indices for data. But since the type of 3 + b is not
bool anymore, the pull request does not prevent the memory corruption.

In my opinion, it would be better to prevent creating invalid bools in @safe
code.

--


More information about the Digitalmars-d-bugs mailing list