C and C++


LANG.ERRCODE.ITEST : Inappropriate Test of Error Code

Summary

The value of errno is tested, but the last function call was not to a function that can set errno.

For this warning class, CodeSonar will treat a function as setting errno if it is specified using the ERRNO_SETTING_FUNCTIONS configuration parameter.

Properties

Class Name Inappropriate Test of Error Code
Significance style
Mnemonic LANG.ERRCODE.ITEST
Categories
MisraC2023 MisraC2023:22.10 The value of errno shall only be tested when the last function to be called was an errno-setting-function
Misra2012 Misra2012:22.10 The value of errno shall only be tested when the last function to be called was an errno-setting-function
Misra2004 Misra2004:20.5 The error indicator errno shall not be used
AUTOSARC++14 AUTOSARC++14:M19-3-1 The error indicator errno shall not be used.
MisraC++2008 MisraC++2008:19-3-1 The error indicator errno shall not be used.
JSF++ JSF++:17 The error indicator errno shall not be used.
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="Inappropriate Test of Error Code"

Example

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

void noerrno(void){
  /* errno is never set by noerrno() or any of its callees */
}

int test_ftell_errno(FILE *fp){
    (void) ftell(fp);
    if (errno !=0) {                                 /* ok: last function call was to ftell(), which can set errno */
        return 1;
    }
    return 0;
}

int test_noerrno_errno(void){
    noerrno();
    if (errno != 0) {   /* 'Inappropriate Test of Error Code' warning issued here
                         * - last function call was to noerrno(), which never sets errno
                         */
        return 1;
    }
    return 0;
}

int test_errno_path(FILE *fp, int i){
  if (i > 0){
    (void) ftell(fp);
  }
  else {
     noerrno();
  }
  if (errno != 0){      /* 'Inappropriate Test of Error Code' warning issued here
                         * - on paths where i<=0 the last function call was to noerrno(), which never sets errno
                         */
    return 1;
  }
  return 0;
}

Relevant Configuration File Parameters

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