0

I have a bunch of executables written in C that are statically analyzed with Polyspace Code Prover and Bug Finder. Both tools flag my main() functions for violation of MISRA's Guideline 8.4, with the following message:

"A compatible declaration shall be visible when an object or function with external linkage is defined. Function 'main' has no visible compatible prototype at definition."

Forward declaring main() seems to solve it, but that is very "weird" for me and it introduces problems when documenting the project with Doxygen.

Here's the function:

int main(int argument_counter, char const *arg_vector[])

also as you can see, we couldn't use the traditional argc and argv[] parameter names because they were too similar to some variables it found on the external headers, which is also superweird in my opinion.

Is this a code problem or is there something wrong with the tools configuration?

Mike
  • 3,513
  • 4
  • 14
  • 31
  • 2
    Does the tool still complain if you remove the `const` (https://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function)? – Mat Nov 27 '18 at 08:56
  • @Mat sorry for the delay in the response. Yes, my definition is now `SI_32 main(SI_32 argument_counter, CHAR_8 *arg_vector[])` and still get the alert. – Jorge Juan Torres Quiroga Nov 29 '18 at 07:06

2 Answers2

4

You often get these kind of false positives from static analysers regarding main, when you use an implementation-defined form. But notably, a strictly conforming hosted program shall use this form:

int main(int argc, char *argv[])

The name of the parameters doesn't matter, but their types do. char* [] is not the same type as const char* []. The const in your code doesn't mark the actual character arrays as const, but rather the array of pointers to them. Which is a bit weird, I don't really see why anyone would attempt to overwrite those.

Also notable, argc and argv must be writable in a strictly conforming program, C17 5.1.2.2.1 §2:

The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination

So you should ideally just change the types to be the ones required by a strictly conforming program.

However, many C programs are not strictly conforming hosted programs, so the static analyser must be able to swallow implementation-defined forms of main too. There's really no harm in forward declaring main either - and you are safe to assume that the compiler does not do so (C17 5.1.2.2.1 §1 "The implementation declares no prototype for this function.").

Suppose you have the implementation-defined form void main (void). To silence the tool you can simply write:

void main (void);
void main (void)
{ ...

I strongly suspect the reason for the tool warning is that it's too blunt to recognize that main is a special case. Similarly you can get warnings for using int as return value from main, instead of int32_t - which is a false positive, as MISRA-C has an explicit exception for the return type of main.

Lundin
  • 155,020
  • 33
  • 213
  • 341
  • Thanks for the detailed explanation. It seems reasonable then to forward declare our `main()`, I had never seen it done so it seemed weird at first. – Jorge Juan Torres Quiroga Nov 29 '18 at 07:12
  • 1
    @JorgeJuanTorresQuiroga It _is_ weird and shouldn't be needed for normal use. Only very special-purpose applications should need it: things like "C runtime" or bootloaders etc that are executed at startup and then call main(). – Lundin Nov 29 '18 at 07:40
2

main() is an exception to many rules, both within MISRA and without...

For the avoidance of doubt, MISRA C:2012 Technical Corrigendum 1 adds an explicit exception to Rule 8.4 for main():

The function main need not have a separate declaration.

Lundin
  • 155,020
  • 33
  • 213
  • 341
Andrew
  • 1,050
  • 14
  • 29
  • 1
    We have justified the violation for now, but if the standard itself acknowledges the exception then I think there should be some kind of configuration option in the tool. I will speak with the department that manages this, thank you! – Jorge Juan Torres Quiroga Dec 03 '18 at 14:45