| Wiki Markup |
|---|
C99 defines the {{ |
fwrite()}} function as follows \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\]: |
size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
The
fwrite()function shall write, from the array pointed to byptr, up tonitemselements whose size is specified bysize, to the stream pointed to bystream. For each object,sizecalls shall be made to thefputc()function, taking the values (in order) from an array of unsigned char exactly overlaying the object. The file-position indicator for the stream (if defined) shall be advanced by the number of bytes successfully written. If an error occurs, the resulting value of the file-position indicator for the stream is unspecified.
The definition does not state that the fwrite() function will stop copying characters into the file if a null character is encountered. Therefore, when writing a
...
null-terminated byte string
...
to a file using the fwrite() function, always use the size of the buffer string plus 1 (to account for the null character) as the nitems parameter.
Noncompliant Code Example
...
| Code Block | ||
|---|---|---|
| ||
#include <stdio.h>
char *buffer = NULL;
long size1, size2;
FILE *filedes;
/*
* Assume size1 and size2 are appropriately initialized
*
*/
filedes = fopen("out.txt", "w+");
if (filedes < 0) {
/* Handle error */
}
buffer = (char *)calloc(1, size1);
if (buffer == NULL) {
/* Handle error */
}
fwrite(buffer, sizeof(char), size2, filedes);
free(buffer);
buffer = NULL;
fclose(filedes);
|
Compliant
...
Solution
This compliant solution ensures that the correct number of characters are written to the file.
...
Failure to follow the recommendation could result in a non-null-terminated string being written to a file. This will create problems when the program tries to read it back as a cnull-terminated byte string.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
FIO18-C | Medium | Probable | Medium | P12 | L1 |
...
This rule can be found in the C++ Secure Coding Practice as FIO18-CPP. Never expect write() to terminate the writing process at a null character.
References
| Wiki Markup |
|---|
\[1\] [http://www.opengroup.org/onlinepubs/009695399/functions/fwrite.html] \[2\] \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 7.19.8.2, "The fwrite function" |