主要内容

CERT C: Rec. INT01-C

R2026b

Use rsize_t or size_t for all integer values representing the size of an object

Since R2026b

Description

Use rsize_t or size_t for all integer values representing the size of an object1

Polyspace Implementation

Polyspace checks for the issue Variable Not Declared as size_t or rsize_t

Examples

expand all

Issue

The issue occurs when a variable or expression that represents an object size is not declared as size_t or rsize_t. Polyspace® identifies a variable as requiring size_t or rsize_t typing when:

  • The variable is used as an array index.

  • The variable is passed as an argument to a function whose corresponding parameter is declared size_t or rsize_t.

Pointer arithmetic is not considered.

Once a variable is identified, the checker reports a violation when the identified variable is:

  • Not declared or explicitly cast to size_t or rsize_t.

  • Assigned from a function call whose return type is not size_t or rsize_t, and the return value is not explicitly cast.

  • Initialized through an implicit cast to size_t or rsize_t, unless the source is an immediate (literal) value.

  • Computed from an arithmetic or logical operation where an operand is not size_t or rsize_t. The right-hand side of shift operators is excluded from this check.

Risk

The type size_t covers the entire address space and is guaranteed sufficient precision to represent the size of any object. When a narrower type such as int or unsigned int is used instead, values exceeding that type's range overflow or truncate silently. This truncation can produce undersized allocations from functions such as malloc(), leading to buffer overflows. The type rsize_t provides the additional benefit of explicit bounds checking against RSIZE_MAX, limiting values to the maximum size of a single object.

Fix

Declare variables used as array indices or object sizes with type rsize_t or size_t. When interfacing with APIs that require a different integer type, use an explicit cast rather than relying on implicit conversion.

Example — Integer Variable Used as Array Index

In this example, Polyspace detects that the loop variable i is declared as int but used as an array index where size_t is appropriate.


#include <stdlib.h>
#include <stddef.h>

char *copy_string(size_t n, const char *src) {
    int i;  // Noncompliant
    char *dest = (char *)malloc(n);
    if (dest == NULL) {
        return NULL;
    }
    for (i = 0; i < n; ++i) {
        dest[i] = src[i];
    }
    return dest;
}

The variable i is used to index into arrays allocated with size n of type size_t. When n exceeds INT_MAX, the int variable overflows, causing undefined behavior.

Correction — Use rsize_t for Index Variable

Declare the index variable as rsize_t to match the size parameter type and prevent overflow.


#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>

typedef size_t rsize_t;
#define RSIZE_MAX (SIZE_MAX >> 1)

char *copy_string(rsize_t n, const char *src) {
    rsize_t i;  // Compliant
    char *dest;
    if (n == 0 || n > RSIZE_MAX) {
        return NULL;
    }
    dest = (char *)malloc(n);
    if (dest == NULL) {
        return NULL;
    }
    for (i = 0; i < n; ++i) {
        dest[i] = src[i];
    }
    return dest;
}

Check Information

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

Version History

Introduced in R2026b


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.