...
| Code Block | ||
|---|---|---|
| ||
wchar_t *wide_str1 = L"0123456789"; wchar_t *wide_str2 = L"0000000000"; wcsncpy(wide_str2, wide_str1, 10); /* Use of proper-width function */ char *narrow_str1 = "0123456789"; char *narrow_str2 = "0000000000"; strncpy(narrow_str2, narrow_str1, 10); /* Use of proper-width function */ |
Implementation Details
C99 recognizes wchar_t * and char * as distinct types. As a result, many compilers will yield a warning if the inappropriate function is used. For example, the following warnings were generated when the second non-compliant example was compiled with no flags in GCC on a Linux i686 platform:
| Code Block |
|---|
warning: passing arg 1 of `wcsncpy' from incompatible pointer type
warning: passing arg 2 of `wcsncpy' from incompatible pointer type
|
Similar warnings were issued by the compiler for the first non-compliant example, with respect to the arguments of the strncpy function instead.
Risk Assessment
Failure to use the proper-width string functions can lead to buffer overflows and the execution of arbitrary code by an attacker.
...