You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 107 Next »

When two pointers are subtracted, both must point to elements of the same array object or just one past the last element of the array object (C Standard, 6.5.6 [ISO/IEC 9899:2011]); the result is the difference of the subscripts of the two array elements. Otherwise, the operation is undefined behavior. (See undefined behavior 48.)

Comparing pointers using the equality operators == and != has well-defined semantics regardless of whether or not either of the pointers is null, points into the same object, or points one past the last element of an array object or function.

Noncompliant Code Example

In this noncompliant code example, pointer subtraction is used to determine how many free elements are left in the nums array:

#include <stddef.h>
 
enum { SIZE = 32 };
 
void func(void) {
  int nums[SIZE];
  int end;
  int *next_num_ptr = nums;
  size_t free_elements;

  /* Increment next_num_ptr as array fills */

  free_elements = &end - next_num_ptr;
}

This program incorrectly assumes that the nums array is adjacent to the end variable in memory. A compiler is permitted to insert padding bits between these two variables or even reorder them in memory.

Compliant Solution

In this compliant solution, the number of free elements is computed by subtracting next_num_ptr from the address of the pointer past the nums array. While this pointer may not be dereferenced, it may be used in pointer arithmetic.

#include <stddef.h>
enum { SIZE = 32 };
 
void func(void) {
  int nums[SIZE];
  int *next_num_ptr = nums;
  size_t free_elements;

  /* Increment next_num_ptr as array fills */

  free_elements = &(nums[SIZE]) - next_num_ptr;
}

Exceptions

ARR36-C-EX1: Comparing two pointers to distinct members of the same struct object is allowed. Pointers to structure members declared later in the structure compare greater-than pointers to members declared earlier in the structure.

Risk Assessment

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

ARR36-C

Medium

Probable

Medium

P8

L2

Automated Detection

Tool

Version

Checker

Description

Astrée
24.04
pointer-subtractionPartially checked
Axivion Bauhaus Suite

7.2.0

CertC-ARR36Can detect operations on pointers that are unrelated
Coverity
2017.07

MISRA C 2004 17.2

MISRA C 2004 17.3

MISRA C 2012 18.2

MISRA C 2012 18.3

Implemented
LDRA tool suite
9.7.1

437 S, 438 S

Fully implemented

Parasoft C/C++test2023.1CERT_C-ARR36-a
CERT_C-ARR36-b

Do not subtract two pointers that do not address elements of the same array
Do not compare two unrelated pointers

Polyspace Bug Finder

R2024a

CERT C: Rule ARR36-C

Checks for subtraction or comparison between pointers to different arrays (rule partially covered)

PRQA QA-C
Unable to render {include} The included page could not be found.

0487, 0513, 2668, 2669, 2761,

2762, 2763, 2766, 2767, 2768,

2771, 2772, 2773

Fully implemented
PVS-Studio

7.33

V736, V782
RuleChecker
24.04
pointer-subtraction
Partially checked
TrustInSoft Analyzer

1.38

differing_blocks

Exhaustively verified (see the compliant and the non-compliant example).

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Key here (explains table format and definitions)

Taxonomy

Taxonomy item

Relationship

CERT CCTR54-CPP. Do not subtract iterators that do not refer to the same containerPrior to 2018-01-12: CERT: Unspecified Relationship
ISO/IEC TS 17961Subtracting or comparing two pointers that do not refer to the same array [ptrobj]Prior to 2018-01-12: CERT: Unspecified Relationship
CWE 2.11CWE-469, Use of Pointer Subtraction to Determine Size

2017-07-10: CERT: Exact

CWE 3.11CWE-469, Use of Pointer Subtraction to Determine Size2018-10-18:CERT:CWE subset of rule

CERT-CWE Mapping Notes

Key here for mapping notes

CWE-469 and ARR36-C

CWE-469 = Subset(ARR36-C)

ARR36-C = Union(CWE-469, list) where list =

  • Pointer comparisons using the relational operators <, <=, >=, and >, where the pointers do not refer to the same array

Bibliography



  • No labels