MISRA C++:2023 Rule 8.7.1
Description
Rule Definition
Pointer arithmetic shall not form an invalid pointer.
Rationale
When performing pointer arithmetic on a pointer to an array element, the resulting points is invalid if it does not point to one of these:
An element of the same array
One past the last element of the same array
Dereferencing an invalid pointer results in undefined behavior. This rule applies to these arithmetic operations:
Binary
+
,-
+=
,-=
Preincrement and postincrement
Predecrement and postdecrement
Indexing
Polyspace Implementation
Polyspace® reports a violation of this rule if any of these conditions is true:
Invalid pointer arithmetic operation — The result of pointer arithmetic on a pointer to an array element does not point to an element of the same array or one past the last element of the array.
Arithmetic operation on pointers to objects that are not arrays — You use pointer arithmetic on a nonarray type pointer, for example, using pointer arithmetic to traverse the fields of a structure.
Invalid argument for memory function — You use invalid arguments for a memory function such as
memchr
,memcmp
,memcpy
,memmove
,memset
,strncat
,strncmp
,strncpy
, orstrxfrm
. For example, thememcpy
function copies to an array that cannot accommodate the number of bytes copied.
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: Expressions |
Category: Required |
Version History
Introduced in R2024b