Skip to main content

IAR Embedded Workbench for RISC-V 3.40

CERT-ARR30-C_h

In this section:
Synopsis

Do not form or use out-of-bounds pointers or array subscripts.

Enabled by default

Yes

Severity/Certainty

High/High

highhigh.png
Full description

Invalid pointer operations could lead to undefined behavior. These include forming an out-of-bounds pointer or array index, dereferencing a past-the-end pointer or array index, accessing or generating a pointer past flexible array member, and null pointer arithmetic.

Coding standards
CERT ARR30-C

Do not form or use out of bounds pointers or array subscripts

CWE 119

Improper Restriction of Operations within the Bounds of a Memory Buffer

CWE 120

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

CWE 121

Stack-based Buffer Overflow

CWE 123

Write-what-where Condition

CWE 124

Buffer Underwrite ('Buffer Underflow')

CWE 126

Buffer Over-read

CWE 127

Buffer Under-read

CWE 129

Improper Validation of Array Index

CWE 786

Access of Memory Location Before Start of Buffer

Code examples

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

#include<wchar.h>

#define MAX_COMPUTERNAME_LENGTH_FQDN 10
void GetMachineName(
    wchar_t *pwszPath,
    wchar_t wszMachineName[MAX_COMPUTERNAME_LENGTH_FQDN+1])
{
    wchar_t *pwszServerName = wszMachineName;
    wchar_t *pwszTemp = pwszPath + 2;
    while (*pwszTemp != L'\\')
        *pwszServerName++ = *pwszTemp++;
    /* ... */
}

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

#include<wchar.h>

#define MAX_COMPUTERNAME_LENGTH_FQDN 10
void GetMachineName(
    wchar_t *pwszPath,
    wchar_t wszMachineName[MAX_COMPUTERNAME_LENGTH_FQDN+1])
{
    wchar_t *pwszServerName = wszMachineName;
    wchar_t *pwszTemp = pwszPath + 2;
    wchar_t *end_addr
        = pwszServerName + MAX_COMPUTERNAME_LENGTH_FQDN;
    while ( (*pwszTemp != L'\\')
            &&  ((*pwszTemp != L'\0'))
            && (pwszServerName < end_addr) )
        {
            *pwszServerName++ = *pwszTemp++;
        }
 
    /* ... */
}