public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] avoid assuming all integers are representable in HOST_WIDE_INT (PR #80497)
@ 2017-04-25  6:07 Martin Sebor
  2017-04-25 14:39 ` Jeff Law
  0 siblings, 1 reply; 3+ messages in thread
From: Martin Sebor @ 2017-04-25  6:07 UTC (permalink / raw)
  To: Gcc Patch List

[-- Attachment #1: Type: text/plain, Size: 446 bytes --]

Bug 80497 brings to light that my fix for PR 80364 where I corrected
the handling for int128_t was incomplete.  I handled the non-constant
case but missed the INTEGER_CST case just a few lines above.  The
attached patch also corrects that problem plus one more elsewhere
in the pass.

Both of the changes in this patch seem safe enough to make even now
in GCC 7 but since they are ice-on-invalid-code perhaps it's better
to wait for 7.1?

Martin

[-- Attachment #2: gcc-80497.diff --]
[-- Type: text/x-patch, Size: 2792 bytes --]

PR tree-optimization/80497 - ICE at -O1 and above on valid code on x86_64-linux-gnu in tree_to_uhwi

gcc/ChangeLog:

	PR tree-optimization/80497
	* gimple-ssa-sprintf.c (get_int_range): Avoid assuming all integer
	constants are representable in HOST_WIDE_INT.
	(parse_directive): Ditto.

gcc/testsuite/ChangeLog:

	PR tree-optimization/80497
	* gcc.dg/tree-ssa/builtin-sprintf-warn-17.c: New test.

diff --git a/gcc/gimple-ssa-sprintf.c b/gcc/gimple-ssa-sprintf.c
index 2e62086..d3771dd 100644
--- a/gcc/gimple-ssa-sprintf.c
+++ b/gcc/gimple-ssa-sprintf.c
@@ -948,7 +948,8 @@ get_int_range (tree arg, HOST_WIDE_INT *pmin, HOST_WIDE_INT *pmax,
       *pmin = tree_to_shwi (TYPE_MIN_VALUE (type));
       *pmax = tree_to_shwi (TYPE_MAX_VALUE (type));
     }
-  else if (TREE_CODE (arg) == INTEGER_CST)
+  else if (TREE_CODE (arg) == INTEGER_CST
+	   && TYPE_PRECISION (TREE_TYPE (arg)) <= TYPE_PRECISION (type))
     {
       /* For a constant argument return its value adjusted as specified
 	 by NEGATIVE and NEGBOUND and return true to indicate that the
@@ -2916,7 +2917,9 @@ parse_directive (pass_sprintf_length::call_info &info,
       if (width != -1)
 	dollar = width + info.argidx;
       else if (star_width
-	       && TREE_CODE (star_width) == INTEGER_CST)
+	       && TREE_CODE (star_width) == INTEGER_CST
+	       && (TYPE_PRECISION (TREE_TYPE (star_width))
+		   <= TYPE_PRECISION (integer_type_node)))
 	dollar = width + tree_to_shwi (star_width);
 
       /* Bail when the numbered argument is out of range (it will
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-17.c b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-17.c
new file mode 100644
index 0000000..27aa839
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-17.c
@@ -0,0 +1,42 @@
+/* PR tree-optimization/80497 - ICE at -O1 and above on valid code on
+   x86_64-linux-gnu in "tree_to_uhwi"
+   { dg-do compile }
+   { dg-options "-O2 -Wall -Wformat-overflow" }
+   { dg-require-effective-target int128 } */
+
+extern char buf[];
+
+const __int128_t sint128_max
+  = (__int128_t)1 << (sizeof sint128_max * __CHAR_BIT__ - 2);
+
+void fn0 (void)
+{
+  __int128_t si128 = 0;
+
+  __builtin_sprintf (buf, "%*i", si128, 0);
+
+  __builtin_sprintf (buf, "%.*i", si128, 0);
+
+  __builtin_sprintf (buf, "%i", si128);
+
+  __builtin_sprintf (buf, "%2$*1$i", si128, 0);
+
+  __builtin_sprintf (buf, "%2$.*1$i", si128, 0);
+}
+
+void fn1 (void)
+{
+  __int128_t si128 = sint128_max;
+
+  __builtin_sprintf (buf, "%*i", si128, 0);
+
+  __builtin_sprintf (buf, "%.*i", si128, 0);
+
+  __builtin_sprintf (buf, "%i", si128);
+
+  __builtin_sprintf (buf, "%2$*1$i", si128, 0);
+
+  __builtin_sprintf (buf, "%2$.*1$i", si128, 0);
+}
+
+/* { dg-prune-output "expects argument of type .int." } */

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] avoid assuming all integers are representable in HOST_WIDE_INT (PR #80497)
  2017-04-25  6:07 [PATCH] avoid assuming all integers are representable in HOST_WIDE_INT (PR #80497) Martin Sebor
@ 2017-04-25 14:39 ` Jeff Law
  2017-04-25 15:09   ` Jakub Jelinek
  0 siblings, 1 reply; 3+ messages in thread
From: Jeff Law @ 2017-04-25 14:39 UTC (permalink / raw)
  To: Martin Sebor, Gcc Patch List

On 04/24/2017 05:35 PM, Martin Sebor wrote:
> Bug 80497 brings to light that my fix for PR 80364 where I corrected
> the handling for int128_t was incomplete.  I handled the non-constant
> case but missed the INTEGER_CST case just a few lines above.  The
> attached patch also corrects that problem plus one more elsewhere
> in the pass.
> 
> Both of the changes in this patch seem safe enough to make even now
> in GCC 7 but since they are ice-on-invalid-code perhaps it's better
> to wait for 7.1?
> 
> Martin
> 
> gcc-80497.diff
> 
> 
> PR tree-optimization/80497 - ICE at -O1 and above on valid code on x86_64-linux-gnu in tree_to_uhwi
> 
> gcc/ChangeLog:
> 
> 	PR tree-optimization/80497
> 	* gimple-ssa-sprintf.c (get_int_range): Avoid assuming all integer
> 	constants are representable in HOST_WIDE_INT.
> 	(parse_directive): Ditto.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR tree-optimization/80497
> 	* gcc.dg/tree-ssa/builtin-sprintf-warn-17.c: New test.
OK for the trunk.  Jakub's call on when it's OK for the branch -- one 
can easily argue this is a regression.

jeff

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] avoid assuming all integers are representable in HOST_WIDE_INT (PR #80497)
  2017-04-25 14:39 ` Jeff Law
@ 2017-04-25 15:09   ` Jakub Jelinek
  0 siblings, 0 replies; 3+ messages in thread
From: Jakub Jelinek @ 2017-04-25 15:09 UTC (permalink / raw)
  To: Jeff Law; +Cc: Martin Sebor, Gcc Patch List

On Tue, Apr 25, 2017 at 08:24:32AM -0600, Jeff Law wrote:
> On 04/24/2017 05:35 PM, Martin Sebor wrote:
> > Bug 80497 brings to light that my fix for PR 80364 where I corrected
> > the handling for int128_t was incomplete.  I handled the non-constant
> > case but missed the INTEGER_CST case just a few lines above.  The
> > attached patch also corrects that problem plus one more elsewhere
> > in the pass.
> > 
> > Both of the changes in this patch seem safe enough to make even now
> > in GCC 7 but since they are ice-on-invalid-code perhaps it's better
> > to wait for 7.1?
> > 
> > Martin
> > 
> > gcc-80497.diff
> > 
> > 
> > PR tree-optimization/80497 - ICE at -O1 and above on valid code on x86_64-linux-gnu in tree_to_uhwi
> > 
> > gcc/ChangeLog:
> > 
> > 	PR tree-optimization/80497
> > 	* gimple-ssa-sprintf.c (get_int_range): Avoid assuming all integer
> > 	constants are representable in HOST_WIDE_INT.
> > 	(parse_directive): Ditto.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> > 	PR tree-optimization/80497
> > 	* gcc.dg/tree-ssa/builtin-sprintf-warn-17.c: New test.
> OK for the trunk.  Jakub's call on when it's OK for the branch -- one can
> easily argue this is a regression.

Ok for 7.1 if you manage to commit before 7.1rc1, otherwise ok for 7.2.

	Jakub

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-04-25 14:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-25  6:07 [PATCH] avoid assuming all integers are representable in HOST_WIDE_INT (PR #80497) Martin Sebor
2017-04-25 14:39 ` Jeff Law
2017-04-25 15:09   ` Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).