Skip to main content

IAR Embedded Workbench for RX 5.20

CERT-FIO44-C

In this section:
Synopsis

Only use values for fsetpos() that are returned from fgetpos().

Enabled by default

Yes

Severity/Certainty

Medium/Low

mediumlow.png
Full description

Invoking the fsetpos() function with any other values for pos is undefined behavior.

Coding standards
CERT FIO44-C

Only use values for fsetpos() that are returned from fgetpos()

Code examples

The following code example fails the check and will give a warning:

#include <stdio.h>
#include <string.h>

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

The following code example passes the check and will not give a warning about this issue:

#include <stdio.h>
#include <string.h>

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