MISRA C:2012 Rule 7.4
A string literal shall not be assigned to an object unless the object’s type is “pointer to const-qualified char”
Description
Rule Definition
A string literal shall not be assigned to an object unless the object’s type is “pointer to const-qualified char”1 .
Rationale
This rule prevents assignments that allow modification of a string literal.
An attempt to modify a string literal can result in undefined behavior. For example, some implementations can store string literals in read-only memory. An attempt to modify the string literal can result in an exception or crash.
As an exception, passing a string literal to a variadic function through a variable argument list does not violate this rule.
Polyspace Implementation
The rule checker flags assignment of string literals to:
Pointers with data type other than
const char*
.Arrays with data type other than
const char
.
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: Literals and Constants |
Category: Required |
AGC Category: Required |
Version History
Introduced in R2014b
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.