Thread overview
[Issue 3077] New: crash exiting main() without result code
Jun 23, 2009
Walter Bright
Jun 23, 2009
Walter Bright
Jun 23, 2009
Koroskin Denis
Sep 03, 2009
Walter Bright
June 18, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3077

           Summary: crash exiting main() without result code
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrei@metalanguage.com


int main(string[] args)
{
    try {} catch(Exception) { return 1; }
}

Running this program will cause a segmentation fault. Adding a return 0; at the end fixes it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 23, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3077


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com




--- Comment #1 from Walter Bright <bugzilla@digitalmars.com>  2009-06-23 11:05:23 PDT ---
I get the message:

core.exception.AssertError@test.d(4): missing return expression

Is that different from the one you get?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 23, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3077





--- Comment #2 from Andrei Alexandrescu <andrei@metalanguage.com>  2009-06-23 11:08:15 PDT ---
(In reply to comment #1)
> I get the message:
> 
> core.exception.AssertError@test.d(4): missing return expression
> 
> Is that different from the one you get?

Compile with -O -release -inline

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 23, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3077


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




--- Comment #3 from Walter Bright <bugzilla@digitalmars.com>  2009-06-23 13:15:25 PDT ---
If main() was typed to return a void, as a special case then the compiler will
insert a:
   return 0;
statement at the end.

But if a function is typed to return an int, then that's a contract saying the
programmer must put in some sort of return statement. If there is none, the
compiler inserts a:
   assert(0, "missing return expression");
at the end.

If you compile with -w, the compiler will warn you about this. (The compiler doesn't always do this because it is perfectly legal for a function that is typed to return an int to never return, such as if it calls abort().)

When compiling in release mode, assert(0) is replaced with a HLT instruction, which generates a hardware exception at runtime. This is what you're seeing.

It's expected behavior. Not a bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 23, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3077


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |




--- Comment #4 from Andrei Alexandrescu <andrei@metalanguage.com>  2009-06-23 15:57:19 PDT ---
(In reply to comment #3)
> If main() was typed to return a void, as a special case then the compiler will
> insert a:
>    return 0;
> statement at the end.
> 
> But if a function is typed to return an int, then that's a contract saying the
> programmer must put in some sort of return statement. If there is none, the
> compiler inserts a:
>    assert(0, "missing return expression");
> at the end.
> 
> If you compile with -w, the compiler will warn you about this. (The compiler doesn't always do this because it is perfectly legal for a function that is typed to return an int to never return, such as if it calls abort().)
> 
> When compiling in release mode, assert(0) is replaced with a HLT instruction, which generates a hardware exception at runtime. This is what you're seeing.
> 
> It's expected behavior. Not a bug.

Thanks for the explanation. It does not do the least in making things better. Bad behavior is bad behavior, period. I am reopening the bug. This is important. It cost me and it could cost anyone precious time.

Crashing at runtime without file and line information is the least effective of all ways to signal that the programmer has a bug in the program, by a large margin.

Allowing a function to compile without a proper return is unreasonable. The diagnostic and refusal to generate code should not be relegated to -w. That state of affairs originates in the days when dmd's had much worse flow checking so it would signal a lot of false errors. Now dmd has good enough control flow checking to penalize all missing returns with a compile-time error, at the cost of requiring the occasional unnecessary insertion of an unreachable return statement.

By this bug report I am requesting to move the "function has no return statement" diagnostic from -w mode to regular mode.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 23, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3077


Koroskin Denis <2korden@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |2korden@gmail.com
           Severity|normal                      |enhancement




-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 03, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3077


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED




--- Comment #5 from Walter Bright <bugzilla@digitalmars.com>  2009-09-03 13:39:14 PDT ---
Fixed dmd 2.032

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------