01.07.2016 Views

SEI CERT C Coding Standard

tqcylJ

tqcylJ

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Error Handling (ERR) - ERR30-C. Set errno to zero before calling a library function known to set errno, and check errno only<br />

after the function returns a value indicating failure<br />

13 Error Handling (ERR)<br />

13.1 ERR30-C. Set errno to zero before calling a library function known<br />

to set errno, and check errno only after the function returns a value<br />

indicating failure<br />

The value of errno is initialized to zero at program startup, but it is never subsequently set to<br />

zero by any C standard library function. The value of errno may be set to nonzero by a C standard<br />

library function call whether or not there is an error, provided the use of errno is not documented<br />

in the description of the function. It is meaningful for a program to inspect the contents of<br />

errno only after an error might have occurred. More precisely, errno is meaningful only after a<br />

library function that sets errno on error has returned an error code.<br />

According to Question 20.4 of C-FAQ [Summit 2005]<br />

In general, you should detect errors by checking return values, and use errno only to<br />

distinguish among the various causes of an error, such as “File not found” or “Permission<br />

denied.” (Typically, you use perror or strerror to print these discriminating error<br />

messages.) It’s only necessary to detect errors with errno when a function does not<br />

have a unique, unambiguous, out-of-band error return (that is, because all of its possible<br />

return values are valid; one example is atoi [sic]). In these cases (and in these<br />

cases only; check the documentation to be sure whether a function allows this), you can<br />

detect errors by setting errno to 0, calling the function, and then testing errno. (Setting<br />

errno to 0 first is important, as no library function ever does that for you.)<br />

Note that atoi() is not required to set the value of errno.<br />

Library functions fall into the following categories:<br />

• Those that set errno and return and out-of-band error indicator<br />

• Those that set errno and return and in-band error indicator<br />

• Those that do not promise to set errno<br />

• Those with differing standards documentation<br />

13.1.1 Library Functions that Set errno and Return an Out-of-Band Error<br />

Indicator<br />

The C <strong>Standard</strong> specifies that the functions listed in the following table set errno and return an<br />

out-of-band error indicator. That is, their return value on error can never be returned by a successful<br />

call.<br />

A program may set and check errno for these library functions but is not required to do so. The<br />

program should not check the value of errno without first verifying that the function returned an<br />

<strong>SEI</strong> <strong>CERT</strong> C <strong>Coding</strong> <strong>Standard</strong>: Rules for Developing Safe, Reliable, and Secure Systems 374<br />

Software Engineering Institute | Carnegie Mellon University<br />

[DISTRIBUTION STATEMENT A] Approved for public release and unlimited distribution.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!