Re: Could use some help with variable length argument lists

看板FB_chat作者時間22年前 (2004/02/11 04:20), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/5 (看更多)
--AhhlLboLdkugWU4S Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Bill Moran wrote: > char **format, **errmsg; >=20 > va_start(ap, message); > if (level <=3D LOGLEVEL) { > asprintf(format, "PID %d: %s", getpid(), message); =2E.. > Doesn't seem too difficult, right? However, if I call the function > from elsewhere in my application like this: >=20 > _log(1, "Log test"); >=20 > 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; =2E.. 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. :-) --=20 Ben Smithurst / ben@FreeBSD.org FreeBSD: The Power To Serve http://www.FreeBSD.org/ --AhhlLboLdkugWU4S Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (FreeBSD) iD8DBQFAKT0JbPzJ+yzvRCwRAu+3AKCzkW07ait8hjbFAmEgQLqYwCFvsgCfbAYw IqL9xzbG7pz2KQMlxCACfik= =keic -----END PGP SIGNATURE----- --AhhlLboLdkugWU4S--
文章代碼(AID): #10AJqe00 (FB_chat)
討論串 (同標題文章)
文章代碼(AID): #10AJqe00 (FB_chat)