CXX.STDLIB.ILLEGAL_WRITE

The CXX.STDLIB.ILLEGAL_WRITE checker reports a defect when pointers returned by getenv(), setlocale(), localeconv(), asctime(), or strerror() are not constant qualified pointers.

Vulnerability and risk

When objects pointed to by a call to the above functions are modified by the program, it can result in undefined behavior. Compilers do not report this issue because there is not a constraint violation.

Mitigation and prevention

The pointers returned by getenv(), setlocale(), localeconv(), asctime(), or strerror() should be assigned to constant qualified pointers. The compiler or analysis tool will report an issue if the code tries to modify the pointer assigned to a constant qualified pointer.

Vulnerable code example

1  void f()
2  {
3      char *s = setlocale( LC_ALL, 0 );      /*  Non-compliant  */
4      struct lconv *t = localeconv();        /*  Non-compliant  */
5      s[1] = 'A';                            /*  Undefined Behavior  */
6      t->decimal_point = "^";                /*  Undefined Behavior  */
7  }

In this example, the code does not assign returned pointers to const qualified pointers.

Fixed code example

1  void f()
2  {
3      char s [64];
4      char *t = setlocale( LC_ALL, 0 );
5      (void) strcpy( s, t );                        /*  Compliant  */
6      (void) strcpy( s, setlocale( LC_ALL, 0 ) );   /*  Compliant  */
7  }

In this fixed example, the pointers returned by calling setlocale() are assigned to const qualified pointers. The second parameter of strcpy() takes constant char *.

Related checkers

  • MISRA.STDLIB.ILLEGAL_WRITE.2012_AMD1