Skip to main content

IAR Embedded Workbench for RH850 3.20.x

RESOURCE-file-pos-neg

In this section:
Synopsis

A file handler might be negative

Enabled by default

No

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

A file handler might be negative. If open() cannot open a file, it will return a negative file descriptor. Using this file descriptor might cause a runtime error.

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 <LowLevelIOInterface.h>

void example(void) {
  int a = __open("test.txt", _LLIO_WRONLY);
  write(a, "Hello", 5);
}

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

#include <LowLevelIOInterface.h>

void example(void) {
  int a = __open("test.txt", _LLIO_WRONLY);
  if (a > 0) {
    write(a, "Hello", 5);
  }
}