Re: [Freeciv-Dev] patch: provide snprintf
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
David Pfitzner (dwp@xxxxxxxxxxxxxx) wrote:
> > Does this work on all Unix systems? I would imagine that there may be
> > a few without sigaction(2) or mprotect(2) (or both).
> - Mostly full re-implementation of printf (at least format/argument
> parsing). (Eg JitterBug does this.) Ouch.
>
> - Implement enough to calculate an upper bound on the output length,
> dynamically allocate that, vsprintf to it, then strncpy, then free
> the temporary memory. (Eg glib does this for g_snprintf if there
> is no native snprintf.)
>
> Maybe one of these would suit us better.
If you do include one of these alternatives, then you could still use
the mprotect-based one. Just use autoconf to determine whether the host
system has the necessary system calls, and use the appropriate vsnprintf
implementation based on what autoconf says.
E.g., in pseudocode:
if host has vsnprintf
define HAS_VSNPRINTF
else
if host has mprotect
define HAS_MPROTECT
if host has sigaction
define HAS_SIGACTION
endif
endif
#ifndef HAS_VSPRINTF
#if defined(HAS_MPROTECT) && defined(HAS_SIGACTION)
/* slick vsnprintf */
#else
/* safe vsnprintf */
#endif
#endif
My guess would be that the majority of systems that have mprotect and
sigaction already have vsnprintf. But perhaps I'm wrong about that.
(There must be some that don't, because otherwise nobody would have
written the vsnprintf that you posted. ;-)
--
Greg Wooledge | Distributed.NET http://www.distributed.net/
wooledge@xxxxxxxxxxx | because a CPU is a terrible thing to waste.
http://www.kellnet.com/wooledge/ |
|
|