diff mbox

[bootstrap-O1] change value type to avoid sprintf buffer size warning

Message ID orinpsx5cp.fsf@lxoliva.fsfla.org
State New
Headers show

Commit Message

Alexandre Oliva Jan. 6, 2017, 2:57 a.m. UTC
On Jan  5, 2017, Andreas Schwab <schwab@linux-m68k.org> wrote:

> On Jan 05 2017, Alexandre Oliva <aoliva@redhat.com> wrote:
>> On Jan  5, 2017, Andreas Schwab <schwab@linux-m68k.org> wrote:
>> 
>>> On Jan 05 2017, Alexandre Oliva <aoliva@redhat.com> wrote:
>>>> -      sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
>>>> +      sprintf (xname, "<U%4hx>", ((unsigned short)((uintptr_t)(t) & 0xffff)));
>> 
>>> Please fix the spacing while you are at it.
>> 
>> Err...  I sure would, if I knew what fix you had in mind.  Care to share
>> your thoughts?  Thanks,

> Space after cast.

Wow, thanks, I think I never got that one right.

Here's what I'm installing, also breaking the now-too-long line:


In stage2 of bootstrap-O1, the code that warns if sprintf might
overflow its output buffer cannot tell that an unsigned value narrowed
to 16 bits will fit in 4 bytes with %4x.

Converting the value to 'unsigned short' makes it obvious that it
fits, at least on machines with 16-bit shorts.

for  gcc/c-family/ChangeLog

	* c-pretty-print.c (pp_c_tree_decl_identifier): Convert 16-bit
	value to unsigned short to fit in 4 hex digits without
	warnings.
---
 gcc/c-family/c-pretty-print.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Andreas Schwab Jan. 6, 2017, 9:05 a.m. UTC | #1
On Jan 06 2017, Alexandre Oliva <aoliva@redhat.com> wrote:

> On Jan  5, 2017, Andreas Schwab <schwab@linux-m68k.org> wrote:
>
>> On Jan 05 2017, Alexandre Oliva <aoliva@redhat.com> wrote:
>>> On Jan  5, 2017, Andreas Schwab <schwab@linux-m68k.org> wrote:
>>> 
>>>> On Jan 05 2017, Alexandre Oliva <aoliva@redhat.com> wrote:
>>>>> -      sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
>>>>> +      sprintf (xname, "<U%4hx>", ((unsigned short)((uintptr_t)(t) & 0xffff)));
>>> 
>>>> Please fix the spacing while you are at it.
>>> 
>>> Err...  I sure would, if I knew what fix you had in mind.  Care to share
>>> your thoughts?  Thanks,
>
>> Space after cast.
>
> Wow, thanks, I think I never got that one right.
>
> Here's what I'm installing, also breaking the now-too-long line:

Alternatively you could remove the redundant parens.

Andreas.
diff mbox

Patch

diff --git a/gcc/c-family/c-pretty-print.c b/gcc/c-family/c-pretty-print.c
index 90428ca..2908669 100644
--- a/gcc/c-family/c-pretty-print.c
+++ b/gcc/c-family/c-pretty-print.c
@@ -2400,7 +2400,8 @@  pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
   else
     {
       static char xname[8];
-      sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
+      sprintf (xname, "<U%4hx>", ((unsigned short) ((uintptr_t) (t)
+						    & 0xffff)));
       name = xname;
     }