The world would make more sense had it happened this way:
Shoot in grainy black and white.
Scene: a sprawling cellar lit by torches and candelabras. Brick archways lead in multiple direction. All the spaces we see are filled with abandoned experiments, strange equipment, and untidy storage. Some of the bits spark. Others bubble. Here and there tables are covered with books, notes, and abandoned comestibles.
Richiestein: "Igor, the world degenerates. Many programmers loose their edge, hiding every day behind strong guarantees from safe environments.1 We must bring LIFE back to the demons and dragons of yesteryear!"
Igor: "Yes Master"
Richiestein: "I needs a way, Igor; a way to trap programmers. To make it easy for them to err; easy to use memory they do not intend."
Igor: "Oh, master. Igor knows. Igor is sure master. You must use sentinel terminated strings, master!"
Richiestein: "Will that help?"
Igor: "Oh yes, master!"
Richiestein: "Will they not see that it is a bad design?"
Igor: "Master must tell them it is for simplicity. And show them correct code saying that it is elegant and basic. Maybe write a book?"
Richiestein: "I suppose."
Richiestein: "But can I get them to overrun their buffers that way?
Igor: "Oh yes master, let Igor show you!"
Igor hurries to a crowded corner of the room and digs frantically through trunks and shelves before returning triumphantly to show the good doctor what he has found.
Igor: "See here, master, I have gets
!"
The doctor squints at the jagged and rusty artifact before looking sharply at his assistant.
Richiestein: "Would anyone accept something so obviously cursed as this? Surely not?"
Igor: "They will, Master! They will! You need only put it in the Standard Library master!"2
Richiestein: "Hmmm. We'll try it. But we also need something more subtle. Something that looks like it would work. Maybe something that only brings disaster occasionally. Do we have that, too?"
Igor: "We do, Master!"
Igor roots through another corner of the cellar before bringing a many-geared contraption of only slightly tarnished brass.
Igor: "See master: snprintf
. A powerful tool,
but if it runs out of space no sentinel is placed, making their string a
trap!"
Richiestein: "Oh, that one is better. It almost works."
Exuant. Laughing.
Sadly it's so much more likely there were making only what they really, really needed (and would fit in the very small machine they had on-hand) and intended to come back and fix it later. That's the way these things usually happen.
I do know the danger
The standard library function snprintf
3 guarantees
to not overwrite the buffer (assuming of course that the programmer passed the
right length). Which looks so promising, but
they don't guarantee to write a
terminal '\0'
. They only add that if there is room left in the
buffer.4
So, if you use snprintf
to fill in a buffer and run out of room
without noticing (always get the young man's name and address check the
return value!), and are later so incautious as to try to measure the length
(with strlen
) there'll be no terminator and you read off into
random memory until you encounter a zero byte. Or a seg-fault. Or nasal
demons, of course.
And it can get even worse.5 Say you think you want to perform an
in-place tokenization with strtok
.6 Now you can
be writing past the end of the buffer.
And, yet, I still use the function
And yet, when I
exhibited my
nifty "safe" string builder for c, I
used vsnprintf
. Twice. Even though it's a trap. What gives?
grug quite satisfied when complexity demon trapped properly in crystal, is best feeling to trap mortal enemy!
The answer, of course, is that by using it very carefully in just one place I intend to relieve future programmers (including my future self) from the need of using it repeatedly elsewhere.
I also put prominent comments in, not so much to convince future readers as to make sure that I was paying attention, though they should serve as a alert for future readers too. They'll have reason to read the docs with "how long" and null termination in mind. I hope.
When is it a good idea, again?
So ... it's sometimes okay to mess with the magic lamp? Really?
Honestly, that function is dangerous, and the comments don't fix it. If I
were developing a non-trivial code in c as a green field project, I'd give
real consideration to not using big parts of the c standard library starting
with string.h
. But the use case that drove me to thinking about
it in the first place was maintenance on a statically-linked launcher for a
C++ project.7
1 Okay, so strong guarantees were few and far between in the early 1970s. but, this is Hollywood! Work with me here.
2 And don't call ne Shirley!
3 Or its variadac sibling vsnprintf
.
4 Cue Admiral Ackbar.
5 As far as the standard is concerned this is exactly as bad as before—undefined behavior—but from a practical point of view it is more immediately destructive.
6 You probably don't, really. For several reasons, but take it arguendo.
7 It is harder to write a C++ program you know will "just work" on a target machine, then to do the same with a C program. Especially on windows. So we have a little program written in c that should "just work". It is suppose to detect the environment, set up the prerequisites for the big C++ code, produce a clear error message if it can't, and then launch the main app. It does a lot of string manipulation. But it's been there a long time and it works; there is no way I could justify switching string libraries wholesale.
No comments:
Post a Comment