Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Illusions of Safety


Prevention by Design

Prevention by design means, simply, designing your application so that low-level code doesn't need to check buffer sizes. If you check the data at the point where it enters your application, you can call low-level functions without worrying about buffer overruns. The code to copy text into a buffer simply copies text into a buffer:

void process(const char *txt)
{ /* strlen(txt) must be less than MAX_LINE */
char buf[MAX_LINE];
assert(strlen(txt) < MAX_LINE);
strcpy(buf, txt);
/* continue normal processing */

The design of this function pushes the responsibility for avoiding buffer overruns up into its caller. By putting an assert in the function, you provide a debugging check to help assure that you've properly validated the data higher up. Functions that call this function will, in turn, push the checking up into their callers [5], all the way to the function that creates the text string. By design, each intermediate function must be called only with a text string that fits in this buffer:

void middle(const char *txt)
{ /* strlen(txt) must be less than MAX_LINE */
process(txt);}

void creator(FILE *fp)
{ /* read and process lines of up
      to MAX_LINE-1 characters */
char buf[MAX_LINE];
while (fgets(buf, MAX_LINE, fp))
	middle(buf);
}


This function uses the same size buffer for its input as we used deeper down, so the check it has to make to ensure that its buffer doesn't overflow does double duty by also ensuring that the low-level function's buffer doesn't overflow. By pushing the checking up to the function that creates the text string, we've eliminated one validity check entirely. In a real application, this approach eliminates far more than one check, and the application becomes simpler, smaller, and faster.

The biggest drawback of eliminating buffer overruns by design is that it requires careful attention from the application's designers to ensure that all buffer sizes are properly specified, so that the high-level tests are sufficient. Validating data at the point where it's used instead of where it's created doesn't require as much thought. It's easier to find places where the check is missing, so it's easier to enforce the discipline of validation.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.