Main Content

Stream argument with possibly unintended side effects

Stream argument side effects occur more than once

Description

This defect occurs when you call getc(), putc(), getwc(), or putwc() with a stream argument that has side effects.

Stream argument with possibly unintended side effects considers the following as stream side effects:

  • Any assignment of a variable of a stream, such as FILE *, or any assignment of a variable of a deeper stream type, such as an array of FILE *.

  • Any call to a function that manipulates a stream or a deeper stream type.

The number of defects raised corresponds to the number of side effects detected. When a stream argument is evaluated multiple times in a function implemented as a macro, a defect is raised for each evaluation that has a side effect.

A defect is also raised on functions that are not implemented as macros but that can be implemented as macros on another operating system.

Risk

If the function is implemented as an unsafe macro, the stream argument can be evaluated more than once, and the stream side effect happens multiple times. For instance, a stream argument calling fopen() might open the same file multiple times, which is unspecified behavior.

Fix

To ensure that the side effect of a stream happens only once, use a separate statement for the stream argument.

Examples

expand all

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

#define fatal_error() abort()

const char* myfile = "my_file.log";

void func(void)
{
    int c;
    FILE* fptr;
    /* getc() has stream argument fptr with
    * 2 side effects: call to fopen(), and assignment
    * of fptr
    */
    c = getc(fptr = fopen(myfile, "r"));
    if (c == EOF) {
        /* Handle error */
        (void)fclose(fptr);
        fatal_error();
    }
    if (fclose(fptr) == EOF) {
        /* Handle error */
        fatal_error();
    }
}

void main(void)
{
    func();

}

In this example, getc() is called with stream argument fptr. The stream argument has two side effects: the call to fopen() and the assignment of fptr. If getc() is implemented as an unsafe macro, the side effects happen multiple times.

Correction — Use Separate Statement for fopen()

One possible correction is to use a separate statement for fopen(). The call to fopen() and the assignment of fptr happen in this statement so there are no side effects when you pass fptr to getc().

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

#define fatal_error() abort()


const char* myfile = "my_file.log";

void func(void)
{
    int c;
    FILE* fptr;

    /* Separate statement for fopen()
    * before call to getc()
    */
    fptr = fopen(myfile, "r");
    if (fptr == NULL) {
        /* Handle error */
        fatal_error();
    }
    c = getc(fptr);
    if (c == EOF) {
        /* Handle error */
        (void)fclose(fptr);
        fatal_error();
    }
    if (fclose(fptr) == EOF) {
        /* Handle error */
        fatal_error();
    }
}

void main(void)
{
    func();

}

Result Information

Group: Programming
Language: C | C++
Default: On for handwritten code, off for generated code
Command-Line Syntax: STREAM_WITH_SIDE_EFFECT
Impact: Low

Version History

Introduced in R2018a