Skip to main content

IAR Embedded Workbench for RISC-V 3.40

PTR-arith-stack

In this section:
Synopsis

Pointer arithmetic applied to a pointer that references a stack address

Enabled by default

Yes

Severity/Certainty

Medium/High

mediumhigh.png
Full description

A pointer is assigned a stack-based address and then used in pointer arithmetic. This check is identical to MISRAC2004-17.1_b, MISRAC++2008-5-0-16_a.

Coding standards
CWE 120

Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')

MISRA C:2004 17.1

(Required) Pointer arithmetic shall only be applied to pointers that address an array or array element.

MISRA C++ 2008 5-0-16

(Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.

Code examples

The following code example fails the check and will give a warning:

void example(void) {
    int i;
    int *p = &i;
    p++;
    *p = 0;
}

The following code example passes the check and will not give a warning about this issue:

void example(void) {
    int i;
    int *p = &i;
    *p = 0;
}