Hacker News new | ask | show | jobs
by hostilefork 4272 days ago
So for the record, I wrote this four years ago. No idea why this is being linked to from here now.

Let the record show, also, that I in no way stand by this as being a sane approach. I just walked through it, trying to answer the question. I wanted to show the path to syscall and it was way wackier than I thought it would be. That's why it ended up a blog entry.

(Though when my blog went down one day, someone copied the content into the answer:

http://stackoverflow.com/revisions/2444508/3

...and they did so ignoring the license clause difference that my blog is CC-BY-NC-SA instead of CC-BY-SA. There's been a bit of a tussle over that distinction lately, with people selling StackOverflow books:

http://meta.stackoverflow.com/questions/272768/is-this-site-...

...and I'll leave it to those with more interest in the issue to decide if that is worth worrying about, because I don't actually care.)

Anyway, I'm sure the people involved in writing this had their reasons for doing it this way, and it had to do with code legacy and evolution. I don't want anyone to mistake my trace through it as endorsement. It's just what it is.

1 comments

You did miss one fun rabbit hole with printf in glibc:

Search for register_printf_function, and realize that printf is now a function that can have whatever side-effects you like (which really sucks for optimization around logging code)

Glibc may have register_printf_function, but GCC does not have register_printf_attribute, so this is not as practical as it could be. That is:

    __attribute__ ((format (printf, 1, 2)))
attaches to a function declaration to indicate that the function works like printf, so GCC can warn if a (compile time) format string does not match the variadic arguments. But there seems to be no way to extend this attribute, nor to make entirely new attributes in the same spirit.

So you can register new format specifiers for printf, but it seems you'll then have to disable warnings about bad format specifiers during compilation. Those warnings have very few false positives and catch real bugs in real code.

"But there seems to be no way to extend this attribute, nor to make entirely new attributes in the same spirit."

Yes, GCC knows, and we're good with that :)

The glibc extension here serves no earthly purpose (and on the gcc side, we've considered pretending it doesn't exist before so we can actually optimize better).

It sounds like it may be a good idea, except 1. It modifies functions that have a standard purpose, standard set of format specifiers, and are, as you point out, often error checked to make sure they match those :)

2. Unless you see the printf handler registration, which may be in a library, linked to your program, or whatever, you'll have no idea that the code is wrong.

The Plan 9 C compilers have #pragma varargck (argpos|type|flag):

http://plan9.bell-labs.com/sys/doc/compiler.html (3.8)