You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Many functions return useful values whether or not the function has side effects. In most cases, this value is used to signify whether the function successfully completed its task or if some error occurred. Other times, the value is the result of some computation and is an integral part of the function's API.

Since a return value often contains important information about possible errors, it should always be checked; otherwise, the cast should be made explicit to signify programmer intent.

Noncompliant Code Example

This noncompliant code example opens a file, reads in its information, and closes it again.

my $source;
open(SOURCE, "<", $source);
@lines = (<SOURCE>);
close(SOURCE);

It makes sure the variable containing file name is properly defined, but does nothing else to catch errors. Consequently, any error, such as the file not existing, being readable, or containing too much data to read into memory will cause the program to abort.

Compliant Solution

This compliant colution does the same thing, but provides useful error messages if anything goes wrong.

my $source;
open(SOURCE, "<", $source) or die "error opening $source: $!";
@lines = (<SOURCE>);
close(SOURCE) or die "error closing $source: $!";

If any error occurs, the program calls the die() function, passing it a string that includes both the source file being opened, and the $! variable, which contains a system error string based on the value of errno, which is set to a useful value when the open(2) or close(2) functions fail.

Exceptions

EXP32-EX1: If the return value is inconsequential or if any errors can be safely ignored, such as for functions called because of their side effects, the function's return value may be silently discarded.

EXP32-EX2: The autodie module is designed to replace functions that return a value indicating failure with functions that throw an exception on failure. When autodie is in use, then functions it redefined may be safely ignored.

use autodie;

my $source;
open(SOURCE, "<", $source);
@lines = (<SOURCE>);
close(SOURCE);

Risk Assessment

Failure to handle error codes or other values returned by functions can lead to incorrect program flow and violations of data integrity.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

EXP32-PL

info

probable

low

P12

L1

Automated Detection

Tool

Diagnostic

Perl::Critic

InputOutput::RequireCheckedClose

 

InputOutput::RequireCheckedOpen

 

InputOutput::RequireCheckedSyscalls

Related Guidelines

CERT C++ Secure Coding Standard: EXP12-CPP. Do not ignore values returned by functions or methods

CERT C Secure Coding Standard: EXP12-C. Do not ignore values returned by functions

The CERT Oracle Secure Coding Standard for Java: EXP00-J. Do not ignore values returned by methods

Bibliography

open()
CPAN autodie


EXP11-C. Do not apply operators expecting one type to data of an incompatible type      03. Expressions (EXP)      EXP13-C. Treat relational and equality operators as if they were nonassociative

  • No labels