...
The type size_t
generally covers the entire address space. The C Standard, Annex K (normative), "Bounds-checking interfaces" [ISO/IEC 9899:2011], introduces introduces a new type, rsize_t
, defined to be size_t
but explicitly used to hold the size of a single object [Meyers 2004]. In code that documents this purpose by using the type rsize_t
, the size of an object can be checked to verify that it is no larger than RSIZE_MAX
, the maximum size of a normal single object, which provides additional input validation for library functions. See STR07-C. Use the bounds-checking interfaces for remediation of existing string manipulation code for additional discussion of C11 Annex K.
...
The unsigned n
may contain a value greater than INT_MAX
. Assuming quiet wraparound on signed overflow, the loop executes n
times because the comparison i < n
is an unsigned comparison. Once i
is incremented beyond INT_MAX
, i
takes on negative values starting with (INT_MIN)
. Consequently, the memory locations referenced by p[i]
precede the memory referenced by p
, and a write outside array bounds occurs.
...
In this noncompliant code example, the value of length
is read from a network connection and passed as an argument to a wrapper to malloc()
to allocate the appropriate data block. Provided that the size of an unsigned long
is equal to the size of an unsigned int
, and both sizes are equal to or smaller than the size of size_t
, this code runs as expected. However, if the size of an unsigned long
is greater than the size of an unsigned int
, the value stored in length
may be truncated when passed as an argument to alloc()
.
...
Declaring both length
and the blocksize
argument to alloc()
as rsize_t
eliminates the possibility of truncation. This compliant solution assumes that read_integer_from_network()
and read_network_data()
can also be modified to accept a length
argument of type pointer to rsize_t
and rsize_t
, respectively. If these functions are part of an external library that cannot be updated, care must be taken when casting length
into an unsigned long
to ensure that integer truncation does not occur.
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
|
| Can detect violations of this recommendation. In particular, it catches comparisons and operations where one operand is of type | |||||||
5.0 |
| Will detect integer operations that cause overflow but not all cases where | |||||||
| 93 S | Fully implemented | |||||||
|
|
|
...
CERT C++ Secure Coding Standard | INT01-CPP. Use rsize_t or size_t for all integer values representing the size of an object |
Bibliography
[ISO/IEC 9899:2011] | Annex K |
Bibliography
...