Contenido principal

CERT C: Rec. FIO09-C

R2026b

Be careful with binary data when transferring data across systems

Since R2026b

Description

Be careful with binary data when transferring data across systems1

Polyspace Implementation

Polyspace® checks for the issue Dangerous binary data transfer.

Examples

expand all

Issue

The issue occurs when fread() or fwrite() operates on types other than char and char*.

Risk

Binary data representations can differ across systems because of endianness, structure padding, and type sizes. Using fread() or fwrite(), which reads or writes the binary representation, makes your code less portable. Code that behaves correctly in one system may not behave the same in another system.

Fix

Serialize data to a text-based or platform-independent format before writing. When reading, parse text representations and convert to the appropriate types. Alternatively, use a serialization library that handles byte-order and alignment differences.

Example — Binary Read of Structure with fread

In this example, Polyspace reports a violation on the fread() call that reads binary data into an aggregate structure.


#include <stdio.h>
#include <stdlib.h>

struct SensorData {
    char id;
    long reading;
};

void read_sensor_data(FILE *stream) {
    struct SensorData data;
    fread(&data, sizeof(struct SensorData), 1, stream); // Noncompliant
}

The binary representation of the aggregate structure SensorData can be different across systems. Reading it with fread() makes the code nonportable. Polyspace reports a violation.

Correction — Use Text-Based Parsing

Read data as text and convert each field to the appropriate type.


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

struct SensorData {
    char id;
    long reading;
};

void read_sensor_data(FILE *stream) {
    struct SensorData data;
    char buf[64];
    char *end_ptr;

    if (fgets(buf, sizeof(buf), stream) == NULL) { // Compliant
        return;
    }
    data.id = buf[0];

    if (fgets(buf, sizeof(buf), stream) == NULL) { // Compliant
        return;
    }
    data.reading = strtol(buf, &end_ptr, 10);
    if ((errno == ERANGE) || (end_ptr == buf)) {
        return;
    }
}

Check Information

Group: Rec. 09. Input Output (FIO)
PQL Name: std.cert.FIO09_C

Version History

Introduced in R2026b


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.