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.

solution can likely be ported to some other processors as well, but the non-x86 systems<br />

on which we have tested do not work (in particular, this trick does not work on<br />

Apple Mac G3 or G4 systems). This solution also requires that you do not compile<br />

your program using the optimization option to omit the frame pointer, -fomitframe-pointer,<br />

because it depends on having the frame pointer available.<br />

The second solution we have presented should work with any compiler and processor<br />

combination. It works by adding an extra argument passed to the function that is<br />

a “magic” value. When spc_next_varg( ) gets the next argument by calling va_arg( )<br />

itself, it checks to see whether the value of the argument matches the “magic” value.<br />

The need to add this extra “magic” argument is the reason for the VARARG_CALL_x<br />

macros. We have chosen a magic value of 0xDEADBEEF here, but if a legitimate argument<br />

with that value might be used, it can easily be changed to something else. Certainly,<br />

the code provided here could also be easily modified to allow different<br />

“magic” values to be used for different function calls.<br />

Finally, note that both implementations of spc_next_varg( ) print an error message<br />

to stderr and call abort( ) to terminate the program immediately. Handling this<br />

error condition differently in your own program may take the form of throwing an<br />

exception if you are using the code in C++, or calling a special handler function.<br />

Anything except allowing the function to proceed can be done here. The error<br />

should not necessarily be treated as fatal, but it certainly is serious.<br />

See Also<br />

Recipe 3.2<br />

13.5 Performing Proper Signal Handling<br />

<strong>Problem</strong><br />

Your program needs to handle asynchronous signals.<br />

Solution<br />

On Unix systems, it is often necessary to perform some amount of signal handling.<br />

In particular, if a program receives a termination signal, it is often desirable to perform<br />

some kind of cleanup before terminating the program—flushing in-memory<br />

caches to disk, recording the event to a log file, and so on. Unfortunately, many programmers<br />

do not perform their signal handling safely, which of course leads to possible<br />

security vulnerabilities. Even more unfortunate is that there is no cookie-cutter<br />

solution to writing safe signal handlers. Fortunately, following some easy guidelines<br />

will help you write more secure signal-handling code.<br />

712 | Chapter 13: Other Topics<br />

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

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!