C and C++


LANG.STRUCT.CUP : Comparison of Unrelated Pointers

Summary

Pointers to different objects are compared.

Pointer comparison should only be performed between pointers that point at different elements of the same array. Comparing pointers (using <, <=, >, >=) that point at different variables or allocations is undefined behavior according to the C and C++ standards.

Properties

Class Name Comparison of Unrelated Pointers
Significance reliability
Mnemonic LANG.STRUCT.CUP
Categories
MisraC2023 MisraC2023:18.3 The relational operators >, >=, < and <= shall not be applied to expressions of pointer type except where they point into the same object
Misra2012 Misra2012:18.3 The relational operators >, >=, < and <= shall not be applied to expressions of pointer type except where they point into the same object
Misra2004 Misra2004:17.3 >, >=, <, <= shall not be applied to pointer types except where they point to the same array
AUTOSARC++14 AUTOSARC++14:M5-0-18 >, >=, <, <= shall not be applied to objects of pointer type, except where they point to the same array.
MisraC++2008 MisraC++2008:5-0-18 > , >= , < , <= shall not be applied to objects of pointer type, except where they point to the same array.
MisraC++2023 MisraC++2023:8.9.1 The built-in relational operators >, >=, < and <= shall not be applied to objects of pointer type, except where they point to elements of the same array
TS17961 TS17961:5.35-ptrobj 5.35. Subtracting or comparing two pointers that do not refer to the same array
CERT-C CERT-C:ARR36-C Do not subtract or compare two pointers that do not refer to the same array
  CERT-C:EXP08-C Ensure pointer arithmetic is used correctly
CERT-CPP CERT-CPP:CTR54-CPP Do not subtract iterators that do not refer to the same container
JSF++ JSF++:171 Relational operators shall not be applied to pointer types except where both operands are of the same type and point to the same entity.
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Comparison of Unrelated Pointers"

Example

int lang_struct_cup()
{
    int i, j;
    return &i < &j;  /* 'Comparison of Unrelated Pointers' warning issued here */
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.