21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

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.

cates failure, it’s likely that no randomness was put into the buffer. If the programmer<br />

does not check the return code, predictable data will be used.<br />

In general, those functions that are not directly security-critical when their return<br />

value goes unchecked are often indirect security problems. (This can often happen<br />

with memory allocation functions, for example.) At the very least, such problems are<br />

often denial of service risks when they lead to a crash.<br />

One solution to this problem is to ensure that you always check return values from<br />

functions. That approach works in theory, but it is very burdensome on the programmer<br />

and also hard to validate.<br />

A more practical answer is to use exception handling. Using exception handling, any<br />

error conditions that the programmer does not explicitly handle will cause the program<br />

to terminate (which is generally a good idea, unless the premature termination<br />

somehow causes an insecure state).<br />

The problem with exception handling is that it does not solve the denial of service<br />

problem. If a developer forgets to handle a particular exception, the program will<br />

generally still terminate. Of course, the entire program can be wrapped by an exception<br />

handler that restarts the program or performs a similar action.<br />

In C++, exception handling is built into the language and should be familiar to many<br />

programmers. We will illustrate via example:<br />

try {<br />

somefunc( );<br />

}<br />

catch (MyException &e) {<br />

// Recover from error type MyException.<br />

}<br />

catch (int e) {<br />

// Recover if we got an integer exception code.<br />

}<br />

The try block designates code we would like to execute that may throw an exception.<br />

It also says that if the code does throw an exception, the following catch blocks<br />

may be able to handle the exception.<br />

If an exception is not handled by one of the specified catch blocks, there may be<br />

some calling code that catches the exception. If no code wants to catch the exception,<br />

the program will abort.<br />

In C++, the catch block used is selected based on the static type of the exception<br />

thrown. Generally, if the exception is not a primitive type, we use the & to indicate<br />

that the exception value should be passed to the handler by reference instead of<br />

being copied.<br />

To raise an exception, we use the throw keyword:<br />

throw 12; // Throw an integer as an error. You can throw arbitrary objects in C++.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Performing Error Handling | 701

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

Saved successfully!

Ooh no, something went wrong!