public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor)
@ 2020-03-24 16:25 moonchasing1999 at gmail dot com
  2020-03-24 17:07 ` [Bug c++/94303] [8/9/10 Regression] " jakub at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: moonchasing1999 at gmail dot com @ 2020-03-24 16:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94303

            Bug ID: 94303
           Summary: Program result error When using global object array
                    (partially initialized with a special constructor, and
                    the rest with the default constructor)
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: moonchasing1999 at gmail dot com
  Target Milestone: ---

g++ version: 8.3.0 (and later)
system: Ubuntu 18.04
complete command line that triggers the bug: g++ -std=c++11 -o out main.cpp
compiler output: none
-----------------------

When I use g++8.3 and later version to compile and run the following code, the
results are not as expected. But if I use 8.2 and earlier version or other
compiler, like clang, wont't occure a fault result.

/* class define */
class A
{
private:
    int d = 9;
public:
    A()=default;
    A(int a) : d(a) {}
    void f() {cout << d << endl;}
};
/* class define end */

/* test code 1 */
A a[3] = {1}; //global
int main()
{
    a[0].f();  //Output: 1
    a[1].f();  //Output: 9
    a[2].f();  //Output: 0  //error
    return 0;
}
/* test code 1 end */

But if put A a[3] = {1} in main() function, it doesn't error.
/* test code 2 */

int main()
{
    A a[3] = {1};
    a[0].f();  //Output: 1
    a[1].f();  //Output: 9
    a[2].f();  //Output: 9  //right!
    return 0;
}
/* test code 2 end */

And I find add number in initialization list, how many elements are initialized
then the last number of elements in the array is an incorrect result. For
example:

/* test code 3 */
A a[100]={1,1, 1};  // global

int main()
{
    a[0].f();   //Output: 1
    a[1].f();   //Output: 1
    a[2].f();   //Output: 1
    a[3].f();   //Output: 9
    a[4].f();   //Output: 9
    .....       //Output: 9
    a[96].f();  //Output: 9
    a[97].f();  //Output: 0  //error
    a[98].f();  //Output: 0  //error
    a[99].f();  //Output: 0  //error
    return 0;
}
/* test code 3 end */

/* full code of test code 1 */
class A
{
private:
    int d = 9;
public:
    A()=default;
    A(int a) : d(a) {}
    void f() {cout << d << endl;}
};

A a[3]={1};

int main()
{
    a[0].f();
    a[1].f();
    a[2].f();
    return 0;
}
/* full code end*/

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

* [Bug c++/94303] [8/9/10 Regression] Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor)
  2020-03-24 16:25 [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor) moonchasing1999 at gmail dot com
@ 2020-03-24 17:07 ` jakub at gcc dot gnu.org
  2020-03-24 17:09 ` redi at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-24 17:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94303

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org
   Last reconfirmed|                            |2020-03-24
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org
            Summary|Program result error When   |[8/9/10 Regression] Program
                   |using global object array   |result error When using
                   |(partially initialized with |global object array
                   |a special constructor, and  |(partially initialized with
                   |the rest with the default   |a special constructor, and
                   |constructor)                |the rest with the default
                   |                            |constructor)
   Target Milestone|---                         |8.5
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Untested fix:
--- gcc/varasm.c.jj     2020-01-22 10:19:24.000000000 +0100
+++ gcc/varasm.c        2020-03-24 18:03:08.532690584 +0100
@@ -5152,6 +5152,26 @@ struct oc_local_state {
 static void
 output_constructor_array_range (oc_local_state *local)
 {
+  /* Perform the index calculation in modulo arithmetic but
+     sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
+     but we are using an unsigned sizetype.  */
+  unsigned prec = TYPE_PRECISION (sizetype);
+  offset_int idx = wi::sext (wi::to_offset (TREE_OPERAND (local->index, 0))
+                            - wi::to_offset (local->min_index), prec);
+  tree valtype = TREE_TYPE (local->val);
+  HOST_WIDE_INT fieldpos
+    = (idx * wi::to_offset (TYPE_SIZE_UNIT (valtype))).to_short_addr ();
+
+  /* Advance to offset of this element.  */
+  if (fieldpos > local->total_bytes)
+    {
+      assemble_zeros (fieldpos - local->total_bytes);
+      local->total_bytes = fieldpos;
+    }
+  else
+    /* Must not go backwards.  */
+    gcc_assert (fieldpos == local->total_bytes);
+
   unsigned HOST_WIDE_INT fieldsize
     = int_size_in_bytes (TREE_TYPE (local->type));

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

* [Bug c++/94303] [8/9/10 Regression] Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor)
  2020-03-24 16:25 [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor) moonchasing1999 at gmail dot com
  2020-03-24 17:07 ` [Bug c++/94303] [8/9/10 Regression] " jakub at gcc dot gnu.org
