<p dir="ltr">It's interesting. Need to check lldb for other places where it can affect.</p>
<p dir="ltr">As I understood the general rule is to cast value to type (that it should be printed as) before passing to printf.</p>
<p dir="ltr">Thanks, <br>
Ilia</p>
<div class="gmail_quote">On Apr 28, 2015 6:47 PM, "Pavel Labath" <<a href="mailto:labath@google.com">labath@google.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Interesting.. I learned something new today... :)<br>
<br>
<br>
<br>
On 28 April 2015 at 16:40, Abid, Hafiz <<a href="mailto:Hafiz_Abid@mentor.com">Hafiz_Abid@mentor.com</a>> wrote:<br>
> PRIx8 is defined as "x" in /usr/include/inttypes.h. This is Ubuntu 14.04.<br>
><br>
> #include <stdio.h><br>
>  #include <string.h><br>
>  #include <inttypes.h><br>
> int main()<br>
> {<br>
>   char cUnescapedChar = 0xeb;<br>
>   const int old_size = sizeof("\\xXX");<br>
>   char strEscapedChar[old_size];<br>
>   ::sprintf(strEscapedChar, "\\x%02" PRIx8, cUnescapedChar);<br>
>   int new_size = strlen(strEscapedChar);<br>
>   printf("old:%d  new:%d\n", old_size, new_size);<br>
>   printf("%s\n", strEscapedChar);<br>
>   return 0;<br>
> }<br>
><br>
> gives this output on my system (with g++  4.8.2).<br>
> old:5  new:10<br>
> \xffffffeb<br>
> *** stack smashing detected ***: ./test terminated<br>
> Aborted (core dumped)<br>
><br>
> Casting the cUnescapedChar to unsigned char also fixes the crash.<br>
><br>
> Regards,<br>
> Abid<br>
><br>
>> -----Original Message-----<br>
>> From: Pavel Labath [mailto:<a href="mailto:labath@google.com">labath@google.com</a>]<br>
>> Sent: 28 April 2015 16:06<br>
>> To: Abid, Hafiz<br>
>> Cc: Ilia K; <a href="mailto:lldb-commits@cs.uiuc.edu">lldb-commits@cs.uiuc.edu</a><br>
>> Subject: Re: [Lldb-commits] [lldb] r235991 - Replace sprintf with snprintf to<br>
>> avoid a crash.<br>
>><br>
>> I find this surprising as well...<br>
>><br>
>> what is PRIx8 defined to on your system? Normally, it should be something<br>
>> like "hhx" as Ilia suggested. Here, "hh" should cast the value to char and "x"<br>
>> to unsigned, so there should be no possibility of overflow.<br>
>><br>
>> Unless I am misunderstanding something...<br>
>><br>
>> cheers,<br>
>> pl<br>
>><br>
>><br>
>> On 28 April 2015 at 15:55, Abid, Hafiz <<a href="mailto:Hafiz_Abid@mentor.com">Hafiz_Abid@mentor.com</a>> wrote:<br>
>> > Well, it was happening :-(. It is \\x%02x” and cUnescapedChar is not<br>
>> > unsigned.<br>
>> ><br>
>> > So when it had value with MSB 1, then it overflew the buffer.<br>
>> ><br>
>> ><br>
>> ><br>
>> > There are other problems too with printing the 1-byte register.<br>
>> > Current code<br>
>> ><br>
>> > tries to print it as a character which is probably not the right<br>
>> > thing. But that<br>
>> ><br>
>> > is a separate fix.<br>
>> ><br>
>> ><br>
>> ><br>
>> > Regards,<br>
>> ><br>
>> > Abid<br>
>> ><br>
>> ><br>
>> ><br>
>> > From: Ilia K [mailto:<a href="mailto:ki.stfu@gmail.com">ki.stfu@gmail.com</a>]<br>
>> > Sent: 28 April 2015 15:26<br>
>> > To: Abid, Hafiz<br>
>> > Cc: <a href="mailto:lldb-commits@cs.uiuc.edu">lldb-commits@cs.uiuc.edu</a><br>
>> > Subject: Re: [Lldb-commits] [lldb] r235991 - Replace sprintf with<br>
>> > snprintf to avoid a crash.<br>
>> ><br>
>> ><br>
>> ><br>
>> > Hello Abid,<br>
>> ><br>
>> ><br>
>> ><br>
>> > I thought it never can happen because cUnescapedChar is less than 255<br>
>> > and "\\x%02hhu" prints something like \x12. Am I wrong?<br>
>> ><br>
>> ><br>
>> ><br>
>> > Thanks,<br>
>> ><br>
>> > Ilia<br>
>> ><br>
>> ><br>
>> ><br>
>> ><br>
>> ><br>
>> > On Tue, Apr 28, 2015 at 5:16 PM, Hafiz Abid Qadeer<br>
>> > <<a href="mailto:hafiz_abid@mentor.com">hafiz_abid@mentor.com</a>><br>
>> > wrote:<br>
>> ><br>
>> > Author: abidh<br>
>> > Date: Tue Apr 28 09:16:00 2015<br>
>> > New Revision: 235991<br>
>> ><br>
>> > URL: <a href="http://llvm.org/viewvc/llvm-project?rev=235991&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=235991&view=rev</a><br>
>> > Log:<br>
>> > Replace sprintf with snprintf to avoid a crash.<br>
>> > During testing -data-list-register-values, I saw a crash here due to<br>
>> > buffer overflow.<br>
>> > This commit should fix the crash. There is still problem with printing<br>
>> > 1-byte register in some cases that I will fix separately.<br>
>> ><br>
>> > No regression on MI test cases.<br>
>> ><br>
>> ><br>
>> > Modified:<br>
>> >     lldb/trunk/tools/lldb-mi/MIUtilString.cpp<br>
>> ><br>
>> > Modified: lldb/trunk/tools/lldb-mi/MIUtilString.cpp<br>
>> > URL:<br>
>> > <a href="http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-mi/MIUtilStr" target="_blank">http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-mi/MIUtilStr</a><br>
>> > ing.cpp?rev=235991&r1=235990&r2=235991&view=diff<br>
>> ><br>
>> ================================================================<br>
>> ======<br>
>> > ========<br>
>> > --- lldb/trunk/tools/lldb-mi/MIUtilString.cpp (original)<br>
>> > +++ lldb/trunk/tools/lldb-mi/MIUtilString.cpp Tue Apr 28 09:16:00 2015<br>
>> > @@ -17,6 +17,7 @@<br>
>> ><br>
>> >  // In-house headers:<br>
>> >  #include "MIUtilString.h"<br>
>> > +#include "Platform.h"<br>
>> ><br>
>> >  //++<br>
>> > ----------------------------------------------------------------------<br>
>> > --------------  // Details: CMIUtilString constructor.<br>
>> > @@ -844,8 +845,9 @@ CMIUtilString::Escape(const bool vbEscap<br>
>> >                      strNew.push_back(cUnescapedChar);<br>
>> >                  else<br>
>> >                  {<br>
>> > -                    char strEscapedChar[sizeof("\\xXX")];<br>
>> > -                    ::sprintf(strEscapedChar, "\\x%02" PRIx8,<br>
>> > cUnescapedChar);<br>
>> > +                    const size_t size = sizeof("\\xXX");<br>
>> > +                    char strEscapedChar[size];<br>
>> > +                    ::snprintf(strEscapedChar, size, "\\x%02" PRIx8,<br>
>> > cUnescapedChar);<br>
>> >                      strNew.append(strEscapedChar);<br>
>> >                  }<br>
>> >                  break;<br>
>> ><br>
>> ><br>
>> > _______________________________________________<br>
>> > lldb-commits mailing list<br>
>> > <a href="mailto:lldb-commits@cs.uiuc.edu">lldb-commits@cs.uiuc.edu</a><br>
>> > <a href="http://lists.cs.uiuc.edu/mailman/listinfo/lldb-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/lldb-commits</a><br>
>> ><br>
>> ><br>
>> ><br>
>> ><br>
>> > _______________________________________________<br>
>> > lldb-commits mailing list<br>
>> > <a href="mailto:lldb-commits@cs.uiuc.edu">lldb-commits@cs.uiuc.edu</a><br>
>> > <a href="http://lists.cs.uiuc.edu/mailman/listinfo/lldb-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/lldb-commits</a><br>
>> ><br>
</blockquote></div>