#1161 allprotected makes invalid dynamic_cast if call from subclas

open
nobody
5
2012-12-21
2011-04-22
Alan Harder
No

Consider this test case:

test.h
--------------------
class A {
public:
virtual ~A();
protected:
char buf[100];
};

class B : public A {
};
===========

test.cxx
--------------------

#include "test.h"

A::~A() { }

test.swig
--------------------

%module(directors="1", allprotected="1") TestModule;
%feature("director");

%{
#include "test.h"
%}

%include "test.h"

test.java
--------------------
public class test {
public static void main(String[] args) {
System.load("/path/to/test.so");
A a = new A();
a.setBuf("right class");
System.out.println(a.getBuf());
B b = new B();
b.setBuf("subclass uh oh");
System.out.println(b.getBuf());
}
}
===========

Running swig 2.0.3 with -c++ -java, the generated A.java has getBuf and setBuf. The generated C++ code has SwigDirector_A class with "public: using A::buf" to make the protected buf accessible, and generated code for getBuf/setBuf does a dynamic_cast of the given object to SwigDirector_A to make the call.
This works correctly when you call A.getBuf/setBuf, as in the first few lines of test.java.
However, when a class extends the class with the protected member you are still able to call getBuf/setBuf, as in the last few lines of test.java. In this case, however, the object passed in is a SwigDirector_B, NOT a SwigDirector_A so the dynamic_cast is incorrect and a crash is the result.

Discussion

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks