RE: Segmentation fault in vsnprintf() from /lib64/tls/libc.so.6
"Agarwal, Saumya" <[email protected]> Tue, 5 Dec 2006 23:31:49 +0530
| Newsgroups | gmane.linux.redhat.amd64 |
|---|---|
| Message-ID | <[email protected]> |
Thanks Jakub! It worked with va_copy. -----Original Message----- From: Jakub Jelinek [mailto:[email protected]]=20 Sent: Tuesday, December 05, 2006 12:25 AM To: Agarwal, Saumya Cc: [email protected] Subject: Re: Segmentation fault in vsnprintf() from /lib64/tls/libc.so.6 On Mon, Dec 04, 2006 at 11:41:02PM +0530, Agarwal, Saumya wrote: > On executing the code (snippet below) I get a segmentation fault at=20 > run time. The code builds fine. The same code runs fine on a 32-bit=20 > linux machine. > =20 > if (NULL !=3D *strp) { > for ( ; NULL !=3D *strp; ) { > left =3D *sizep - len - 1; > if (left > 0) { > result =3D vsnprintf(&(*strp)[len], left, format, ap); > if ((result !=3D -1) && (result < left)) { //vsnprintf= =20 > truncated the output string > break; > } > } > *sizep *=3D 2; > Renew(*strp, *sizep, char); //reallocate sizep amount of=20 > space to strp > } > } >=20 > The crash happens in the second iteration of the for loop. It goes=20 > through fine in the first iteration. No wonder, this is clearly invalid code, see ISO C99, 7.15(3): The type declared is va_list which is an object type suitable for holding information needed by the macros va_start, va_arg, va_end, and va_copy. If access to the varying arguments is desired, the called function shall declare an object (generally referred to as ap in this subclause) having type va_list. The object ap may be passed as an argument to another function; if that function invokes the va_arg macro with parameter ap, the value of ap in the calling function is indeterminate and shall be passed to the va_end macro prior to any further reference to ap. As vsnprintf uses va_arg on the 4th argument passed to it (ap), you really need to va_copy before you call vsnprintf (and don't forget to pass it to va_end afterwards). The va_copy man page should explain it too. Jakub --=20 amd64-list mailing list [email protected] https://www.redhat.com/mailman/listinfo/amd64-list