Skip to content

Commit f206ce9

Browse files
committed
fix Issue 18016 - using uniitialized value is considered @safe but has undefined behavior
1 parent 64ee618 commit f206ce9

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

spec/declaration.dd

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,18 +604,34 @@ $(GNAME VoidInitializer):
604604
$(P Normally, variables are initialized either with an explicit
605605
$(GLINK Initializer) or are set to the default value for the
606606
type of the variable. If the $(I Initializer) is $(D void),
607-
however, the variable is not initialized. If its value is
608-
used before it is set, undefined program behavior will result.
607+
however, the variable is not initialized.
609608
)
610609

611-
$(UNDEFINED_BEHAVIOR If a void initialized variable's value is
612-
used before it is set, the behavior is undefined.
610+
$(IMPLEMENTATION_DEFINED If a void initialized variable's value is
611+
used before it is set, the value is implementation defined.
613612

614613
---
614+
import std.stdio;
615615
void foo()
616616
{
617617
int x = void;
618-
writeln(x); // will print garbage
618+
writeln(x); // will likely print garbage
619+
}
620+
---
621+
)
622+
623+
$(UNDEFINED_BEHAVIOR If a void initialized variable is a reference type
624+
and its value is dereferenced before it is set, the behavior is undefined.
625+
Such initializations are not allowed in `@safe` code.
626+
627+
---
628+
void foo()
629+
{
630+
int* p = void;
631+
*p = 3; // undefined behavior
632+
int x;
633+
p = &x;
634+
*p = 3; // ok
619635
}
620636
---
621637
)

0 commit comments

Comments
 (0)