Once spotted, they
are usually eradicated quickly. Because they are more likely to be found by automated
processes, as discussed in later chapters, format string errors appear to be on the decline.
That said, it is still good to have a basic understanding of them because you never know
what will be found tomorrow. Perhaps you might find a new format string error!
169
The Problem
Format strings are found in format functions. In other words, the function may behave
in many ways depending on the format string provided. Here are a few of the many format
functions that exist (see the ???References??? section for a more complete list):
??? printf() Prints output to STDIO (usually the screen)
??? fprintf() Prints output to FILESTREAMS
??? sprintf() Prints output to a string
??? snprintf() Prints output to a string with length checking built in
Format Strings
As you may recall from Chapter 6, the printf() function may have any number of arguments.
We presented the following forms:
printf(
, );
printf();
The first form is the most secure way to use the printf() function. This is because with
the first form, the programmer explicitly specifies howthe function is to behave by using
a format string (a series of characters and special format tokens).
Pages:
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344