Main Content

CERT C: Rec. MSC05-C

Do not manipulate time_t typed values directly

Since R2026a

Description

Do not manipulate time_t typed values directly1

Polyspace Implementation

The rule checker checks for Direct time_t manipulation.

Examples

expand all

Issue

The issue occurs when the you attempt to perform arithmetic operations directly on values of type time_t.

Risk

The type time_t is implementation-defined and can vary across different systems and compilers. Performing arithmetic on values of this type can lead to errors such as infinite loops, or unexpected errors due to the lack of a guaranteed representation.

Fix

Instead of directly manipulating time_t values, use the C standard library functions such as difftime() for calculating time differences, and mktime(), gmtime(), or localtime() for converting between time_t and struct_tm. Using these functions also keeps your code portable and accurate across different platforms.

Example — Direct Manipulation of time_t

#include <stdio.h>
#include <time.h>
#include <unistd.h>

int main() {
    // Get the starting time
    time_t start_time = time(NULL);
    if (start_time == (time_t)(-1)) {
        perror("Failed to get the start time");
        return 1;
    }

    // Simulate a task by sleeping for 5 seconds
    printf("Starting a task...\n");
    for (int i = 0; i < 5; i++) {
        printf("Processing...\n");
        sleep(1);  // Sleep for 1 second
    }

    // Get the ending time
    time_t end_time = time(NULL);
    if (end_time == (time_t)(-1)) {
        perror("Failed to get the end time");
        return 1;
    }

    // Calculate the elapsed time by direct subtraction
    double elapsed_seconds = end_time - start_time;    //Noncompliant

    // Display the elapsed time
    printf("The task took %.f seconds to complete.\n", elapsed_seconds);

    return 0;
}

In this example, the code calculates the time it takes to perform a task by directly subtracting start_time from end_time, which are of type time_t. Direct subtraction can work on some systems but can lead to incorrect or undefined behavior on others, especially if time_t is not a simple arithmetic type.

Correction — Use C Standard Library Functions to Manipulate time_t Values

In order to calculate the difference between start_time and end_time, you can instead use the function difftime().

#include <stdio.h>
#include <time.h>

int main() {
    // Get the starting time
    time_t start_time = time(NULL);
    if (start_time == (time_t)(-1)) {
        perror("Failed to get the start time");
        return 1;
    }

    // Simulate a task by sleeping for 5 seconds
    printf("Starting a task...\n");
    for (int i = 0; i < 5; i++) {
        printf("Processing...\n");
        sleep(1);  // Sleep for 1 second
    }

    // Get the ending time
    time_t end_time = time(NULL);
    if (end_time == (time_t)(-1)) {
        perror("Failed to get the end time");
        return 1;
    }

    // Calculate the elapsed time using difftime
    double elapsed_seconds = difftime(end_time, start_time);  //Compliant

    // Display the elapsed time
    printf("The task took %.f seconds to complete.\n", elapsed_seconds);

    return 0;
}

Check Information

Group: Rec. 48. Miscellaneous (MSC)
PQL Name: std.cert.MSC05_C

Version History

Introduced in R2026a


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.