Bug 53232 - No warning for main() without a return statement with -std=c99
Summary: No warning for main() without a return statement with -std=c99
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.7.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
: 70958 (view as bug list)
Depends on:
Blocks:
 
Reported: 2012-05-04 12:55 UTC by Vincent Lefèvre
Modified: 2024-01-12 00:42 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-08-28 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent Lefèvre 2012-05-04 12:55:49 UTC
On the following program, GCC (4.4 to 4.7 at least) invoked with -std=c99 -Wreturn-type doesn't give a warning about the missing return statement.

#include <stdio.h>

static int i = 0;

int main (void)
{
  if (i++ == 0)
    printf ("%d\n", main ());
}

According to the C99 rules, it seems that the return statement is optional only for program termination. Though this is ambiguous, someone else at least has the same interpretation as me:

  http://groups.google.com/group/comp.std.c/msg/c2f56fecfb699952

Before seeing this message, I posted

  http://groups.google.com/group/comp.std.c/browse_thread/thread/0187ef7b23bedf16

to comp.std.c (Subject: main function without a return statement in C99/C11).

Also, I think that the warning should be given in every case for the following reasons:
* A missing return statement may be unintentional (I think that implicit values like here should be discouraged in general, and that the C99 rule is there more to avoid undefined behavior than to save space).
* Compatibility with C90 and with freestanding environments.
* It is difficult to guarantee that main() will not be called from another C file.
Comment 1 Manuel López-Ibáñez 2012-05-04 13:44:02 UTC
Neither -Wmain warns about this... I think it is a bug, at least in the case of -Wmain.
Comment 2 Jonathan Wakely 2012-05-04 14:33:53 UTC
(In reply to comment #1)
> Neither -Wmain warns about this... I think it is a bug, at least in the case of
> -Wmain.

Why? -Wmain checks the type of main, not whether it has a redundant 'return 0;' as the last statement.
Comment 3 Manuel López-Ibáñez 2012-05-04 15:28:55 UTC
(In reply to comment #2)
> Why? -Wmain checks the type of main, not whether it has a redundant 'return 0;'
> as the last statement.

You are right, I misread the description. 

So this is not a bug because Wreturn-type says that it specifically does not check main?
Comment 4 Jonathan Wakely 2012-05-04 15:38:22 UTC
I think -Wreturn-type says in C++ it doesn't warn about declaring:
   main() {}
with no return type.  That only applies to C++, so isn't relevant here.

IMHO this isn't a bug because in C99 it's well-defined what happens if you fall off the end of main, so it would annoy people to issue a warning on perfectly valid code whenever -Wall is enabled.

But then I rarely use C99 so I don't really care.   If anyone suggested warning for C++ programs that fall off the end of main I would be upset :)
The fact people might call main() from other functions is a sort-of-convincing argument to warn about it (calling main isn't allowed in C++).
Comment 5 Vincent Lefèvre 2012-05-04 15:44:04 UTC
(In reply to comment #4)
> IMHO this isn't a bug because in C99 it's well-defined what happens if you fall
> off the end of main,

Only at program termination (if my interpretation of C99 is correct). Please read again the bug report and the links.

> so it would annoy people to issue a warning on perfectly valid code
> whenever -Wall is enabled.

Well, GCC has various warnings on perfectly valid code, just because the code is suspicious. This is the case here too. And there are potential portability problems (not everyone may use a C99 compiler). So, good reasons to issue a warning.
Comment 6 Jonathan Wakely 2012-05-04 16:09:40 UTC
(In reply to comment #5)
> (In reply to comment #4)
> > IMHO this isn't a bug because in C99 it's well-defined what happens if you fall
> > off the end of main,
> 
> Only at program termination (if my interpretation of C99 is correct). Please
> read again the bug report and the links.

I did. Please read the last sentence of comment 4.
 
> > so it would annoy people to issue a warning on perfectly valid code
> > whenever -Wall is enabled.
> 
> Well, GCC has various warnings on perfectly valid code, just because the code
> is suspicious. This is the case here too.

Yes, and in each case some people want it and some don't.  I'm pointing out to Manu the reasons not everyone wants the warning.  Your opinion isn't the only valid one.

> And there are potential portability
> problems (not everyone may use a C99 compiler). So, good reasons to issue a
> warning.

Er, if you want to find portability problems for people not using C99 then don't use -std=c99. Then -Wreturn-type warns about main.
Comment 7 Jonathan Wakely 2012-05-04 16:11:35 UTC
Changing to "enhancement" since the current behaviour is by design, not a bug.
Comment 8 Vincent Lefèvre 2012-05-04 19:58:01 UTC
(In reply to comment #6)
> Yes, and in each case some people want it and some don't.  I'm pointing out to
> Manu the reasons not everyone wants the warning.  Your opinion isn't the only
> valid one.

Then the solution would be to split -Wreturn-type into two different warnings.

> Er, if you want to find portability problems for people not using C99 then
> don't use -std=c99. Then -Wreturn-type warns about main.

There are several reasons one may want to use -std=c99, e.g. to be able to use C99 features when available (via autoconf and/or preprocessor tests).
Comment 9 Andrew Pinski 2021-08-28 19:07:58 UTC
Confirmed.
Comment 10 Andrew Pinski 2023-01-20 07:21:08 UTC
*** Bug 70958 has been marked as a duplicate of this bug. ***
Comment 11 Vincent Lefèvre 2023-01-20 13:33:19 UTC
(In reply to Vincent Lefèvre from comment #8)
> (In reply to comment #6)
> > Er, if you want to find portability problems for people not using C99 then
> > don't use -std=c99. Then -Wreturn-type warns about main.
> 
> There are several reasons one may want to use -std=c99, e.g. to be able to
> use C99 features when available (via autoconf and/or preprocessor tests).

In any case, there does not seem to be a -std value to say that the program must be valid for all C90, C99, C11 and C17 standards (and the future C23 standard). That's what portability is about.
Comment 12 Jonathan Wakely 2023-01-20 13:47:25 UTC
Because GCC is primarily a compiler, not a linter for portability problems.
Comment 13 Jakub Jelinek 2023-01-20 13:52:27 UTC
And intersection of all the standards is quite hard to define.
So, adding -std=intersection_of_all_c_standards is just not a good idea, any time same construct changes meaning between different standard you need to decide what to do.
This is something that should be solved with warnings or external tools and on the warning side we already have various warnings.
Comment 14 Vincent Lefèvre 2023-01-20 13:59:56 UTC
Anyway, as I said initially, the warning would be interesting even in C99+ mode, because the lack of a return statement may be unintentional. For instance, the developer may have forgotten a "return err;".
Comment 15 Jakub Jelinek 2023-01-20 14:01:33 UTC
But much more often it is intentional than unintentional.
Comment 16 Vincent Lefèvre 2023-01-20 14:29:41 UTC
(In reply to Jakub Jelinek from comment #15)
> But much more often it is intentional than unintentional.

That's the same thing for many kinds of warnings.
Comment 17 Jakub Jelinek 2023-01-20 14:31:32 UTC
Yeah, but warnings with high false positivity rates at least shouldn't be in -Wall.
Comment 18 Vincent Lefèvre 2023-01-20 15:10:47 UTC
(In reply to Jakub Jelinek from comment #17)
> Yeah, but warnings with high false positivity rates at least shouldn't be in
> -Wall.

Well, there already is -Wunused, which is included in -Wall (such warnings may typically be emitted due to #if and also in temporary code when debugging), and -Wsign-compare in C++.

Anyway, there is a first issue: the warning is inexistent, even with -Wextra. There is a second issue: the warning is not emitted with -Wreturn-type when there is a call to main(). Solving these two issues alone would not yield a high false positivity rate with -Wall. (That said, I think that developers should be encouraged to have an explicit "return" for main(); in particular, this is really easy to do and improves the code readability, specially knowing the difference of behavior with other languages, such as shell scripts and Perl.)