Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Subclause 7.21.9.3 of the C Standard [ISO/IEC 9899:2011] defines the following behavior for fsetpos():

...

Code Block
bgColor#FFCCCC
langc
#include <stdio.h>
#include <string.h>
 
enum { NO_FILE_POS_VALUES = 3 };

int opener(FILE *file) {
  int rc;
  fpos_t offset;

  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;
}

...

Code Block
bgColor#CCCCFF
langc
#include <stdio.h>
#include <string.h>
 
enum { NO_FILE_POS_VALUES = 3 };

int opener(FILE *file) {
  int rc;
  fpos_t offset;

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

  rc = fgetpos(file, &offset);
  if (rc != 0 ) { return rc; }

  /* Read in data from file. */

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

  return 0;
}

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

FIO44-C

mediumMedium

unlikelyUnlikely

mediumMedium

P4

L3

Automated Detection

Tool

Version

Checker

Description

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()

Fortify SCA

5.0

 

Can detect violations of this rule with CERT C Rule Pack

LDRA tool suite

Include Page
LDRA_V
LDRA_V

82 D

Fully implemented

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...

CERT C++ Secure Coding StandardFIO44-CPP. Only use values for fsetpos() that are returned from fgetpos()
ISO/IEC TS 17961 (Draft)Using a value for fsetpos other than a value returned from fgetpos [xfilepos]

Bibliography

[ISO/IEC 9899:2011]Subclause 7.21.9.3, "The fsetpos Function"

...