Main Content

CERT C: Rec. DCL09-C

Declare functions that return errno with a return type of errno_t

Since R2026a

Description

Declare functions that return errno with a return type of errno_t1

Polyspace Implementation

The rule checker checks for the issue errno returning function not declared with errno_t return type.

Examples

expand all

Issue

This issue occurs when a function returns errno in at least one execution path but the function is not declared with the return type errno_t.

Risk

Consider a function that returns errno in at least one execution path. If the return type of such a function is not errno_t, then it is unclear from the function prototype if the function returns a error status in any of its execution paths. The meaning and nature of the returned value is not clear.

Fix

If a function checks for errors and returns errno, declare the function with the return type errno_t. The type errno_t is introduced in C11 Annex K. For older version of the C language, use a typedef to create this type:

#ifndef __STDC_LIB_EXT1__
  typedef int errno_t;
#endif

Example

In this example, the function func() checks for an error and returns errno. Because the return type of this function is int, Polyspace® reports a violation.

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

int func() {    //Noncompliant
	FILE *file;
	const char *filename = "filename.txt";

	// Attempt to open a file
	file = fopen(filename, "r");

	// Check if the file pointer is NULL
	if(file == NULL) {
		fprintf(stderr, "Error opening file '%s': %s\n", filename, strerror(errno));
		// Return errno to indicate the specific error
		return errno;
	}

	// If the file was opened successfully, close it
	fclose(file);

	return 0;
}

Correction

To fix this violation, use errno_t as the return type of func(). If necessary, use a typedef to define the type errno_t.

#include <stdio.h>
#include <errno.h>
#include <string.h>
typedef int errno_t;  //Define errno_t

errno_t func() {    //Compliant
	FILE *file;
	const char *filename = "filename.txt";

	// Attempt to open a file
	file = fopen(filename, "r");

	// Check if the file pointer is NULL
	if(file == NULL) {
		fprintf(stderr, "Error opening file '%s': %s\n", filename, strerror(errno));
		// Return errno to indicate the specific error
		return errno;
	}

	// If the file was opened successfully, close it
	fclose(file);

	return 0;
}

Check Information

Group: Rec. 02. Declarations and Initialization (DCL)
PQL Name: std.cert.DCL09_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.