Skip to main content

The KISS Principle

“Keep It Simple, Sweetheart“ sounds easy enough to do, but in many ways, this is not a simple world. I do, however, enjoy simplifying things. (-:

Facing this rotten, ancient C program, having it segfault after a few minor changes for no obvious reason, I began to feel a bit stupid.

So... what happens when “We believe that we are stupid?

The curious thing about this belief is that it has been inflicted upon us by somebody else. We would have no concept of stupidity if someone had not told us about it & applied the adjective to us.

So reckons Diana Beaver, an educational researcher.

After discussing life for a bit with someone I seriously value, in which I learned that being “The Crazy Grandpa” is acceptable, but that I am very much not stupid, I decided to apply KISS.

It wasn’t working. Why not? I’d made only a few changes, so the reason is likely to be simple.

Back to the basics.

I sat down & pored through every line of code I was dealing with, rapidly (by #if 0-ing out sections temporarily to see if the rest worked) isolated it to within one function that I had barely touched.

In order for a problem to be made simple, one must understand it. Not always in excruciating detail, but it must be understood, So I began reading statement by statement to be sure that I understood it.

One of the local variables was char *buf[39];, which is an unusually specific value. It turned out that original author had sprintfed a %-38.38s into it. If that overflowed, it wrote text onto the next item in memory: the call-return address on the stack. Off to hyperspace in an instant!

One of my additions produced a 40-character (-byte) string. So... cw80<Esc> then make (which itself only became possible on Sunday), fixed.

The other problem appeared to be where an early routine called a date-interpreting function (which fetched a date stored as a string, hand-scrolled the month digits to the beginning of the string, hand-terminated it there, then returned an atoi ( ) of it).

Careful research discovered that the if ( ) statement this expression was embedded within first checked a string variable (from the argv [ ] array) for a specific value. If no value was supplied, the result was strcmp (NULL, "word"); which of course segfaulted as it attempted to fetch characters from address zero.

Fixed.

Simple can be quite satisfying. (-:

Comments

GregoryO said…
One wonders if the original coder had any experience or training, or simply chose to ignore them. I'm not even a programmer and I, from your musings, could identify and fix gaping holes in this code!
Major said…
Unix-like operating systems have a really great tool for dealing with programs like that. Its called "rm".
Leon RJ Brooks said…
Major, that sounds like the Naive Interface, which when asked “How do I make more space in this directory?” responded “Type rm *”

That would be my favourite solution as well, but I have not yet managed to sell the idea to the business concerned.
Leon RJ Brooks said…
Greg, I doubt that he had any... & in that guy’s case, admitting it would be about the last thing he’d do. (-:

Popular posts from this blog

every-application-is-part-of-a-toolkit at work

I have a LibreOffice Impress slideshow that I wish to turn into a narrated video. 1. export the slideshow as PNG images (if that is partially broken — as at now — at higher resolutions, Export Directly as PDF then use ‘pdftoppm’ (from the poppler-utils package) to do the same). 2. write a small C program (63 lines including comments) to display those images one at a time, writing a config file entry for Imagination (default transition: ‘cross fade’) based on when the image-viewer application (‘display,’ from the GraphicsMagick suite) is closed on each one; run that, read each image aloud, then close each image in turn. 3. run ‘Imagination’ over the config file to produce a silent MP4 video with the correct timings. 4. run ‘Audacity’ to record speech while using ‘SMPlayer’ to display the silent video, then export that recording as a WAV file. 4a. optionally, use ‘TiMIDIty’ to convert a non-copyright-encumbered MIDI tune to WAV, then import that and blend it with the speech (as a quiet b

new life for an old (FTX) PSU, improved life for one human

the LEDs on this 5m strip happen to emit light centred on a red that does unexpectedly helpful things to (and surprisingly deeply within) a human routinely exposed to it. it has been soldered to a Molex connector, plugged into a TFX power supply from a (retired: the MoBo is cactus) Small Form Factor PC, the assorted PSU connectors (and loose end from the strip) have been taped over. the LED strip cost $10.24 including postage, the rest cost $0, the PSU is running at 12½% of capacity, consumes less power than a laptop plug-pack despite running a fan. trial runs begin today.

boundaries

pushing the actual boundaries of the physical (not extremes, the boundaries themselves) can often remove barriers not otherwise perceived. one can then often resolve an issue itself, rather than merely stonewalling at the physical consequences of the issue.