Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Minor edits; reviewed

...

Some C implementations do not issue a diagnostic for the violation of this constraint. These noncompliant C translators continue to treat such declarations as implying the type int.

Compliant

...

Solution (Implicit int)

This compliant solution explicitly includes a type specifier:

...

If a function declaration is not visible at the point at which a call to the function is made, C90-compliant platforms assume an implicit declaration of

extern int funcidentifier();

This implies that the function may take any number and type of arguments and returns a single int.

...

Code Block
bgColor#FFCCCC
langc
/* #include <stdlib.h> is missing */
 
int main(void) {
  for (size_t i;
  for (i = 0; i < 100; ++i) {
    /* int malloc() assumed */
    char *ptr = (char *)malloc(0x10000000); /* int malloc() assumed */
    *ptr = 'a';
  }
  return 0;
}

When compiled with Microsoft Visual Studio (a C90-only platform) for a 64-bit platform, this noncompliant code example will eventually cause an access violation when dereferencing ptr in the loop.

...

Code Block
bgColor#ccccff
langc
#include <stdlib.h>
 
int main(void) {
  for (size_t i;
  for (i = 0; i < 100; ++i) {
    char *ptr = (char *)malloc(0x10000000); /* int malloc() assumed */
    *ptr = 'a';
  }
  return 0;
}

...

Do not declare a function with implicit an implicit return type. For example, if a function returns a meaningful integer value, declare it int. If it returns no meaningful value, declare it void.

...

Because the compiler assumes that foo() returns a value of type int for this noncompliant code example, UINT_MAX is incorrectly converted to −1.

Compliant Solution (Implicit Return Type)

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

...