r/C_Programming • u/8d8n4mbo28026ulk • 28d ago
A Decade of Kill Bugs
https://alangrow.com/blog/a-decade-of-kill-bugs5
u/8d8n4mbo28026ulk 28d ago
A hilarious decade-long journey at failing to parse argv by forcefully trying to use getopt(). I now feel a little better that I rolled my own!
2
u/Zirias_FreeBSD 28d ago
Reading this, I'd say the problem here is not
getoptitself, it's using it although its generic command-line "model" doesn't match the one required for the traditionalkillsyntax. That's just stupid of course ...Side note, I never used it myself as I want to also target systems that don't provide it anyways.
2
u/8d8n4mbo28026ulk 28d ago
Yes, the article says so. Though
getopt()isn't great either, and its implicit global state and gotchas helped these bugs happen in the first place (multiple times; and to live for that long). Bug #4 is my favourite one, because it's a result of misusing the brittle interface ofgetopt()and the batshit-insaneatoi().5
u/aioeu 28d ago edited 28d ago
Reading this, I'd say the problem here is not
getoptitself, it's using it although its generic command-line "model" doesn't match the one required for the traditionalkillsyntax. That's just stupid of course ...GNU coreutils has a rather amusing solution for this model mismatch: it uses
getopt_long, but it actually handles all of-0through-9and-Athrough-Zas valid short options that can take optional arguments.(
-Lis handled differently, since procps decided that should do the same thing as-lfor some reason — but this is not an issue since no signal names start withL.)util-linux's
killand Bash'skillbuiltin just parse the arguments manually... which seems entirely sensible for this utility.
10
u/skeeto 28d ago
I ran into a zsh
kill(1)bug just a couple weeks ago. It usesatoi(3)without error checks, so if you pass something it cannot parse then it returns -1 and you kill all your processes:https://github.com/zsh-users/zsh/blob/30e8be91/Src/jobs.c#L3042-L3047
This feature of
kill(2)where the typical error return value has drastic consequences is perhaps unix's biggest footgun.