Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.STDLIB.ILLEGAL_REUSE.2012_AMD1

The 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 function

Category: Mandatory

Analysis: Undecidable, System

Applies to: C90, C99

Amplification

Calls 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.

Rationale

The 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.

Example

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" );

  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 */ 
}