D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4158 - switch segfault
Summary: switch segfault
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-04 19:49 UTC by Kyle Foley
Modified: 2015-06-09 05:15 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kyle Foley 2010-05-04 19:49:19 UTC
DMD 2.045

int main(string[] args)
{
	//switch (0) { } // segfaults
	switch (0) { case 0: }
	//switch (0) { case 1: } // segfaults
	switch (0) { case 1: default: }
	switch (0) { default: }

	return 0;
}

---

Seems to segfault when none of the labels inside the switch are jumped to.
Comment 1 Walter Bright 2010-05-04 23:44:21 UTC
It doesn't actually segfault, it executes a HALT instruction. If there is no default in a switch, the compiler inserts one of the form:

  default: HALT

The idea is that the default should not be implied as fall-through, which it does in C.

D is behaving as it is designed to.
Comment 2 Kyle Foley 2010-05-05 14:21:13 UTC
(In reply to comment #1)
> It doesn't actually segfault, it executes a HALT instruction. If there is no
> default in a switch, the compiler inserts one of the form:
> 
>   default: HALT
> 
> The idea is that the default should not be implied as fall-through, which it
> does in C.
> 
> D is behaving as it is designed to.

http://digitalmars.com/d/2.0/statement.html#SwitchStatement

"If none of the case expressions match, and there is not a default statement, a std.switcherr.SwitchError is thrown. The reason for this is to catch the common programming error of adding a new value to an enum, but failing to account for the extra value in switch statements. This behavior is unlike C or C++."

Should sync this with the docs.