C and C++


LANG.ERRCODE.NZ : Non-zero Error Code

Summary

The code calls a function f() that will set an error code (such as errno) on failure, but the error code is not set to zero (0) before the call. This means that if the error code is non-zero after calling f(), it may have been set by some earlier function call and not by f().

Properties

Class Name Non-zero Error Code
Significance style
Mnemonic LANG.ERRCODE.NZ
Categories
MisraC2023 MisraC2023:22.8 The value of errno shall be set to zero prior to a call to an errno-setting-function
  MisraC2023:D.4.7 If a function returns error information, then that error information shall be tested
Misra2012 Misra2012:22.8 The value of errno shall be set to zero prior to a call to an errno-setting-function
  Misra2012:D.4.7 If a function returns error information, then that error information shall be tested
Misra2004 Misra2004:16.10 If a function returns error information, then that error information shall be tested
  Misra2004:20.5 The error indicator errno shall not be used
AUTOSARC++14 AUTOSARC++14:M0-3-2 If a function generates error information, then that error information shall be tested.
  AUTOSARC++14:M19-3-1 The error indicator errno shall not be used.
MisraC++2008 MisraC++2008:0-3-2 If a function generates error information, then that error information shall be tested.
  MisraC++2008:19-3-1 The error indicator errno shall not be used.
TS17961 TS17961:5.24-inverrno 5.24. Incorrectly setting and using errno
CERT-C CERT-C:ERR33-C Detect and handle standard library errors
  CERT-C:POS54-C Detect and handle POSIX library errors
JSF++ JSF++:17 The error indicator errno shall not be used.
  JSF++:115 If a function returns error information, then that error information will be tested.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Non-zero Error Code"

Example

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

void errno_nonzero(FILE *fp1, FILE *fp2){
    errno=0;
    (void) fseek(fp1, 0L, 0);              /* ok: errno set to 0 immediately before call */
    (void) ftell(fp1);       /* 'Non-zero Error Code' warning issued here
                              * - errno not set to 0 before call to ftell()
                              * - if errno was set by the fseek() call, this value is not cleared
                              */
    if (errno==0){
        (void) ftell(fp2);                 /* ok: call only occurs on path where errno==0 */
    }
    else {
        /* handle error */                 /* errno!=0 may have been caused by the ftell(fp1) call, but it may
                                            * also have been caused by the fseek() call. */
    }
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.