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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jun 6 09:21:34 UTC 2020


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

Patrick Schluter <Patrick.Schluter at ec.europa.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Patrick.Schluter at ec.europa.
                   |                            |eu

--- Comment #4 from Patrick Schluter <Patrick.Schluter at ec.europa.eu> ---
(In reply to hsteoh from comment #2)
> There's more to it than a hole in @safe.  Look at the disassembly below,
> there seems to be a codegen bug as well:
> 
> -------------------
> 000000000003f698 <_Dmain>:
>    3f698:       55                      push   %rbp
>    3f699:       48 8b ec                mov    %rsp,%rbp
>    3f69c:       48 83 ec 10             sub    $0x10,%rsp
>    3f6a0:       40 8a 7d f8             mov    -0x8(%rbp),%dil

The bug is here and only in dmd! 

gdb and ldc use movzx to load the EDI register no mov. When b is initialized
the error doesn't manifest as it reuses the EAX register to load EDI that it
had used to zero the byte.
This said. The example doesn't compile with option -O . It returns then

<source>(4): Error: variable b used before set


> 
> Even though technically this codegen works if b is either 0 or 1, it seems
> inconsistent at best (why compare the entire 32-bit value to 0 when checking
> for false, but only the lower byte when checking for true?), and in this
> case outright wrong when b is uninitialized and therefore can have any
> random garbage value other than 0 or 1.

This is C integer promotion rule. bool being really just an integral type with
2 values instead of being a real special thing (see Java for the drawbacks of
that).

--


More information about the Digitalmars-d-bugs mailing list