Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In this example, the function func() incorrectly returns a pointer to a local stack variable.

Code Block
bgColor#FFCCCC
char * func() {
   char a[10] ;
   /* ... Operate on a[0] */
   return &a[0];
}

Compiling with appropriate warning levels should generate a warning when a local stack variable is returned from a function.

...

Correcting this example depends on the intent of the programmer. If the intent is to modify the value of a0 and have it available outside of func(), then a can be declared elsewhere and passed as an parameter to func().

Code Block
bgColor#ccccff
void func(char a[]) {
   /* Operate on a[0] */
   return;
}

Risk Assessment

...