Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: REM Cost Reform

Calling The C Standard, 7.23.9.3 paragraph 2 [ISO/IEC 9899:2024], defines the following behavior for fsetpos():

The fsetpos function sets the mbstate_t object (if any) and file position indicator for the stream pointed to by stream according to the value of the object pointed to by pos, which shall be a

...

value obtained from an earlier successful call to the fgetpos function 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
bgColor#FFCCCC
langc
#include <stdio.h>
#include <string.h>
 
int
errno_t opener(FILE * file, int *width, int *height, int *data_offset) {
  int file_wrc;
  int file_h;
  int file_o;
  int rc;
  int offset = 0fpos_t offset;

  memset(&offset, 0, sizeof(offset));

  if (file == NULL) { return EINVAL; }
  if (fscanf(file, "%i %i %i", &file_w, &file_h, &file_o)  != 3) { return EIO; }
  if ((rc = fsetpos(file, &offset)) != 0 ) { return rc; return -1;
  }

  /*width = file_w;
  *height = file_h;
  *data_offset = file_o;

  return 0; 
}

int main(void) {
  int width;
  int height;
  int data_offset;
  FILE *file;
  ...Read in data from file */

  filerc = fopenfsetpos("myfile"file, "rb"&offset);
  if (opener(file, &width, &height, &data_offset) rc != 0 ) {
    return 0rc;
  }

  ...return 0;
}

However, since only Only the return value of a getposan fgetpos() call is a valid argument to be used with setposfsetpos(), ; passing a specified int in instead may not work. It is possible that the position will be set to an arbitrary location in the filevalue 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
bgColor#CCCCFF
langc

errno_t#include <stdio.h>
#include <string.h>
 
int opener(FILE * file, int *width, int *height, int *data_offset) {
  int file_w;
  int file_h;
  int file_o;
  int rc;
  fpos_t offset;

  if (file == NULL) {
 return EINVAL;    return -1;
  }

  if ((rc = fgetpos(file, &offset)) != 0 ) { return rc; }
  if (fscanf(file, "%i %i %i", &file_w, &file_h, &file_o)  != 3) { return EIO; }
  if ((rc = fsetpos(file, &offset)) != 0 ) { return rc; }

  *width = return file_wrc;
  *height = file_h;
  *data_offset = file_o;

  return 0; 
 }

int main(void) {
  int width;
  int height;
  int data_offset;
  FILE *file;
  ...

  file = fopen("myfile", "rb" /* Read in data from file */

  rc = fsetpos(file, &offset);
  if (opener(file, &width, &height, &data_offset) rc != 0 ) {
 return   return 0rc;
  }

  return ...0;
}

Risk Assessment

The misuse Misuse of the fsetpos() could move function can position a file stream read to a undesired position indicator to an unintended location in the file. If this location held input from user the user would then gain control of the variables being read from the file.

Rule

Severity

Likelihood

Detectable

Remediation Cost

Repairable

Priority

Level

FIO44-C

2 (medium)

1 (low)

2 (medium)

P2

L3

References

Medium

Unlikely

No

No

P2

L3

Automated Detection

Tool

Version

Checker

Description

CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
(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 fgetpos() is copied between several variables before being passed to fsetpos()

Cppcheck Premium

Include Page
Cppcheck Premium_V
Cppcheck Premium_V

premium-cert-fio44-c
Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

DF4841, DF4842, DF4843


Klocwork

Include Page
Klocwork_V
Klocwork_V

CERT.FSETPOS.VALUE


LDRA tool suite
Include Page
LDRA_V
LDRA_V

82 D

Fully implemented

Parasoft C/C++test

Include Page
Parasoft_V
Parasoft_V

CERT_C-FIO44-aOnly use values for fsetpos() that are returned from fgetpos()
Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rule FIO44-CChecks for invalid file position (rule partially covered)
PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

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:2013Using 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"


...

Image Added Image Added Image Added Wiki Markup\[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\] Section 7.19.9.3, "The fsetpos function"