Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWIG crashes with segfault during creating %template. SWIG 4.2.1 #2977

Open
AnDevi opened this issue Jul 29, 2024 · 2 comments
Open

SWIG crashes with segfault during creating %template. SWIG 4.2.1 #2977

AnDevi opened this issue Jul 29, 2024 · 2 comments
Labels

Comments

@AnDevi
Copy link

AnDevi commented Jul 29, 2024

I'm trying to create template from std::vector %template(ColorList) std::vector<Color> but SWIG binary crashes with Segmentation fault: 11
I think it crashes due to that that implementation of my header with Color struct looks like that:

extern "C" {

typedef struct Color Color;

struct Color {
    int red;
    int green;
    int blue;
};
}

The problem is in typedef struct Color Color; probably there is some name conflict, cuz when I comment it out, it works fine.
But for few reasons I need to it stay like this.

When I comment out creating a template with type Color it also works fine, so this typedef is only problematic during template creation.

Is this crash known? Or is there any trick that can I use in swig's interface files to avoid this crash ?

@ojwb
Copy link
Member

ojwb commented Jul 29, 2024

Complete reproducer:

%module x
%include std_vector.i
%inline %{
#include <vector>
extern "C" {

typedef struct Color Color;

struct Color {
    int red;
    int green;
    int blue;
};
}
%}
%template(ColorList) std::vector<Color>;

Process with swig -c++ -python test.i

gdb shows Swig_symbol_typedef_reduce calling itself over and over at Swig/symbol.c:1905 (using the commit 44990e2). It looks like the parameters are the same every time, so we could check for that and catch this case, but that's not very useful without knowing how to handle it.

I thought there was an existing report of a similar case and found https://sourceforge.net/p/swig/bugs/140/ in the old tracker, but that was fixed long ago and is probably significantly different (the equivalent in the case here would be a typedef of ColorList rather than Color).

@ojwb ojwb added the C++ label Jul 29, 2024
@ojwb
Copy link
Member

ojwb commented Aug 30, 2024

As a workaround, it seems to work if adjusted to use:

%template(ColorList) std::vector<struct Color>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants