Skip to main content

IAR Embedded Workbench for RISC-V 3.40

CERT-ERR30-C_b

In this section:
Synopsis

Check errno only after the function returns a value indicating failure.

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

It is meaningful for a program to inspect the contents of errno only after an error might have occurred. More precisely, errno is meaningful only after a library function that sets errno on error has returned an error code.

Coding standards

This check does not correspond to any coding standard rules.

Code examples

The following code example fails the check and will give a warning:

#include <errno.h>
#include <stdlib.h>

void example(char *c) {
  long a = strtol(c, NULL, 8);
  // Not checking the return value, just errno
  if (errno == 0) {
    return;
  }
}

The following code example passes the check and will not give a warning about this issue:

#include <limits.h>
#include <errno.h>
#include <stdlib.h>

void example(char *c) {
  long a = strtol(c, NULL, 8);
  if (a == LONG_MAX && errno == ERANGE) {
    return;
  }
}