Versions Compared

Key

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

...

This noncompliant code example reads data from a file stream into a data structure.:

Code Block
bgColor#FFCCCC
langc
struct myData {
  char c;
  long l;
};

/* ... */

FILE *file;
struct myData data;

/* initializeInitialize file */

if (fread(&data, sizeof(struct myData), 1, file) < sizeof(struct myData)) {
  /* Handle error */
}

...

The best solution is to use either a text representation or a special library that ensures data integrity.:

Code Block
bgColor#ccccff
langc
struct myData {
  char c;
  long l;
};

/* ... */

FILE *file;
struct myData data;
char buf[25];
char *end_ptr;

/* initializeInitialize file */

if (fgets(buf, 1, file) == NULL) {
  /* Handle error */
}

data.c = buf[0];

if (fgets(buf, sizeof(buf), file) == NULL) {
  /* Handle Error */
}

data.l = strtol(buf, &end_ptr, 10);

if ((ERANGE == errno)
 || (end_ptr == buf)
 || ('\n' != *end_ptr && '\0' != *end_ptr)) {
    /* Handle Error */
}

...

Reading binary data that has a different format than expected may result in unintended program behavior.

Recommendation

Severity

Likelihood

Detectable

Remediation Cost

Repairable

Priority

Level

FIO09-C

medium

Medium

Probable

probable

No

high

No

P4

L3

Automated Detection

Tool

Version

Checker

Description

Compass/ROSE

 

 



Could flag possible violations of this rule by noting any pointer to struct that is passed to fread(), as the noncompliant code example demonstrates

.

LDRA tool suite
Include Page
LDRA_V
LDRA_V
44 SEnhanced Enforcement

Related Vulnerabilities

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

Related Guidelines

...

...

Bibliography

...


...

Image Modified Image Modified Image Modified