www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19968] New: safe code can create invalid bools resulting in


          Issue ID: 19968
           Summary:  safe code can create invalid bools resulting in
                    memory corruption
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: tim.dlang t-online.de

Variables of type bool are expected to be 0 or 1. Using void initialization
this invariant can be wrong. This can result in memory corruption. See the
following example:

import std.stdio;

static int[2] data;
static int[253] data2;

void test(bool b)  safe

void main()  safe
        bool b = void;
        writeln(data, data2);
        writeln(data, data2);   

It can happen, that data2 is modified here.

See http://forum.dlang.org/post/brxxdumxnzktcjjjvedo forum.dlang.org for

Another problem is, that the code generation for !b assumes b is 0 or 1. The
following code shows, that bools can seem to be true and false at the same

import std.stdio;
void main()  safe
    bool b = void;
        writeln("b seems to be true");
        writeln("b seems to be false");

 trusted functions, that are correct for true and false may result in memory
corruption for invalid values.
See http://forum.dlang.org/reply/yrncciusmngbcxztnhyn forum.dlang.org for

Jun 15 2019