Could use some help with variable length argument lists

Bill Moran wmoran at potentialtech.com
Tue Feb 10 12:34:44 PST 2004


Ben Smithurst wrote:
> 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. :-)

Nope.  You're understanding far more clearly than I, today.

Your suggestion fixed it!  I owe you a big one, if you're ever in
western PA, give me a call, the drinks are on me - you've earned it!

I guess it just goes to show that I've been sitting in front of this
computer too long ;)

-- 
Bill Moran
Potential Technologies
http://www.potentialtech.com



More information about the freebsd-chat mailing list