...
Note that this code does not prevent wrapping unless the unsigned long long type is at least 4 bits larger than size_t.
Noncompliant Code Example (size_t)
The mbstowcs() function converts a multibyte string to a wide character string, returning the number of characters converted. If an invalid multibyte character is encountered, mbstowcs() returns (size_t)(-1). Depending on how size_t is implemented, comparing the return value of mbstowcs() to signed integer literal -1 may not evaluate as expected.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdlib.h>
void func(wchar_t *pwcs, const char *restrict s, size_t n) {
size_t count_modified = mbstowcs(pwcs, s, n);
if (count_modified == -1) {
/* Handle error */
}
} |
Compliant Solution (size_t)
To ensure the comparison is properly performed, the return value of mbstowcs() should be compared against -1 cast to type size_t:
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdlib.h>
void func(wchar_t *pwcs, const char *restrict s, size_t n) {
size_t count_modified = mbstowcs(pwcs, s, n);
if (count_modified == (size_t)-1) {
/* Handle error */
}
} |
Risk Assessment
Failure to cast integers before comparing or assigning them to a larger integer size can result in software vulnerabilities that can allow the execution of arbitrary code by an attacker with the permissions of the vulnerable process.
Rule | Severity | Likelihood |
|---|
Detectable | Repairable | Priority | Level |
|---|---|---|---|
INT18-C |
High | Likely |
No |
Yes | P18 | L1 |
Automated Detection
Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Astrée |
| Supported, but no explicit checker | |||||||
| CodeSonar |
| LANG.TYPE.AWID | Expression value widened by assignment | ||||||
| Compass/ROSE |
Can detect violations of this rule. It should look for patterns of
| |||
| Coverity | 6.5 | OVERFLOW_BEFORE_WIDEN | Fully |
5.0
Can detect violations of this rule with CERT C Rule Pack
| implemented | |||||||||
| Helix QAC |
| C1890, C1891, C1892, C1893, C1894, C1895, C4490, C4491, C4492 | |||||||
| Klocwork |
| PORTING.CAST.SIZE | |||||||
| LDRA tool suite |
| 452 S | Partially implemented | ||||||
| Parasoft C/C++test |
| CERT_C-INT18-a | Avoid possible integer overflow in expressions in which the result is assigned to a variable of a wider integer type | ||||||
| Polyspace Bug Finder |
| Checks for integer overflow and unsigned integer overflow (rec. fully covered) |
1890
1891
1892
1893
1894
1895
2790 (C)
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
| SEI CERT C++ |
| Coding Standard | INT35-CPP. Evaluate integer expressions in a larger size before comparing or assigning to that size |
| ISO/IEC TR 24772:2013 | Numeric Conversion Errors [FLC] |
| MITRE CWE | CWE-681, Incorrect conversion between numeric types CWE-190, Integer overflow (wrap or wraparound) |
Bibliography
| [Dowd 2006] | Chapter 6, "C Language Issues" |
| [Seacord 2013] | Chapter 5, "Integer Security" |
...
...