MISRAC2012-Rule-21.20
Synopsis
(Mandatory) The pointer returned by the Standard Library functions asctime, ctime, gmtime, localtime, localeconv, getenv, setlocale or strerror shall not be used following a subsequent call to the same function.
Enabled by default
Yes
Severity/Certainty
High/Medium

Full description
A pointer returned by asctime, ctime, gmtime, localtime, localeconv, getenv, setlocale or strerror is used after following a subsequent call to the same function.
Coding standards
- MISRA C:2012 Rule-21.20
(Mandatory) The pointer returned by the Standard Library functions asctime, ctime, gmtime, localtime, localeconv, getenv, setlocale or strerror shall not be used following a subsequent call to the same function
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
void f1( void )
{
const char *res1;
const char *res2;
char copy[ 128 ];
res1 = setlocale ( LC_ALL, 0 );
( void ) strcpy ( copy, res1 );
res2 = setlocale ( LC_MONETARY, "French" );
/* Non-compliant - use after subsequent call */
printf ( "%s\n", res1 );
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
void f1( void )
{
const char *res1;
const char *res2;
char copy[ 128 ];
res1 = setlocale ( LC_ALL, 0 );
( void ) strcpy ( copy, res1 );
res2 = setlocale ( LC_MONETARY, "French" );
/* Compliant - copy made before subsequent call */
printf ( "%s\n", copy );
/* Compliant - no subsequent call before use */
printf ( "%s\n", res2 );
}