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

            Bug ID: 70984
           Summary: Templated derived class erroneously allows taking
                    address of private base class member functions
           Product: gcc
           Version: 5.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: awise.gcc.bug at gmail dot com
  Target Milestone: ---

Created attachment 38431
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=38431&action=edit
Preprocessed source code

Given a base class:

class B {
protected:
  void fprot();
  int  iprot;
private:
  void fpriv();
  int  ipriv;
};

A normal non-templated derived class appears to honour the visibility of base
class members in the way that I'd expect:

class A : public B {
public:
  void f() {
    void (B::*f)();
    int i;
    //f = &B::fprot; - protected in this context
    //f = &B::fpriv; - private in this context
    f = &A::fprot;  // OK - access to protected base member via public
derivation
    //f = &A::fpriv; - private in this context
    fprot();        // OK - access to protected base member via public
derivation
    //fpriv();       - private in this context
    i = iprot;      // OK - access to protected base member via public
derivation
    //i = ipriv;     - private in this context
  }
};

However, a templated derived class allows the address of the private and
protected member functions of the base class to be evaluated when I don't
believe it should:

template<int N>
class C : public B {
public:
  void f() {
    void (B::*f)();
    int i;
    f = &B::fprot; // Should be an error - protected in this context?
    f = &B::fpriv; // Should be an error - private in this context?
    f = &C::fprot; // OK - access to protected base member via public
derivation
    f = &C::fpriv; // Should be an error - private in this context?
    fprot();       // OK - access to protected base member via public
derivation
    //fpriv();      - private in this context
    i = iprot;
    //i = ipriv;    - private in this context
  }
};

template class C<42>;

I'd expect the visibility to be the same as in the non-templated derived class,
above.

log of compile invoked as "g++ -v -save-temps -c gcc_bug1.cpp":

Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.3.1-14ubuntu2'
--with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs
--enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-5 --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib
--disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home
--with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64
--with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar
--enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--with-tune=generic --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/5/cc1plus -E -quiet -v -imultiarch
x86_64-linux-gnu -D_GNU_SOURCE gcc_bug1.cpp -mtune=generic -march=x86-64
-fpch-preprocess -fstack-protector-strong -Wformat -Wformat-security -o
gcc_bug1.ii
ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/5"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/5
 /usr/include/x86_64-linux-gnu/c++/5
 /usr/include/c++/5/backward
 /usr/lib/gcc/x86_64-linux-gnu/5/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/5/cc1plus -fpreprocessed gcc_bug1.ii -quiet
-dumpbase gcc_bug1.cpp -mtune=generic -march=x86-64 -auxbase gcc_bug1 -version
-fstack-protector-strong -Wformat -Wformat-security -o gcc_bug1.s
GNU C++ (Ubuntu 5.3.1-14ubuntu2) version 5.3.1 20160413 (x86_64-linux-gnu)
        compiled by GNU C version 5.3.1 20160413, GMP version 6.1.0, MPFR
version 3.1.4, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++ (Ubuntu 5.3.1-14ubuntu2) version 5.3.1 20160413 (x86_64-linux-gnu)
        compiled by GNU C version 5.3.1 20160413, GMP version 6.1.0, MPFR
version 3.1.4, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: a544146c1c8bd944ae4c4ff6181f339e
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
 as -v --64 -o gcc_bug1.o gcc_bug1.s
GNU assembler version 2.26 (x86_64-linux-gnu) using BFD version (GNU Binutils
for Ubuntu) 2.26
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'

Reply via email to