...
To correct this example, the loop termination condition must be modified to account for the null-termination character that is appended to dest
.:
Code Block | ||||
---|---|---|---|---|
| ||||
char dest[ARRAY_SIZE]; char src[ARRAY_SIZE]; size_t i; /* ... */ for (i=0; src[i] && (i < sizeof(dest)-1); i++) { dest[i] = src[i]; } dest[i] = '\0'; /* ... */ |
...
A more general solution is to use the snprintf()
function.:
Code Block | ||||
---|---|---|---|---|
| ||||
char* name; /* initialized externally */ char filename[128]; snprintf(filename, sizeof(filename), "%s.txt", name); /* Open filename * / |
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Compass/ROSE |
|
| Can detect violations of the rule. However, it is unable to handle cases involving | ||||||
Coverity | 6.5 | STRING_OVERFLOW STRING_SIZE | Fully Implemented. Fully implemented. | ||||||
Fortify SCA | 5.0 |
|
| ||||||
|
|
| |||||||
|
|
| |||||||
Splint |
|
|
|
...