public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [patch c++]: Fix PR/53904
@ 2014-11-20 20:14 Kai Tietz
  2014-11-21 11:42 ` Richard Biener
  2014-11-25 20:15 ` Jason Merrill
  0 siblings, 2 replies; 7+ messages in thread
From: Kai Tietz @ 2014-11-20 20:14 UTC (permalink / raw)
  To: GCC Patches

Hello,

this issue fixes a type-overflow issue caused by trying to cast a UHWI
via tree_to_shwi.
As soon as value gets larger then SHWI_MAX, we get an error for it.
So we need to cast it
via tree_to_uhwi, and then casting it to the signed variant.

ChangeLog

2014-11-20  Kai Tietz  <ktietz@redhat.com>

    PR c++/63904
    * constexpr.c (cxx_eval_vec_init_1): Avoid
    type-overflow issue.

2014-11-20  Kai Tietz  <ktietz@redhat.com>

    PR c++/63904
    * g++.dg/cpp0x/pr63904.C: New.


Regression tested for x86_64-unknown-linux-gnu.  Ok for apply?

Regards,
Kai

Index: gcc/gcc/cp/constexpr.c
===================================================================
--- gcc.orig/gcc/cp/constexpr.c
+++ gcc/gcc/cp/constexpr.c
@@ -2006,12 +2050,12 @@ cxx_eval_vec_init_1 (const constexpr_ctx
              bool *non_constant_p, bool *overflow_p)
 {
   tree elttype = TREE_TYPE (atype);
-  int max = tree_to_shwi (array_type_nelts (atype));
+  HOST_WIDE_INT max = (HOST_WIDE_INT) tree_to_uhwi (array_type_nelts (atype));
   verify_ctor_sanity (ctx, atype);
   vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
   vec_alloc (*p, max + 1);
   bool pre_init = false;
-  int i;
+  HOST_WIDE_INT i;

   /* For the default constructor, build up a call to the default
      constructor of the element type.  We only need to handle class types
Index: gcc/gcc/testsuite/g++.dg/cpp0x/pr63904.C
===================================================================
--- /dev/null
+++ gcc/gcc/testsuite/g++.dg/cpp0x/pr63904.C
@@ -0,0 +1,13 @@
+// { dg-do compile { target c++11 } }
+
+template<int N>
+struct foo {
+    constexpr foo() : a() {}
+    int a[N];
+};
+
+int main() {
+  foo< (foo<1>{}).a[0] > f;
+  return 0;
+}
+

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

* Re: [patch c++]: Fix PR/53904
  2014-11-20 20:14 [patch c++]: Fix PR/53904 Kai Tietz
@ 2014-11-21 11:42 ` Richard Biener
  2014-11-25 20:15 ` Jason Merrill
  1 sibling, 0 replies; 7+ messages in thread
From: Richard Biener @ 2014-11-21 11:42 UTC (permalink / raw)
  To: Kai Tietz; +Cc: GCC Patches

On Thu, Nov 20, 2014 at 8:48 PM, Kai Tietz <ktietz70@googlemail.com> wrote:
> Hello,
>
> this issue fixes a type-overflow issue caused by trying to cast a UHWI
> via tree_to_shwi.
> As soon as value gets larger then SHWI_MAX, we get an error for it.
> So we need to cast it
> via tree_to_uhwi, and then casting it to the signed variant.

I think it's better to handle the degenerate case (no element) explicitely.
And I would think that sth like "nelts" should have a positive result,
thus why is 'max' not unsigned?  Also 'max' and using 'nelts' looks
like a mismatch?  max == nelts - 1.  Ah, because array_type_nelts
returns nelts - 1 ... how useful ;)

Still you want to special-case the array_type_nelts == -1 case.

Richard.

> ChangeLog
>
> 2014-11-20  Kai Tietz  <ktietz@redhat.com>
>
>     PR c++/63904
>     * constexpr.c (cxx_eval_vec_init_1): Avoid
>     type-overflow issue.
>
> 2014-11-20  Kai Tietz  <ktietz@redhat.com>
>
>     PR c++/63904
>     * g++.dg/cpp0x/pr63904.C: New.
>
>
> Regression tested for x86_64-unknown-linux-gnu.  Ok for apply?
>
> Regards,
> Kai
>
> Index: gcc/gcc/cp/constexpr.c
> ===================================================================
> --- gcc.orig/gcc/cp/constexpr.c
> +++ gcc/gcc/cp/constexpr.c
> @@ -2006,12 +2050,12 @@ cxx_eval_vec_init_1 (const constexpr_ctx
>               bool *non_constant_p, bool *overflow_p)
>  {
>    tree elttype = TREE_TYPE (atype);
> -  int max = tree_to_shwi (array_type_nelts (atype));
> +  HOST_WIDE_INT max = (HOST_WIDE_INT) tree_to_uhwi (array_type_nelts (atype));
>    verify_ctor_sanity (ctx, atype);
>    vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
>    vec_alloc (*p, max + 1);
>    bool pre_init = false;
> -  int i;
> +  HOST_WIDE_INT i;
>
>    /* For the default constructor, build up a call to the default
>       constructor of the element type.  We only need to handle class types
> Index: gcc/gcc/testsuite/g++.dg/cpp0x/pr63904.C
> ===================================================================
> --- /dev/null
> +++ gcc/gcc/testsuite/g++.dg/cpp0x/pr63904.C
> @@ -0,0 +1,13 @@
> +// { dg-do compile { target c++11 } }
> +
> +template<int N>
> +struct foo {
> +    constexpr foo() : a() {}
> +    int a[N];
> +};
> +
> +int main() {
> +  foo< (foo<1>{}).a[0] > f;
> +  return 0;
> +}
> +

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

* Re: [patch c++]: Fix PR/53904
  2014-11-20 20:14 [patch c++]: Fix PR/53904 Kai Tietz
  2014-11-21 11:42 ` Richard Biener
@ 2014-11-25 20:15 ` Jason Merrill
  2014-11-26 18:14   ` Kai Tietz
  1 sibling, 1 reply; 7+ messages in thread
From: Jason Merrill @ 2014-11-25 20:15 UTC (permalink / raw)
  To: Kai Tietz, GCC Patches

On 11/20/2014 02:48 PM, Kai Tietz wrote:
> this issue fixes a type-overflow issue caused by trying to cast a UHWI
> via tree_to_shwi.
> As soon as value gets larger then SHWI_MAX, we get an error for it.
> So we need to cast it via tree_to_uhwi, and then casting it to the signed variant.

The problem seems to be with zero-length arrays getting -1 from 
array_type_nelts.  Let's use array_type_nelts_top instead so we don't 
ever see negative values.

Jason

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

* Re: [patch c++]: Fix PR/53904
  2014-11-25 20:15 ` Jason Merrill
@ 2014-11-26 18:14   ` Kai Tietz
  2014-11-26 18:51     ` Jason Merrill
  2014-11-27 20:57     ` H.J. Lu
  0 siblings, 2 replies; 7+ messages in thread
From: Kai Tietz @ 2014-11-26 18:14 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Patches

Ok.  Adjusted patch attached.  Nevertheless we should use here
unsigned HWI instead of possible truncation to signed int.  I admit
that it is unlikely to have more then 2^31 elements, but well ....

Ok for apply with adjusted ChangeLog?

Regards,
Kai

Index: constexpr.c
===================================================================
--- constexpr.c (Revision 218076)
+++ constexpr.c (Arbeitskopie)
@@ -2013,12 +2013,12 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tre
                     bool *non_constant_p, bool *overflow_p)
 {
   tree elttype = TREE_TYPE (atype);
-  int max = tree_to_shwi (array_type_nelts (atype));
+  unsigned HOST_WIDE_INT max = tree_to_uhwi (array_type_nelts_top (atype));
   verify_ctor_sanity (ctx, atype);
   vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
   vec_alloc (*p, max + 1);
   bool pre_init = false;
-  int i;
+  unsigned HOST_WIDE_INT i;

   /* For the default constructor, build up a call to the default
      constructor of the element type.  We only need to handle class types
@@ -2043,7 +2043,7 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tre
       pre_init = true;
     }

-  for (i = 0; i <= max; ++i)
+  for (i = 0; i < max; ++i)
     {
       tree idx = build_int_cst (size_type_node, i);
       tree eltinit;

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

* Re: [patch c++]: Fix PR/53904
  2014-11-26 18:14   ` Kai Tietz
@ 2014-11-26 18:51     ` Jason Merrill
  2014-11-27 20:57     ` H.J. Lu
  1 sibling, 0 replies; 7+ messages in thread
From: Jason Merrill @ 2014-11-26 18:51 UTC (permalink / raw)
  To: Kai Tietz; +Cc: GCC Patches

OK, thanks.

Jason

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

* Re: [patch c++]: Fix PR/53904
  2014-11-26 18:14   ` Kai Tietz
  2014-11-26 18:51     ` Jason Merrill
@ 2014-11-27 20:57     ` H.J. Lu
  2014-11-27 21:44       ` Kai Tietz
  1 sibling, 1 reply; 7+ messages in thread
From: H.J. Lu @ 2014-11-27 20:57 UTC (permalink / raw)
  To: Kai Tietz; +Cc: Jason Merrill, GCC Patches

On Wed, Nov 26, 2014 at 9:52 AM, Kai Tietz <ktietz70@googlemail.com> wrote:
> Ok.  Adjusted patch attached.  Nevertheless we should use here
> unsigned HWI instead of possible truncation to signed int.  I admit
> that it is unlikely to have more then 2^31 elements, but well ....
>
> Ok for apply with adjusted ChangeLog?
>
> Regards,
> Kai
>

On Linux/x86, the testcase fails with

output is:
/export/gnu/import/git/gcc/gcc/testsuite/g++.dg/cpp0x/pr63904.C: In
instantiation of 'struct foo<0>':^M
/export/gnu/import/git/gcc/gcc/testsuite/g++.dg/cpp0x/pr63904.C:10:26:
  required from here^M
/export/gnu/import/git/gcc/gcc/testsuite/g++.dg/cpp0x/pr63904.C:6:12:
error: ISO C++ forbids zero-size array [-Wpedantic]^M

FAIL: g++.dg/cpp0x/pr63904.C  -std=c++11 (test for excess errors)

H.J.

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

* Re: [patch c++]: Fix PR/53904
  2014-11-27 20:57     ` H.J. Lu
@ 2014-11-27 21:44       ` Kai Tietz
  0 siblings, 0 replies; 7+ messages in thread
From: Kai Tietz @ 2014-11-27 21:44 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Jason Merrill, GCC Patches

Sorry, missed to add needed hunk to disable pedantic warnings for this testcase.
Committed it as obvious fix at rev 218130.

Kai

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

end of thread, other threads:[~2014-11-27 20:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-20 20:14 [patch c++]: Fix PR/53904 Kai Tietz
2014-11-21 11:42 ` Richard Biener
2014-11-25 20:15 ` Jason Merrill
2014-11-26 18:14   ` Kai Tietz
2014-11-26 18:51     ` Jason Merrill
2014-11-27 20:57     ` H.J. Lu
2014-11-27 21:44       ` Kai Tietz

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).