On Jan  4, 2017, Martin Sebor <mse...@gmail.com> wrote:

> The manual recommends to use a length modifier to constrain the length
> of output to that of a narrower type:

>   sprintf (xname, "<U%4hx>", ((unsigned short)((uintptr_t)(t) & 0xffff)));

> This should work even without optimization.

It might not work if short happens to be wider than 16 bits, but I guess
we need not worry about that at -O1.

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.

Regstrapped on x86_64-linux-gnu and i686-linux-gnu.  Ok to install?

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 |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/c-family/c-pretty-print.c b/gcc/c-family/c-pretty-print.c
index 90428ca..07fdbae 100644
--- a/gcc/c-family/c-pretty-print.c
+++ b/gcc/c-family/c-pretty-print.c
@@ -2400,7 +2400,7 @@ 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;
     }
 


-- 
Alexandre Oliva, freedom fighter    http://FSFLA.org/~lxoliva/
You must be the change you wish to see in the world. -- Gandhi
Be Free! -- http://FSFLA.org/   FSF Latin America board member
Free Software Evangelist|Red Hat Brasil GNU Toolchain Engineer

Reply via email to