r/C_Programming • • 24d ago

A Decade of Kill Bugs

https://alangrow.com/blog/a-decade-of-kill-bugs
25 Upvotes

6 comments sorted by

View all comments

5

u/8d8n4mbo28026ulk 24d 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 24d ago

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.

5

u/aioeu 24d ago edited 24d ago

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.

2

u/8d8n4mbo28026ulk 24d 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 of getopt() and the batshit-insane atoi().