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.
1
Rationale
The C++ standard library functions localeconv,
getenv, setlocale or strerror
returns a pointer to a buffer:
localeconvreturns astruct lconv*object, which points to a structure that includes variouschar*pointers.getenv,setlocaleandstrerrorreturns 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
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.