21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

86 | Chapter 3: Input Validation<br />

Casting a safe string to a normal C-style string and modifying it using<br />

C-style string-manipulation functions or other means defeats the protections<br />

and accounting afforded by the SafeStr library.<br />

The SafeStr library provides a rich set of API functions to manipulate the strings it<br />

manages. The large number of functions prohibits us from enumerating them all<br />

here, but note that the library comes with complete documentation in the form of<br />

Unix man pages, HTML, and PDF. Table 3-1 lists the functions that have C equivalents,<br />

along with those equivalents.<br />

Table 3-1. SafeStr API functions and equivalents for normal C strings<br />

SafeStr function C function<br />

safestr_append( ) strcat( )<br />

safestr_nappend( ) strncat( )<br />

safestr_find( ) strstr( )<br />

safestr_copy( ) strcpy( )<br />

safestr_ncopy( ) strncpy( )<br />

safestr_compare( ) strcmp( )<br />

safestr_ncompare( ) strncmp( )<br />

safestr_length( ) strlen( )<br />

safestr_sprintf( ) sprintf( )<br />

safestr_vsprintf( ) vsprintf( )<br />

You can typically create safe strings in any of the following three ways:<br />

SAFESTR_ALLOC( )<br />

Allocates a resizable string with an initial allocation size in bytes as specified by<br />

its only argument. The string returned will be an empty string (actual size zero).<br />

Normally the size allocated for a string will be larger than the actual size of the<br />

string. The library rounds memory allocations up, so if you know that you will<br />

need a large string, it is worth allocating it with a large initial allocation size up<br />

front to avoid reallocations as the actual string length grows.<br />

SAFESTR_CREATE( )<br />

Creates a resizable string from the normal C-style string passed as its only argument.<br />

This is normally the appropriate way to convert a C-style string to a safe<br />

string.<br />

SAFESTR_TEMP( )<br />

Creates a temporary resizable string from the normal C-style string passed as its<br />

only argument. SAFESTR_CREATE( ) and SAFESTR_TEMP( ) behave similarly, except<br />

that a string created by SAFESTR_TEMP( ) will be automatically destroyed by the<br />

next SafeStr function that uses it. The only exception is safestr_reference( ),<br />

which increments the reference count on the string, allowing it to survive until<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!