MISRA C++:2023 Rule 25.5.2
The pointers returned by the C++ Standard Library functions
localeconv
, getenv
, setlocale
or
strerror
must only be used as if they have pointer to
const
-qualified type
Since R2024b
Description
Rule Definition
The pointers returned by the C++ Standard Library functions
localeconv
, getenv
, setlocale
or strerror
must only be used as if they have pointer to
const
-qualified type.
Rationale
The C++ standard library functions localeconv
,
getenv
, setlocale
or strerror
returns a pointer to a buffer:
localeconv
returns astruct lconv*
object, which points to a structure that includes variouschar*
pointers.getenv
,setlocale
andstrerror
returns achar*
pointer.
Modifying the pointer returned by these functions results in undefined
behavior. To avoid undefined behavior, assign the returned value of these functions to a
const
-qualified pointer. Any attempt to modify the
const
pointer is then detected by the compiler. Avoid using these
functions using function pointers.
If modification to the return values are required, make a non-const
copy of the const
-qualified object and then modify the copy.
Polyspace Implementation
Polyspace® reports a violation of this rule if you assign the output of these functions
to a non-const
pointer:
localeconv
,getenv
,setlocale
,strerror
A violation is also reported if you take the address of any of the preceding functions.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Localization library |
Category: Mandatory |
Version History
Introduced in R2024b