MISRA.STDLIB.ILLEGAL_REUSE.2012_AMD1The pointer returned by the Standard Library functions asctime and similar shall not be used following a subsequent call to the same function MISRA C 2012 Rule 21.20: 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 functionCategory: Mandatory Analysis: Undecidable, System Applies to: C90, C99 AmplificationCalls to setlocale may change the values accessible through a pointer that was previously returned by localeconv. For the purposes of this rule, the setlocale and localeconv function shall therefore be treated as if they are the same function. RationaleThe Standard Library functions asctime, ctime, gmtime, localtime, localeconv, getenv, setlocale and strerror return a pointer to an object within the Standard Library. Implementations are permitted to use static buffers for any of these objects and a second call to the same function may modify the contents of the buffer. The value accessed through a pointer held by the program before a subsequent call to a function may therefore change unexpectedly. Examplevoid 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" ); printf ( "%s\n", res1 ); /* Non-compliant - use after subsequent call */ printf ( "%s\n", copy ); /* Compliant - copy made before subsequent call */ printf ( "%s\n", res2 ); /* Compliant - no subsequent call before use */ } |