@ 2020-03-24 17:09 ` redi at gcc dot gnu.org
  2020-03-24 17:10 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2020-03-24 17:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94303

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |10.0, 8.3.0, 9.2.0
      Known to work|                            |8.2.0
           Keywords|                            |wrong-code

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Regressed with r267142

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

* [Bug c++/94303] [8/9/10 Regression] Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor)
  2020-03-24 16:25 [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor) moonchasing1999 at gmail dot com
  2020-03-24 17:07 ` [Bug c++/94303] [8/9/10 Regression] " jakub at gcc dot gnu.org
  2020-03-24 17:09 ` redi at gcc dot gnu.org
@ 2020-03-24 17:10 ` jakub at gcc dot gnu.org
  2020-03-24 17:20 ` [Bug middle-end/94303] " jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-24 17:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94303

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Jonathan has bisected this to my
r9-4877-gfaa9232da39587b27b46341667d6d415d2af9280 change (though, as the patch
shows, the bug is actually that varasm.c didn't handle RANGE_EXPRs properly
during output_constructor.

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

* [Bug middle-end/94303] [8/9/10 Regression] Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor)
  2020-03-24 16:25 [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor) moonchasing1999 at gmail dot com
                   ` (2 preceding siblings ...)
  2020-03-24 17:10 ` jakub at gcc dot gnu.org
@ 2020-03-24 17:20 ` jakub at gcc dot gnu.org
  2020-03-25  7:41 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-24 17:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94303

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 48107
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48107&action=edit
gcc10-pr94303.patch

Full untested patch.

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

* [Bug middle-end/94303] [8/9/10 Regression] Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor)
  2020-03-24 16:25 [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor) moonchasing1999 at gmail dot com
                   ` (3 preceding siblings ...)
  2020-03-24 17:20 ` [Bug middle-end/94303] " jakub at gcc dot gnu.org
@ 2020-03-25  7:41 ` redi at gcc dot gnu.org
  2020-03-25  8:21 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2020-03-25  7:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94303

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rookiezjz at gmail dot com

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
*** Bug 94316 has been marked as a duplicate of this bug. ***

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

* [Bug middle-end/94303] [8/9/10 Regression] Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor)
  2020-03-24 16:25 [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor) moonchasing1999 at gmail dot com
                   ` (4 preceding siblings ...)
  2020-03-25  7:41 ` redi at gcc dot gnu.org
@ 2020-03-25  8:21 ` cvs-commit at gcc dot gnu.org
  2020-03-25  8:23 ` [Bug middle-end/94303] [8/9 " jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-03-25  8:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94303

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:5f18995e23edc944af3a401d9d9d3320a9362652

commit r10-7368-g5f18995e23edc944af3a401d9d9d3320a9362652
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Mar 25 09:21:05 2020 +0100

    varasm: Fix output_constructor where a RANGE_EXPR index needs to skip some
elts [PR94303]

    The following testcase is miscompiled, because output_constructor doesn't
    output the initializer correctly.  The FE creates {[1...2] = 9} in this
    case, and we emit .long 9; long 9; .zero 8 instead of the expected
    .zero 8; .long 9; .long 9.  If the CONSTRUCTOR is {[1] = 9, [2] = 9},
    output_constructor_regular_field has code to notice that the current
    location (local->total_bytes) is smaller than the location we want to write
    to (1*sizeof(elt)) and will call assemble_zeros to skip those.  But
    RANGE_EXPRs are handled by a different function which didn't do this,
    so for RANGE_EXPRs we emitted them properly only if local->total_bytes
    was always equal to the location where the RANGE_EXPR needs to start.

    2020-03-25  Jakub Jelinek  <jakub@redhat.com>

            PR middle-end/94303
            * varasm.c (output_constructor_array_range): If local->index
            RANGE_EXPR doesn't start at the current location in the
constructor,
            skip needed number of bytes using assemble_zeros or assert we don't
            go backwards.

            PR middle-end/94303
            * g++.dg/torture/pr94303.C: New test.

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

* [Bug middle-end/94303] [8/9 Regression] Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor)
  2020-03-24 16:25 [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor) moonchasing1999 at gmail dot com
                   ` (5 preceding siblings ...)
  2020-03-25  8:21 ` cvs-commit at gcc dot gnu.org
@ 2020-03-25  8:23 ` jakub at gcc dot gnu.org
  2020-04-07 19:04 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-25  8:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94303

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[8/9/10 Regression] Program |[8/9 Regression] Program
                   |result error When using     |result error When using
                   |global object array         |global object array
                   |(partially initialized with |(partially initialized with
                   |a special constructor, and  |a special constructor, and
                   |the rest with the default   |the rest with the default
                   |constructor)                |constructor)
      Known to work|                            |10.0
      Known to fail|10.0                        |

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed on the trunk so far.

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

