MISRA C++:2023 Rule 8.7.1
Description
Rule Definition
Pointer arithmetic shall not form an invalid pointer. 1
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, or- strxfrm. For example, the- memcpyfunction 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
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.