Skip to content

Commit 64d2a3c

Browse files
authored
Fix SWIG bindings SEGFAULT when .dl filename contains hyphens (souffle-lang#2573)
1 parent c94bc58 commit 64d2a3c

7 files changed

Lines changed: 60 additions & 1 deletion

File tree

src/include/souffle/SouffleInterface.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "souffle/SymbolTable.h"
2222
#include "souffle/datastructure/ConcurrentCache.h"
2323
#include "souffle/utility/MiscUtil.h"
24+
#include "souffle/utility/StringUtil.h"
2425
#include <algorithm>
2526
#include <cassert>
2627
#include <cstddef>
@@ -1134,7 +1135,10 @@ class ProgramFactory {
11341135
* @return The new instance(SouffleProgram*), or null pointer if the instance not found
11351136
*/
11361137
static SouffleProgram* newInstance(const std::string& name) {
1137-
ProgramFactory* factory = find(name);
1138+
// Normalize name to handle special characters (e.g., hyphens become underscores)
1139+
// This matches how factory names are generated from filenames in Synthesiser.cpp
1140+
std::string normalizedName = identifier(name);
1141+
ProgramFactory* factory = find(normalizedName);
11381142
if (factory != nullptr) {
11391143
return factory->newInstance();
11401144
} else {

tests/swig/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ endfunction()
152152
souffle_positive_swig_test(dump_output COMPARE_STDOUT)
153153
souffle_positive_swig_test(family COMPARE_STDOUT)
154154
souffle_positive_swig_test(flights)
155+
souffle_positive_swig_test(hyphen-name)
155156
souffle_positive_swig_test(insert_for)
156157
souffle_positive_swig_test(movies)
157158
souffle_positive_swig_test(paths)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Souffle - A Datalog Compiler
3+
Copyright (c) 2025, The Souffle Developers. All rights reserved
4+
Licensed under the Universal Permissive License v 1.0 as shown at:
5+
- https://opensource.org/licenses/UPL
6+
- <souffle root>/licenses/SOUFFLE-UPL.txt
7+
8+
Test case for issue #2533: SWIG python bindings SEGFAULT if name of .dl file contains -
9+
10+
The bug: when a .dl file contains a hyphen in its name (e.g., "hyphen-name.dl"),
11+
the identifier() function converts it to a valid C++ identifier by replacing
12+
hyphens with underscores ("hyphen_name"). However, users calling newInstance()
13+
expect to use the original filename with the hyphen.
14+
15+
This test verifies that newInstance("hyphen-name") works correctly.
16+
"""
17+
18+
import SwigInterface
19+
import sys
20+
21+
# This should work with the original filename containing a hyphen
22+
# Before the fix, this would cause a SEGFAULT because:
23+
# 1. The program is registered in the factory as "hyphen_name" (with underscore)
24+
# 2. But newInstance("hyphen-name") looks for "hyphen-name" (with hyphen)
25+
# 3. The factory returns nullptr
26+
# 4. SWIGSouffleProgram tries to dereference nullptr -> SEGFAULT
27+
p = SwigInterface.newInstance('hyphen-name')
28+
p.loadAll(sys.argv[1])
29+
p.run()
30+
p.printAll('.')
31+
p.thisown = 1
32+
del p
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Souffle - A Datalog Compiler
2+
// Copyright (c) 2025, The Souffle Developers. All rights reserved
3+
// Licensed under the Universal Permissive License v 1.0 as shown at:
4+
// - https://opensource.org/licenses/UPL
5+
// - <souffle root>/licenses/SOUFFLE-UPL.txt
6+
7+
// Test case for issue #2533: SWIG python bindings SEGFAULT if name of .dl file contains -
8+
9+
.type Node <: symbol
10+
11+
.decl edge(node1:Node, node2:Node)
12+
.decl path(node1:Node, node2:Node)
13+
.output path()
14+
15+
edge("a","b").
16+
edge("b","c").
17+
18+
path(X,Y) :- edge(X,Y).
19+
path(X,Y) :- path(X,Z), edge(Z,Y).

tests/swig/python/hyphen-name/hyphen-name.err

Whitespace-only changes.

tests/swig/python/hyphen-name/hyphen-name.out

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a b
2+
a c
3+
b c

0 commit comments

Comments
 (0)