This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: glibc segfault on "special" long double values is _ok_!?
On 11 Jun 2007, Maciej W. Rozycki stated:
> I gather for those people an xprintf() (and others as necessary) that
> wraps around printf() and traps the necessary signals is the solution.
> You could also use a similar approach to validate each of the function's
> arguments one by one beforehand so that the offending ones are actually
> caught and passed to the diagnostic facility that you have in your
> software for detailed examination.
A faster approach I implemented once is to trap SIGSEGV and try an
sprintf() instead; if it fails, mmap() a `big enough' buffer and try
again. If *that* fails, you can be reasonably sure that either someone
is trying to print insanely vast arguments (a bug in itself) or that one
or more arguments are NULL (or non-null-terminated strings or something
similarly evil).
(This is all decidedly ugly and probably best avoided unless you
actually need to do it, but it's not actually all that slow: the fast
path is an sprintf(), a printf() and two signal disposition resets.)
It's probably often easier to reimplement printf() so that you have more
control over its internals, as gnulib has done.