Bug 31977 - can return OK from "int main()" without a return statement by using a goto before the return
Summary: can return OK from "int main()" without a return statement by using a goto be...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.4.5
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-05-17 16:46 UTC by Brian McFadden
Modified: 2007-05-17 20:45 UTC (History)
3 users (show)

See Also:
Host: i386-redhat-linux
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
The file from -save-temps (3.35 KB, application/octet-stream)
2007-05-17 16:48 UTC, Brian McFadden
Details
the assembly (.s) file (356 bytes, application/octet-stream)
2007-05-17 16:52 UTC, Brian McFadden
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Brian McFadden 2007-05-17 16:46:18 UTC
I'm using gcc 3.4.5, and I was looking into a weird condition.  The following program compiles OK:

#include <stdio.h>
int alloc()
{
return 0;
}
int main(int argc, char* argv[])
{
       printf("Testing!\n");
       if(alloc() == 0)goto Radhe;
       return 0;
Radhe:
         printf("Hello\n");
}

The result is a program which prints "Testing", then "Hello", and then exits.  I and a few other people expect that the compiler would generate an error during compile, because main should return an int, and not all code paths have a return statement; however, both gcc and msvc are compiling this OK.

added note: this bug was recently discussed on the NTDEV mailing list.
Comment 1 Brian McFadden 2007-05-17 16:48:37 UTC
Created attachment 13571 [details]
The file from -save-temps
Comment 2 Brian McFadden 2007-05-17 16:52:05 UTC
Created attachment 13572 [details]
the assembly (.s) file
Comment 3 Brian McFadden 2007-05-17 16:54:36 UTC
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.5/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.5 20051201 (Red Hat 3.4.5-2)
Comment 4 Andrew Pinski 2007-05-17 18:53:08 UTC
[pinskia-laptop:~/src] pinskia% gcc -Wreturn-type t.c
t.c: In function 'main':
t.c:15: warning: control reaches end of non-void function


This code is only undefined at runtime which means you cannot error out.
Comment 5 Wolfgang Bangerth 2007-05-17 20:38:43 UTC
Both the C99 standard (section 5.1.2.2.3) and the C++ standard (3.6.1/5)
say that if you hit the end of the main() function, then this is equivalent
to a "return 0;" statement.

W.
Comment 6 Andrew Pinski 2007-05-17 20:45:09 UTC
(In reply to comment #5)
> Both the C99 standard (section 5.1.2.2.3) and the C++ standard (3.6.1/5)
> say that if you hit the end of the main() function, then this is equivalent
> to a "return 0;" statement.

C90 is different from both of those and it is just undefined behavior there.