...
The following standard library functions take a pointer argument and a size argument, with the constraint that the pointer must point to a valid memory object of at least the number of bytes or wide characters (as appropriate) indicated by the size argument.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Library functions that take two pointers and an integer
The following standard library functions take two pointer arguments and a size argument, with the constraint that both pointers must point to valid memory objects of at least the number of bytes or wide characters as appropriate, indicated by the size argument.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Library functions that take a pointer and two integers
The following standard library functions take a pointer argument and two size arguments, with the constraint that the pointer must point to a valid memory object containing at least as many bytes as the product of the two size arguments.
|
|
|
|
Standard memory allocation functions
The following are the standard memory allocation functions that take a size integer argument and return a pointer.
|
|
|
|
Other Library Functions:
|
|
| ||
vswprintf() | swprintf() |
| ||
|
|
|
|
|
*Both functions take more than one size_t
argument. In such cases, the compliant code must be consistent with the purpose of these arguments. For example, in the case of fread()
:
...
Code Block | ||
---|---|---|
| ||
wchar_t *f4() { const wchar_t *p = L"Hello, World!"; const size_t n = sizeof(wchar_t) * (wcslen(p) + 1); wchar_t *q = (wchar_t*) malloc(n); return q; } |
Noncompliant Code Example
In this noncompliant example, a diagnostic is required because the value of n is not computed correctly, allowing a possible write past the end of the object referenced by p.
Code Block | ||
---|---|---|
| ||
void f4(char p[], const char *q) {
const size_t n = sizeof(p);
if ((memcpy(p, q, n)) == p) { /* violation */
/* ... */
}
/* ... */
}
|
Compliant Solution
This compliant solution ensures that the n is equal to the size of the character array.
Code Block | ||
---|---|---|
| ||
void f4(char p[], const char *q, size_t size_p) {
const size_t n = size_p;
if ((memcpy(p, q, n)) == p) {
/* ... */
}
/* ... */
} |
Risk Assessment
Depending on the library function called, the attacker may be able to use a heap overflow vulnerability to run arbitrary code.
...