Did Sir Pterry every address the question of which divinty attends to tangled cables? I mean, it seems likely to me that the duty would fall to Anoia. Right?
2026-06-27
2025-04-11
No really. Should I use that library function that I shouldn't use?
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 lose 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 need 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."
Exeunt. Laughing.
Sadly it's 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 snprintf3 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
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 me 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.
2024-01-28
Career opportunity
Desperately seaking a licensned professional to tell us that we're making good parenting choices.
2022-11-07
Not an advertised feature
The child has a tablet and likes to stream stuff on it in the car, which means tethering it to one of our phones (yes, we're wondering about making the next one cellular enabled). You can imagine the various minor inconveniences that implies, but it also means that when my wife set off on the pre-school delivery run leaving her phone in the upstairs bathroom (where the tablet had no trouble connecting to it), she rapidly learned that something was wrong.
Back they came and (using her smartwatch) she called me from the driveway to tell me she didn't have her phone. Cue double take.
New use for the table: phone abandonment detector.
2021-01-05
The way I remember it...
- Parent:
- So it's to be torture?
- The Albino:
- [nods enthusiastically]
- Parent:
- I can cope with torture.
- The Albino:
- [shakes head enthusiastically]
- Parent:
- Don't believe me?
- The Albino:
- You survived grad school, so you must be very brave, but no one withstands The Toddler.
And later
- Count Rugen:
- As you know, the concept of the suction pump is centuries old. Really that's all The Toddler is except that instead of sucking water, she's sucking serenity, sanity and coolness.
2020-09-10
On styling text in a QTextBrowser
My project at work is complicated enough to need fairly extensive on-line documentation, which we write as html and diplay to the user in a QTextBrowser. Now, this thing is not a full web browser1 and only a supports a limited subset of html/css.
Suffice it to say that if you're just had a clever CSS idea it's not supported by QTextBrowser.
At first this didn't bother me too much because I had never done css before. But as I've become more familiar with the tool (and at once awed and appalled by it), I've become aware of just what I'm missing out on. I wrote the quoted sentence as part of our guidelines for writing and formatting the help files.
I'm sticking with QTextBrowser for now because our needs are simple and we can't really expect an incoming junior dev to be able to support a sophisticated pile of css anyway.2
1 That's available as QWebEngine, but it brings in a large module so I didn't want to use it.
2 Because of the nature of our work, our target demographic for new hires is "scientists and engineers who can program" more than it is people with a strong development background. Having web skills would be a bonus, but it's not part of the expectation.
2020-07-21
Neolithic

I'm taking the position that awk may be a stone aged tool, but like a Clovis point it represents the very finest craftsmanship of that age: simple in form, highly functional, and beautifully constructed with everything it needs for its task and nothing else.
And no, perl is not a better example. Larry bolted everything including the kitchen sink onto it. That certainly made it more functional, but it also made it ugly. I'm not highly conversant with the vast array of choices in the rapid development marketplace, but I would judge that both ruby and python come a lot closer than perl to the goal of being at once all-singing-all-dancing and beautiful. Tcl/tk is right out and I an happy having no basis on which to judge php.
2020-07-20
On toddler fearlessness
Can you get a subscription to the emergency room?
2020-06-12
Wordo of the day
And someone else's for once. Anyway, seen in my news feed:
fiend of the courtin a context where it is clearly meant to be the customary translation of amicus curiae.
A little amused poking around shows that the diabolical version has occassionally been used with malice aforethought. I think I'll have a look at some of those.
2020-06-11
Question of the day
Do you ever wonder what kind of barbarian we're raising?
2020-05-10
On the sleep habits of celestial beings
2020-02-01
The 2nd Law of Thermodynamics, toddler versions
- The performance of heat engines or refrigerators
- The natural direction of heat flow (arguably equivalent to item 1, but it may not be obvious at first)
- Classical concepts of entropy
- Statistical (and even information theoretical) concepts of entropy.
Toddler versions
My daughter keeps encountering phenomena which to my professorial eye look like physics teaching moments. To her they just represent disasters.In any case, I'd like to codify some of the toddler statements of the 2nd Law:
- You can't put your banana back together again,
- You can't separate your play dough colors after you've mixed them.