Calling The C Standard, 7.23.9.3 paragraph 2 [ISO/IEC 9899:2024], defines the following behavior for fsetpos():
The
fsetposfunction sets thembstate_tobject (if any) and file position indicator for the stream pointed to bystreamaccording to the value of the object pointed to bypos, which shall be a
...
value obtained from an earlier successful call to the
fgetposfunction on a stream associated with the same file.
Invoking the fsetpos() function with any other values for pos is undefined behavior 181.
Noncompliant Code Example
This noncompliant code example attempts to read three values from a file and then set the file position pointer
Non-Compliant Code Example
The following non-compliant code attempts to read two parameters from a file, then set the cursor position back to the beginning of the file and return to the caller.:
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdio.h> #include <string.h> int mainopener(intFILE argc, char *argv[]*file) { int widthrc; int height; int data_fpos_t offset; FILE *file; ... file = fopen("myfile", "rb"); if(opener(file, &width, &height, &data_offset) != 0 ) { return 0; } ... } int opener(FILE* file, int *width, int *height, int *data_offset) { int w; int h; int o; int offset = 0; if(file == NULL) { return -1; } if (fscanf(file, "%i %i %i", &w, &h, &o) != 2) { return -1; } if (fsetpos(info, &offset) != 0) { return -1; } *width = w; *height = h; *data_offset = o;memset(&offset, 0, sizeof(offset)); if (file == NULL) { return -1; } /* Read in data from file */ rc = fsetpos(file, &offset); if (rc != 0 ) { return rc; } return 0; } |
However, since only Only the return value of a getposan fgetpos() call is a valid argument to be used with setpos(), it is unknown whether the above code will actually do what is intended. It is possible that the position will be set to an arbitrary location in the file fsetpos(); passing a value of type fpos_t that was created in any other way is undefined behavior 181.
Compliant Solution
In this compliant solution, the initial file position indicator is stored by first calling fgetpos(), which is used to restore the state back to the beginning of the file in the later call to fsetpos().:
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { int width; int height; int data_offset; FILE *file; ... file = fopen("myfile", "rb"); if(opener(file, &width, &height, &data_offset) != 0 ) { return 0; } ... } int opener(FILE * file, int *width, int *height, int *data_offset) { int wrc; int h; int o; fpos_t offset; if (file == NULL) { return return -1; } rc = if(fgetpos(file, &offset); if (rc != 0 ) { return -1; return rc; } if (fscanf(file, "%i %i %i", &w, &h, &o) != 2) { return -1; } /* Read in data from file */ rc = fsetpos(file, &offset); if (fsetpos(info, &offset) rc != 0 ) { return -1; } *width = return wrc; *height = h; *data_offset = o; } return 0; } |
Risk Assessment
The misuse Misuse of the fsetpos() could move function can position a file stream read to a undesired location in the file. If this location held input from user the user would then gain control of the variables being read from the position indicator to an unintended location in the file.
Rule | Severity | Likelihood |
|---|
Detectable | Repairable | Priority | Level |
|---|
DRAFT
2 (medium)
1 (low)
2 (medium)
P2
L3
References
FIO44-C | Medium | Unlikely | No | No | P2 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| CodeSonar |
| (customization) | Users can add a custom check for violations of this constraint. | ||||||
| Compass/ROSE | Can detect common violations of this rule. However, it cannot handle cases in which the value returned by | ||||||||
| Cppcheck Premium |
| premium-cert-fio44-c | |||||||
| Helix QAC |
| DF4841, DF4842, DF4843 | |||||||
| Klocwork |
| CERT.FSETPOS.VALUE | |||||||
| LDRA tool suite |
| 82 D | Fully implemented | ||||||
| Parasoft C/C++test |
| CERT_C-FIO44-a | Only use values for fsetpos() that are returned from fgetpos() | ||||||
| Polyspace Bug Finder |
| CERT C: Rule FIO44-C | Checks for invalid file position (rule partially covered) | ||||||
| PVS-Studio |
| V1035 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
Key here (explains table format and definitions)
Taxonomy | Taxonomy item | Relationship |
|---|---|---|
| ISO/IEC TS 17961:2013 | Using a value for fsetpos other than a value returned from fgetpos [xfilepos] | Prior to 2018-01-12: CERT: Unspecified Relationship |
Bibliography
| [ISO/IEC 9899:2024] | 7.23.9.3, "The fsetpos Function" |
...
\[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\] Section 7.19.9.3, "The fsetpos function"Wiki Markup