D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 14532 - switch block allows creating uninitialized variables
Summary: switch block allows creating uninitialized variables
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, pull, safe
: 3820 10524 16549 (view as issue list)
Depends on:
Blocks: 10524
  Show dependency treegraph
 
Reported: 2015-04-30 08:39 UTC by Ketmar Dark
Modified: 2017-08-02 08:07 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Ketmar Dark 2015-04-30 08:39:52 UTC
consider the code:

  void main () {
    import std.stdio;
    char ch = '!';
    switch (ch) {
      int n = 42;
      case '!': writeln(n, ": wow!"); break; //(1)
      default:
    }
  }

`n` variable is available at (1), but it is not initialized to 42, it contains random garbage instead.

compiler should either execute all code before first `case` label unconditionally, or forbid having unlabeled code in `switch` block.
Comment 1 yebblies 2015-04-30 13:02:47 UTC
This is the underlying issue in bug 10524 and is closely related to issue 602.
Comment 2 Walter Bright 2016-06-15 18:14:37 UTC
https://github.com/dlang/dmd/pull/5869
Comment 3 github-bugzilla 2016-06-17 01:37:16 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/1a26535fd627ca398d2609228bce20905440360d
fix Issue 14532 - switch block allows creating uninitialized variables

https://github.com/dlang/dmd/commit/039853c2a752c9bc3bef770610ac04cadbdeb51f
Merge pull request #5869 from WalterBright/fix14523

fix Issue 14532 - switch block allows creating uninitialized variables
Comment 4 Walter Bright 2016-06-18 21:03:55 UTC
*** Issue 10524 has been marked as a duplicate of this issue. ***
Comment 5 Martin Krejcirik 2016-09-18 14:00:29 UTC
*** Issue 3820 has been marked as a duplicate of this issue. ***
Comment 6 Mathias Lang 2016-09-27 15:32:10 UTC
*** Issue 16549 has been marked as a duplicate of this issue. ***
Comment 7 github-bugzilla 2016-10-01 11:47:50 UTC
Commits pushed to stable at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/1a26535fd627ca398d2609228bce20905440360d
fix Issue 14532 - switch block allows creating uninitialized variables

https://github.com/dlang/dmd/commit/039853c2a752c9bc3bef770610ac04cadbdeb51f
Merge pull request #5869 from WalterBright/fix14523
Comment 8 github-bugzilla 2017-08-02 08:07:18 UTC
Commit pushed to dmd-cxx at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/38c168303afcdf641877e31122106b2276ca642b
Issue 14532 - switch block allows creating uninitialized variables