Re: Could use some help with variable length argument lists
--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--
討論串 (同標題文章)
完整討論串 (本文為第 2 之 5 篇):