Could use some help with variable length argument lists

Ben Smithurst ben at FreeBSD.org
Tue Feb 10 12:20:27 PST 2004


Bill Moran wrote:

>     char **format, **errmsg;
> 
>     va_start(ap, message);
>     if (level <= LOGLEVEL) {
>         asprintf(format, "PID %d: %s", getpid(), message);
...
> Doesn't seem too difficult, right?  However, if I call the function
> from elsewhere in my application like this:
> 
> _log(1, "Log test");
> 
> I get a coredump!  gdb complains that message is "out of bounds"
> on the line "asprintf(format, "PID %d: %s", getpid(), message);"

are you sure it isn't the format variable causing the problem?  It's
a while since I've used asprintf() but from memory you should write
something more like

char *format;
...
asprintf(&format, "PID %d: %s", getpid(), message);

Your code above is passing format uninitialized (you should have got a
compile time warning about that).

Or maybe I'm misunderstanding something. :-)

-- 
Ben Smithurst / ben at FreeBSD.org                 FreeBSD: The Power To Serve
                                                    http://www.FreeBSD.org/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-chat/attachments/20040210/e1204191/attachment.bin


More information about the freebsd-chat mailing list