Segmentation fault in vsnprintf() from /lib64/tls/libc.so.6

"Agarwal, Saumya" <[email protected]> Mon, 4 Dec 2006 23:41:02 +0530
Newsgroups gmane.linux.redhat.amd64
Message-ID <[email protected]>
This is a multi-part message in MIME format.

--===============1420400633==
Content-class: urn:content-classes:message
Content-Type: multipart/alternative;
	boundary="----_=_NextPart_001_01C717CF.A505F125"

This is a multi-part message in MIME format.

------_=_NextPart_001_01C717CF.A505F125
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi,
=20
I am executing a piece of code which continually tries to do the sprintf
into the allocated buffer on a 64-bit RedHat linux machine.=20
=20
Here are the details of the system and the gcc version used -=20
=20
bash-3.00$ uname -a
Linux saumya.foo.com 2.6.9-5.ELsmp #1 SMP Wed Jan 5 19:29:47 EST 2005
x86_64 x86_64 x86_64 GNU/Linux

bash-3.00$ gcc -v
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.3/specs
Configured with: ../configure --prefix=3D/usr --mandir=3D/usr/share/man
--infodir=3D/usr/share/info --enable-shared --enable-threads=3Dposix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-languages=3Dc,c++,objc,java,f77
--enable-java-awt=3Dgtk --host=3Dx86_64-redhat-linux
Thread model: posix
gcc version 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)
=20
On executing the code (snippet below) I get a segmentation fault at run
time. The code builds fine. The same code runs fine on a 32-bit 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
truncated the output string
                    break;
                }
            }
            *sizep *=3D 2;
            Renew(*strp, *sizep, char); //reallocate sizep amount of
space to strp
        }
    }

The crash happens in the second iteration of the for loop. It goes
through fine in the first iteration.
=20
Here is the gdb backtrace -
=20
#0  0x00000037d776fc10 in strlen () from /lib64/tls/libc.so.6
#1  0x00000037d7742b4b in vfprintf () from /lib64/tls/libc.so.6
#2  0x00000037d7761ce4 in vsnprintf () from /lib64/tls/libc.so.6
#3  0x00000000004965a6 in str_vappend (strp=3D0x7fbfffe790,
sizep=3D0x7fbfffe788,=20
    format=3D0x4adf1b "%s /%s HTTP/1.1\r\n", ap=3D0x7fbfffe7e0) at =
str.c:684
=20
=20
Is this a known issue with vsnprintf() on 64-bit linux platforms? Is
there a fix or any workaround available?
=20
Thanks,
saumya

------_=_NextPart_001_01C717CF.A505F125
Content-Type: text/html;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dus-ascii">
<META content=3D"MSHTML 6.00.2800.1561" name=3DGENERATOR></HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D294583614-04122006>Hi,</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D294583614-04122006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D294583614-04122006>I am =
executing a=20
piece of code&nbsp;which continually tries to do the sprintf into the =
allocated=20
buffer on a 64-bit RedHat linux machine.&nbsp;</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D294583614-04122006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D294583614-04122006>Here =
are the details=20
of the system and the gcc version used -&nbsp;</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>bash-3.00$ uname -a<BR>Linux&nbsp;<SPAN =

class=3D294583614-04122006>saumya.foo.com </SPAN>2.6.9-5.ELsmp #1 SMP =
Wed Jan 5=20
19:29:47 EST 2005 x86_64 x86_64 x86_64 GNU/Linux<BR></FONT><FONT =
face=3DArial=20
size=3D2></DIV></FONT>
<DIV><FONT face=3DArial size=3D2>bash-3.00$ gcc -v<BR>Reading specs from =

/usr/lib/gcc/x86_64-redhat-linux/3.4.3/specs<BR>Configured with: =
../configure=20
--prefix=3D/usr --mandir=3D/usr/share/man --infodir=3D/usr/share/info =
--enable-shared=20
--enable-threads=3Dposix --disable-checking --with-system-zlib=20
--enable-__cxa_atexit --disable-libunwind-exceptions=20
--enable-languages=3Dc,c++,objc,java,f77 --enable-java-awt=3Dgtk=20
--host=3Dx86_64-redhat-linux<BR>Thread model: posix<BR>gcc version 3.4.3 =
20041212=20
(Red Hat 3.4.3-9.EL4)</FONT><FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><SPAN class=3D294583614-04122006><FONT face=3DArial size=3D2>On =
executing the=20
code (snippet below) I get a segmentation fault at run time. The code =
builds=20
fine. The same code runs fine on a 32-bit linux =
machine.</FONT></SPAN></DIV>
<DIV><SPAN class=3D294583614-04122006><FONT face=3DArial=20
size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D294583614-04122006><FONT face=3DArial =
size=3D2>&nbsp;if (NULL !=3D=20
*strp) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( ; NULL !=3D =
*strp; )=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
left =3D=20
*sizep - len -=20
1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
if=20
(left &gt; 0)=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;=20
result =3D vsnprintf(&amp;(*strp)[len], left, format,=20
ap);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;=20
if ((result !=3D -1) &amp;&amp; (result &lt; left)) =
{&nbsp;&nbsp;//vsnprintf=20
truncated the output=20
string<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;=20
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
*sizep=20
*=3D =
2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =

Renew(*strp, *sizep, char); //reallocate sizep amount of&nbsp;space to=20
strp<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
}<BR>&nbsp;&nbsp;&nbsp;=20
}<BR></FONT></SPAN></DIV>
<DIV><SPAN class=3D294583614-04122006><FONT face=3DArial size=3D2>The =
crash happens in=20
the second iteration of the for loop. It goes through fine in the first=20
iteration.</FONT></SPAN></DIV>
<DIV><SPAN class=3D294583614-04122006><FONT face=3DArial=20
size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D294583614-04122006><FONT face=3DArial size=3D2>Here =
is the gdb=20
backtrace -</FONT></SPAN></DIV></FONT>
<DIV><FONT><SPAN class=3D294583614-04122006><FONT face=3DArial=20
size=3D2></FONT></SPAN>&nbsp;</DIV></FONT>
<DIV><FONT face=3DArial size=3D2>#0&nbsp; 0x00000037d776fc10 in strlen =
() from=20
/lib64/tls/libc.so.6<BR>#1&nbsp; 0x00000037d7742b4b in vfprintf () from=20
/lib64/tls/libc.so.6<BR>#2&nbsp; 0x00000037d7761ce4 in vsnprintf () from =

/lib64/tls/libc.so.6<BR>#3&nbsp; 0x00000000004965a6 in str_vappend=20
(strp=3D0x7fbfffe790, sizep=3D0x7fbfffe788, <BR>&nbsp;&nbsp;&nbsp; =
format=3D0x4adf1b=20
"%s /%s HTTP/1.1\r\n", ap=3D0x7fbfffe7e0) at str.c:684</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT><SPAN class=3D294583614-04122006><FONT face=3DArial =
size=3D2>Is this a=20
known issue with vsnprintf() on 64-bit linux platforms? Is there a fix =
or any=20
workaround available?</FONT></SPAN></FONT></DIV>
<DIV><FONT><SPAN class=3D294583614-04122006><FONT face=3DArial=20
size=3D2></FONT></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT><SPAN class=3D294583614-04122006><FONT face=3DArial=20
size=3D2>Thanks,</FONT></SPAN></FONT></DIV>
<DIV><FONT><SPAN class=3D294583614-04122006><FONT face=3DArial=20
size=3D2>saumya</FONT></SPAN></DIV></FONT></BODY></HTML>

------_=_NextPart_001_01C717CF.A505F125--


--===============1420400633==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline