V591. Non-void function should return a value.

The analyzer detected a function that returns a random value. It might be an error.

Consider this sample:

int main (int argc, char** argv)
{
  ...
  printf("FINISH\r\n");
}

The main() function returns an integer number which is accepted by the calling process. If main() does not return a value explicitly, the calling process gets a nominally undefined value. This is the correct code:

int main (int argc, char** argv)
{
  ...
  printf("FINISH\r\n");
  return retCode;
}

A more interesting and dangerous case is when we deal with code of functions where an undefined value is returned only sometimes. Consider the following sample:

BOOL IsInterestingString(char *s)
{
  if (s == NULL)
    return FALSE;
  if (strlen(s) < 4)
    return;
  return (s[0] == '#') ? TRUE : FALSE;
}

There is a misprint in the code. If a string's length is less than 4 characters, the function will return an undefined value. This is the correct code:

BOOL IsInterestingString(char *s)
{
  if (s == NULL)
    return FALSE;
  if (strlen(s) < 4)
    return FALSE;
  return (s[0] == '#') ? TRUE : FALSE;
}

Note. The analyzer tries to determine cases when absence of a returned value is not an error. Here is an example of code analyzer will consider safe:

int Foo()
{
  ...
  exit(10);
}

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-393.

You can look at examples of errors detected by the V591 diagnostic.


Bugs Found

Checked Projects
409
Collected Errors
14 072
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site. Learn More →
Accept