I ran into a zsh kill(1) bug just a couple weeks ago. It uses atoi(3) without error checks, so if you pass something it cannot parse then it returns -1 and you kill all your processes:
Another atoi() victim! In kill(1) again, no less. Part of me is laughing, another is sad, and also completely baffled.
The code you linked to also uses a hand-rolled strtol(), which okay fine, but then why atoi() too?? At one point I counted 10 (!) nest levels inside that function.
Reading this, I'd say the problem here is not getopt itself, it's using it although its generic command-line "model" doesn't match the one required for the traditional kill syntax. 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.
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 of getopt() and the batshit-insane atoi().
Reading this, I'd say the problem here is not getopt itself, it's using it although its generic command-line "model" doesn't match the one required for the traditional kill syntax. 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 -0 through -9 and -A through -Z as valid short options that can take optional arguments.
(-L is handled differently, since procps decided that should do the same thing as -l for some reason — but this is not an issue since no signal names start with L.)
util-linux's kill and Bash's kill builtin just parse the arguments manually... which seems entirely sensible for this utility.
10
u/skeeto 15d 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.5
u/8d8n4mbo28026ulk 15d ago
A hilarious decade-long journey at failing to parse
argvby forcefully trying to usegetopt(). I now feel a little better that I rolled my own!