MISRAC2012-Rule-21.19_b
In this section:
Synopsis
(Mandatory) The pointers returned by the Standard Library functions localeconv, getenv, setlocale or, strerror shall only be used as if they have pointer to const-qualified type.
Enabled by default
Yes
Severity/Certainty
High/High

Full description
The pointer returned by localeconv, getenv, setlocale or, strerror is assigned to a non-const type.
Coding standards
- MISRA C:2012 Rule-21.19
(Mandatory) The pointers returned by the Standard Library functions localeconv, getenv, setlocale or, strerror shall only be used as if the have pointer to const-qualified type
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
void example(void) {
char *s = getenv("MY_VAR");
*s = 'A';
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
#include <string.h>
void example(void) {
char *str = getenv("MY_VAR");
char *copy_of_str = (char *)malloc(strlen(str) + 1);
*copy_of_str = 'A';
}