public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/19665] New: Can access private member function if it is virtual
@ 2005-01-28  9:20 amit_choudhary at mindtree dot com
  2005-01-28  9:29 ` [Bug c++/19665] " amit_choudhary at mindtree dot com
  2005-01-28  9:42 ` falk at debian dot org
  0 siblings, 2 replies; 3+ messages in thread
From: amit_choudhary at mindtree dot com @ 2005-01-28  9:20 UTC (permalink / raw)
  To: gcc-bugs

I was doing some experiments and I discovered that I can access a private member
function if I make it virtual and do few other tricks. The code is below.

The "Base class called" is printed. So, basically when someone wants to access 
a virtual function (by explicit type casting), there is no check whether that
virtual function is private or not. But if I try to call it directly, it checks
that it is a private member function. May be g++ compiler needs to be modified.


#include<iostream>

using namespace std;

class A {
private:
        virtual void abc()
        {
                cout << "Base Class called\n";
        }
};

class B : public A {
public:
        void abc()
        {
                cout << "Derived Class called\n";
        }
};

int main()
{
        A a;
        B *b = static_cast<B *>(&a);
        b->abc();
}

[amit]$ g++ c++-bug.cpp
c++-bug.cpp:6: warning: all member functions in class `A' are private
[amit]$ ./a.out
Base Class called
[amit]$

-- 
           Summary: Can access private member function if it is virtual
           Product: gcc
           Version: 3.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: amit_choudhary at mindtree dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: g++ c++-bug.cpp c++-bug.cpp:6: warning: all member
                    functions in
  GCC host triplet: gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
GCC target triplet: Configured with: ../configure --prefix=/usr --
                    mandir=/usr/share/


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19665


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

* [Bug c++/19665] Can access private member function if it is virtual
  2005-01-28  9:20 [Bug c++/19665] New: Can access private member function if it is virtual amit_choudhary at mindtree dot com
@ 2005-01-28  9:29 ` amit_choudhary at mindtree dot com
  2005-01-28  9:42 ` falk at debian dot org
  1 sibling, 0 replies; 3+ messages in thread
From: amit_choudhary at mindtree dot com @ 2005-01-28  9:29 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From amit_choudhary at mindtree dot com  2005-01-28 09:29 -------
Created an attachment (id=8088)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=8088&action=view)
Preprocessed file


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19665


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

* [Bug c++/19665] Can access private member function if it is virtual
  2005-01-28  9:20 [Bug c++/19665] New: Can access private member function if it is virtual amit_choudhary at mindtree dot com
  2005-01-28  9:29 ` [Bug c++/19665] " amit_choudhary at mindtree dot com
@ 2005-01-28  9:42 ` falk at debian dot org
  1 sibling, 0 replies; 3+ messages in thread
From: falk at debian dot org @ 2005-01-28  9:42 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From falk at debian dot org  2005-01-28 09:41 -------
Since this cast has undefined behaviour, as a is not actually a B, any
behaviour of the program is acceptable, including what you get. Note that
"private" was never meant as a security feature, there are lots of ways to
circumvent it.


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


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19665


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

end of thread, other threads:[~2005-01-28  9:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-28  9:20 [Bug c++/19665] New: Can access private member function if it is virtual amit_choudhary at mindtree dot com
2005-01-28  9:29 ` [Bug c++/19665] " amit_choudhary at mindtree dot com
2005-01-28  9:42 ` falk at debian dot 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).