Main Content

MISRA C++:2023 Rule 7.0.5

Integral promotion and the usual arithmetic conversions shall not change the signedness or the type category of an operand

Since R2024b

Description

Rule Definition

Integral promotion and the usual arithmetic conversions shall not change the signedness or the type category of an operand. 1

Rationale

Some conversions from signed to unsigned data types or change in type category can lead to implementation-defined behavior. You can see unexpected results from the conversion.

Polyspace Implementation

The checker flags:

  • Implicit conversions from a signed to an unsigned integer data type or vice versa.

    The checker assumes that ptrdiff_t is a signed integer.

  • Changes in type category.

    Based on the MISRA™ C++:2023 specifications, the rule checker classifies types into four categories:

    • Character: char, wchar_t, char16_t and char32_t

    • Integer: Signed and unsigned integer and unscoped enumeration types, including signed char and unsigned char

    • Floating point: float, double and long double

    • Other: bool, void, and nullptr_t

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

expand all

typedef char int8_t;
typedef unsigned char uint8_t;

void func()
  {
    int8_t s8;
    uint8_t u8;

    s8 = u8; //Noncompliant 
    u8 = s8 + u8; //Noncompliant
    u8 = static_cast< uint8_t > ( s8 ) + u8; //Compliant
}

In this example, the rule is violated when a variable with a variable with signed data type is implicitly converted to a variable with unsigned data type or vice versa. If the conversion is explicit, as in the preceding example, the rule violation does not occur.

Check Information

Group: Standard Conversions
Category: Required

Version History

Introduced in R2024b

expand all


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.