Main Content

CERT C++: FIO44-C

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

Description

Rule Definition

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

Polyspace Implementation

The rule checker checks for Invalid file position.

Examples

expand all

Issue

Invalid file position occurs when the file position argument of fsetpos() uses a value that is not obtained from fgetpos().

Risk

The function fgetpos(FILE *stream, fpos_t *pos) gets the current file position of the stream. When you use any other value as the file position argument of fsetpos(FILE *stream, const fpos_t *pos), you might access an unintended location in the stream.

Fix

Use the value returned from a successful call to fgetpos() as the file position argument of fsetpos().

Example - memset() Sets File Position Argument
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


FILE *func(FILE *file)
{
    fpos_t offset;
    if (file == NULL)
    {
        /* Handle error */
    }
    /* Store initial position in variable 'offset' */
    (void)memset(&offset, 0, sizeof(offset)); 

    /* Read data from file */

    /* Return to the initial position. offset was not
	returned from a call to fgetpos()	*/
    if (fsetpos(file, &offset) != 0) //Noncompliant
    {
        /* Handle error */
    }
    return file;
}
        
      

In this example, fsetpos() uses offset as its file position argument. However, the value of offset is set by memset(). The preceding code might access the wrong location in the stream.

Correction — Use a File Position Returned From fgetpos()

Call fgetpos(), and if it returns successfully, use the position argument in your call to fsetpos().

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

FILE *func(FILE *file)
{
    fpos_t offset;
    if (file == NULL)
    {
        /* Handle error */
    }
    /* Store initial position in variable 'offset' 
    using fgetpos() */
    if (fgetpos(file, &offset) != 0)         
    {
        /* Handle error */
    }

    /* Read data from file */

    /* Back to the initial position */
    if (fsetpos(file, &offset) != 0)          
    {
        /* Handle error */
    }
    return file;
}

Check Information

Group: 07. Input Output (FIO)

Version History

Introduced in R2019a


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.