From owner-freebsd-chat@FreeBSD.ORG Tue Feb 10 12:12:44 2004 Return-Path: Delivered-To: freebsd-chat@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E0D0016A4D4 for ; Tue, 10 Feb 2004 12:12:44 -0800 (PST) Received: from internet.potentialtech.com (h-66-167-251-6.PHLAPAFG.covad.net [66.167.251.6]) by mx1.FreeBSD.org (Postfix) with ESMTP id BBEB243D1D for ; Tue, 10 Feb 2004 12:12:44 -0800 (PST) (envelope-from wmoran@potentialtech.com) Received: from potentialtech.com (pa-plum1c-102.pit.adelphia.net [24.53.179.102]) by internet.potentialtech.com (Postfix) with ESMTP id 0253669A4B for ; Tue, 10 Feb 2004 15:12:44 -0500 (EST) Message-ID: <40293B38.30401@potentialtech.com> Date: Tue, 10 Feb 2004 15:12:40 -0500 From: Bill Moran User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.4) Gecko/20031005 X-Accept-Language: en-us, en MIME-Version: 1.0 To: freebsd-chat@freebsd.org Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Subject: Could use some help with variable length argument lists X-BeenThere: freebsd-chat@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Non technical items related to the community List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Feb 2004 20:12:45 -0000 I'm so frustrated I could cry. I want my application to be able to do all kinds of logging, so I wrote a log function: void _log(int level, const char *message, ...) { va_list ap; char **format, **errmsg; va_start(ap, message); if (level <= LOGLEVEL) { asprintf(format, "PID %d: %s", getpid(), message); vasprintf(errmsg, *format, ap); free(*format); syslog(LOG_INFO | LOG_LOCAL1, *errmsg); if (LOGLEVEL == 10) printf(*errmsg); free(*errmsg); } va_end(ap); } 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);" I'm at the end of my rope with this. I really need a way to easily log with variable arguments or I'll never get some other issues with this application debugged! If I tweak the _log function to this: void _log(int level, char *message) { if (level <= LOGLEVEL) { syslog(LOG_INFO | LOG_LOCAL1, "PID %d: %s", getpid(), message); if (LOGLEVEL == 10) printf("PID %d: %s", getpid(), message); } } everything works just dandy (I haven't changed any other code anywhere else! I'm not _using_ variable arguments to _log() yet) What is is between these two functions that causes the first one to coredump, yet the second one works fine? I can only guess that my usage of va_* isn't quite right, but how is that affecting the message pointer? -- Bill Moran Potential Technologies http://www.potentialtech.com