Main Content

CERT C: Rec. INT17-C

Define integer constants in an implementation-independent manner

Since R2026a

Description

Define integer constants in an implementation-independent manner1

Polyspace Implementation

The rule checker checks for the issue Implementation dependent hexadecimal constant.

Examples

expand all

Issue

This issue occurs when you declare a hexadecimal constant using either of these formats:

  • 4, 8, 16, or 32 F or f. For example, 0xFFFF or 0xffffffff.

  • An 8 followed by 3, 7, 15, or 31 zeros. For example: 0x8000 or 0x80000000.

Risk

The preceding usages indicate that the hexadecimal constant is a bit-mask that makes assumption about the length of signed or unsigned integer. The code behaves correctly only in platforms where the length of an integer matches the assumed length. In other platforms, the code behavior is incorrect.

Fix

Avoid using hardcoded hexadecimal constants. Use approaches that are portable.

Example — use of nonportable hardcoded hexadecimal constant

This code uses hardcoded hexadecimal constants, which are not portable. Polyspace® reports violations.

#include <stdio.h>

int main() {
    unsigned int value = 0xA5A5A5A5;
    unsigned int mask = 0xFFFFFFFF;   //Noncompliant
    unsigned int result;

    // Use bitmask to invert all bits
    result = value ^ mask;
    printf("Inverted value: 0x%X\n", result);

    // Use 0x80000000 to check if the MSB is set
    unsigned int msbCheck = 0x80000000;   //Noncompliant
    if ((value & msbCheck) == msbCheck) {
        //...
    } 
    return 0;
}

Correction

To fix the violations, avoid using hardcoded hexadecimal values. For example, to obtain a bit mask that has 1 for each bit regardless of the size of integer types, set the unsigned integer to -1. Similarly, to create a mask to identify the most significant bit of an unsigned integer, use a shifted UNIT_MAX.

#include <stdio.h>
#include <limits.h>
int main() {
    unsigned int value = 0xA5A5A5A5;
    const unsigned int mask = -1; //Compliant
    unsigned int result;
    unsigned int msbMask = UINT_MAX ^ (UINT_MAX >> 1);

    // Use bitmask to invert all bits
    result = value ^ mask;
    printf("Inverted value: 0x%X\n", result);

    // Use UINT_MAX to check if the MSB is set
    if (value & msbMask) {
        //...
    } 
    return 0;
}

Check Information

Group: Rec. 04. Integers (INT)
PQL Name: std.cert.INT17_C

Version History

Introduced in R2026a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.