* [Bug middle-end/94303] [8/9 Regression] Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor)
  2020-03-24 16:25 [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor) moonchasing1999 at gmail dot com
                   ` (6 preceding siblings ...)
  2020-03-25  8:23 ` [Bug middle-end/94303] [8/9 " jakub at gcc dot gnu.org
@ 2020-04-07 19:04 ` cvs-commit at gcc dot gnu.org
  2020-04-07 20:01 ` [Bug middle-end/94303] [8 " jakub at gcc dot gnu.org
  2020-09-17 17:21 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-04-07 19:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94303

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:56407bab53a514ffcd6ac011965cebdc5eb3ef54

commit r9-8471-g56407bab53a514ffcd6ac011965cebdc5eb3ef54
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Apr 7 20:59:37 2020 +0200

    varasm: Fix output_constructor where a RANGE_EXPR index needs to skip some
elts [PR94303]

    The following testcase is miscompiled, because output_constructor doesn't
    output the initializer correctly.  The FE creates {[1...2] = 9} in this
    case, and we emit .long 9; long 9; .zero 8 instead of the expected
    .zero 8; .long 9; .long 9.  If the CONSTRUCTOR is {[1] = 9, [2] = 9},
    output_constructor_regular_field has code to notice that the current
    location (local->total_bytes) is smaller than the location we want to write
    to (1*sizeof(elt)) and will call assemble_zeros to skip those.  But
    RANGE_EXPRs are handled by a different function which didn't do this,
    so for RANGE_EXPRs we emitted them properly only if local->total_bytes
    was always equal to the location where the RANGE_EXPR needs to start.

    2020-03-25  Jakub Jelinek  <jakub@redhat.com>

            PR middle-end/94303
            * varasm.c (output_constructor_array_range): If local->index
            RANGE_EXPR doesn't start at the current location in the
constructor,
            skip needed number of bytes using assemble_zeros or assert we don't
            go backwards.

            PR middle-end/94303
            * g++.dg/torture/pr94303.C: New test.

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

* [Bug middle-end/94303] [8 Regression] Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor)
  2020-03-24 16:25 [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor) moonchasing1999 at gmail dot com
                   ` (7 preceding siblings ...)
  2020-04-07 19:04 ` cvs-commit at gcc dot gnu.org
@ 2020-04-07 20:01 ` jakub at gcc dot gnu.org
  2020-09-17 17:21 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-04-07 20:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94303

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[8/9 Regression] Program    |[8 Regression] Program
                   |result error When using     |result error When using
                   |global object array         |global object array
                   |(partially initialized with |(partially initialized with
                   |a special constructor, and  |a special constructor, and
                   |the rest with the default   |the rest with the default
                   |constructor)                |constructor)

--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for 9.4+ too.

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

* [Bug middle-end/94303] [8 Regression] Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor)
  2020-03-24 16:25 [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor) moonchasing1999 at gmail dot com
                   ` (8 preceding siblings ...)
  2020-04-07 20:01 ` [Bug middle-end/94303] [8 " jakub at gcc dot gnu.org
@ 2020-09-17 17:21 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-17 17:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94303

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for 8.5 in r8-10475-g0aa738f8d4469434c131dc36711a045cfd8ecd7f too.

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

end of thread, other threads:[~2020-09-17 17:21 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-24 16:25 [Bug c++/94303] New: Program result error When using global object array (partially initialized with a special constructor, and the rest with the default constructor) moonchasing1999 at gmail dot com
2020-03-24 17:07 ` [Bug c++/94303] [8/9/10 Regression] " jakub at gcc dot gnu.org
2020-03-24 17:09 ` redi at gcc dot gnu.org
2020-03-24 17:10 ` jakub at gcc dot gnu.org
2020-03-24 17:20 ` [Bug middle-end/94303] " jakub at gcc dot gnu.org
2020-03-25  7:41 ` redi at gcc dot gnu.org
2020-03-25  8:21 ` cvs-commit at gcc dot gnu.org
2020-03-25  8:23 ` [Bug middle-end/94303] [8/9 " jakub at gcc dot gnu.org
2020-04-07 19:04 ` cvs-commit at gcc dot gnu.org
2020-04-07 20:01 ` [Bug middle-end/94303] [8 " jakub at gcc dot gnu.org
2020-09-17 17:21 ` jakub at gcc dot gnu.org

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