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


Slogans

I'm not a big fan of design by slogan. I think it's far more important for programmers to understand what they're doing and why, than for them to be able to repeat catchy sayings that give overly broad guidelines and discourage thinking. Let's look at the slogans I mentioned earlier to see how helpful they are.

As we saw, careful design eliminates the need for many validity checks. So while it's tempting to compare programming to crossing streets, the safety rules are obviously different. Don't fall for this one.

Comparing functions that don't check buffer sizes to sharp knives is a graphic way of saying that some programmers can't be trusted to make safety checks on their own. That may be true, but it doesn't mean that such functions should be banned. In fact, most people know that dull knives are more dangerous than sharp ones, because you have to push harder on them, and so they're much more likely to slip and cut you. In programming terms, if you always have to check buffer sizes, then you always have to know the size of the buffer when you write to it. If that value isn't available, what do you do? Of course, what you ought to do is redesign the call chain all the way from the creator of the buffer down to its user, and pass that information down, along with the buffer itself. If you're pressed for time, you might give in to the temptation to just use the value that you know is right. Of course, it's only right until the next time it's changed, and then it's wrong. Now the safety check isn't checking anything meaningful.

The claim that anyone using gets is incompetent is rooted in the idea that there is no way that gets can be used safely. That was one of the early arguments for providing a safer version. However, in a suite of applications, one application can write data to be read by another application, with the assurance that the designed-in assumptions about line lengths are valid. The counterargument is that even this isn't safe, because a malicious user could modify the file between the time it's written and the time it's read. That's not something I stay up nights worrying about: My computer sits on my desk, and no malicious user can sneak in during the night and modify my saved files. It is possible to use gets safely, and it is not a sign of incompetence to do so.


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.