MISRA C:2023 Rule 21.19
The pointers returned by the Standard Library functions
localeconv, getenv,
setlocale or strerror shall only be used as if
they have pointer to const-qualified type
Since R2024a
Description
Rule Definition
The pointers returned by the Standard Library functions
localeconv, getenv,
setlocale or strerror shall only be
used as if they have pointer to const-qualified
type.1
This rule comes from MISRA C™: 2012 Amendment 1.
Rationale
The C99 Standard states that if the program modifies the structure pointed to by the
value returned by localeconv, or the strings returned by
getenv, setlocale or strerro,
undefined behavior occurs. Treating the pointers returned by these functions as if they were
const-qualified allows an analysis tool to detect any attempt to modify
an object through one of the pointers. Assigning the return values of the functions to
const-qualified pointers results in the compiler issuing a diagnostic
if an attempt is made to modify an object.
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
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
| Group: Standard libraries |
| Category: Mandatory |
| AGC Category: Mandatory |
Version History
Introduced in R2024aSee Also
Check MISRA C:2023
(-misra-c-2023) | MISRA C:2023 Rule
7.4 | MISRA C:2023 Rule
11.8 | MISRA C:2023 Rule
21.8
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.