public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/63364] New: GCC optimizer causing memory corruption
@ 2014-09-25  3:18 contact at ncomputers dot org
  2014-09-25  3:38 ` [Bug c++/63364] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: contact at ncomputers dot org @ 2014-09-25  3:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 63364
           Summary: GCC optimizer causing memory corruption
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: contact at ncomputers dot org

http://ncomputers.org/content/code.php?src=findings/gcc%20optimizer%20memory%20corruption.cpp

We found that probably some optimization flags of the GCC compiler are causing
memory corruption after the reading of a variable's content through a pointer
to a class or a struct.

We've tested and confirmed this bug with these compiler's versions: 4.7.2 and
4.8.2

#include<iostream>
using namespace std;

struct A{
    unsigned int const v;
    A():v(10){}
    void test_A();
};

struct B{
    A const*const&a;
    B(A const*const&aa):a(aa){}
    void test_B(){
        unsigned int it=0;
        cout<<"Value of constant a->v: "<<a->v<<endl;
        do{
            cout<<it<<',';
        }while(++it<a->v);
        cout<<endl<<"Value of constant a->v: "<<a->v<<endl;
        //cout<<endl<<"Value of constant a->v:"<<' '<<a->v<<endl;

        /*
         * GNU Compiler with -O2 or -O3 flag
         * Value of constant a->v changes to zero
         *
         * If this result was shown:
         *
         * Value of constant a->v: 10
         * 0,
         * Value of constant a->v: 0
         *
         * Now comment the previous "cout" line and uncomment the next "cout"
line.
         * Note that this is the only difference between both lines:
         * <<' '
         *
         * The right result should be shown:
         *
         * Value of constant a->v: 10
         * 0,1,2,3,4,5,6,7,8,9,
         * Value of constant a->v: 10
         */
    }
};

void A::test_A(){
    B*b=new B(this);
    b->test_B();
    delete b;
}

int main(){
    A*a=new A();
    a->test_A();
    delete a;
    return 0;
};


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

* [Bug c++/63364] GCC optimizer causing memory corruption
  2014-09-25  3:18 [Bug c++/63364] New: GCC optimizer causing memory corruption contact at ncomputers dot org
@ 2014-09-25  3:38 ` pinskia at gcc dot gnu.org
  2014-09-25  5:13 ` contact at ncomputers dot org
  2014-09-25  5:26 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-09-25  3:38 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>    B(A const*const&aa):a(aa){}

>    B*b=new B(this);


There is a temporary variable being created there and it goes out of scope
after the statement is finished.   The reason is because this is not a lvalue,
prvalue.

Here is the quote from the standard:
In the body of a non-static (9.3) member function, the keyword this is a
prvalue expression whose value
is the address of the object for which the function is called.


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

* [Bug c++/63364] GCC optimizer causing memory corruption
  2014-09-25  3:18 [Bug c++/63364] New: GCC optimizer causing memory corruption contact at ncomputers dot org
  2014-09-25  3:38 ` [Bug c++/63364] " pinskia at gcc dot gnu.org
@ 2014-09-25  5:13 ` contact at ncomputers dot org
  2014-09-25  5:26 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: contact at ncomputers dot org @ 2014-09-25  5:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from ncomputers.org <contact at ncomputers dot org> ---
(In reply to Andrew Pinski from comment #1)
> >    B(A const*const&aa):a(aa){}
> 
> >    B*b=new B(this);
> 
> 
> There is a temporary variable being created there and it goes out of scope
> after the statement is finished.   The reason is because this is not a
> lvalue, prvalue.
> 
> Here is the quote from the standard:
> In the body of a non-static (9.3) member function, the keyword this is a
> prvalue expression whose value
> is the address of the object for which the function is called.

Andrew, thank you for your answer!

Yes it is true! We understand now better the reference variables.

Can we write your name in our website?

"Andrew Pinski taught us something new".


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

* [Bug c++/63364] GCC optimizer causing memory corruption
  2014-09-25  3:18 [Bug c++/63364] New: GCC optimizer causing memory corruption contact at ncomputers dot org
  2014-09-25  3:38 ` [Bug c++/63364] " pinskia at gcc dot gnu.org
  2014-09-25  5:13 ` contact at ncomputers dot org
@ 2014-09-25  5:26 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-09-25  5:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to ncomputers.org from comment #2)
> Can we write your name in our website?
> 
> "Andrew Pinski taught us something new".

I guess.


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

end of thread, other threads:[~2014-09-25  5:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-25  3:18 [Bug c++/63364] New: GCC optimizer causing memory corruption contact at ncomputers dot org
2014-09-25  3:38 ` [Bug c++/63364] " pinskia at gcc dot gnu.org
2014-09-25  5:13 ` contact at ncomputers dot org
2014-09-25  5:26 ` pinskia 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).