Skip to content

Commit f914da8

Browse files
authored
[ffigen] Fix infinite recursion when parsing cyclic C++ class references (#3584)
1 parent 0f05f88 commit f914da8

3 files changed

Lines changed: 161 additions & 16 deletions

File tree

‎pkgs/ffigen/lib/src/header_parser/sub_parsers/classdecl_parser.dart‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,6 @@ CppClass? parseClassDeclaration(Context context, clang_types.CXCursor cursor) {
5252
'++++ Adding C++ Class: Name: $className, ${cursor.completeStringRepr()}',
5353
);
5454

55-
final methods = <CppMethod>[];
56-
57-
cursor.visitChildren((child) {
58-
final kind = clang.clang_getCursorKind(child);
59-
if (kind == clang_types.CXCursorKind.CXCursor_CXXMethod) {
60-
_parseAnyMethod(context, child, decl, methods, CppMethodKind.method);
61-
} else if (kind == clang_types.CXCursorKind.CXCursor_Constructor) {
62-
_parseAnyMethod(context, child, decl, methods, CppMethodKind.constructor);
63-
}
64-
});
65-
66-
// Parse public base classes (only public specifiers; non-public are ignored).
67-
final bases = _parsePublicBases(context, cursor);
68-
6955
final cppClass = CppClass(
7056
usr: usr,
7157
dartDoc: getCursorDocComment(
@@ -76,13 +62,37 @@ CppClass? parseClassDeclaration(Context context, clang_types.CXCursor cursor) {
7662
originalName: className,
7763
name: className,
7864
context: context,
79-
methods: methods,
65+
methods: <CppMethod>[],
8066
fields: <CppMember>[],
81-
bases: bases,
67+
bases: <CppClass>[],
8268
);
8369

8470
context.bindingsIndex.addCppClassToSeen(usr, cppClass);
8571

72+
cursor.visitChildren((child) {
73+
final kind = clang.clang_getCursorKind(child);
74+
if (kind == clang_types.CXCursorKind.CXCursor_CXXMethod) {
75+
_parseAnyMethod(
76+
context,
77+
child,
78+
decl,
79+
cppClass.methods,
80+
CppMethodKind.method,
81+
);
82+
} else if (kind == clang_types.CXCursorKind.CXCursor_Constructor) {
83+
_parseAnyMethod(
84+
context,
85+
child,
86+
decl,
87+
cppClass.methods,
88+
CppMethodKind.constructor,
89+
);
90+
}
91+
});
92+
93+
// Parse public base classes (only public specifiers; non-public are ignored).
94+
cppClass.bases.addAll(_parsePublicBases(context, cursor));
95+
8696
return cppClass;
8797
}
8898

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
#include <memory>
6+
7+
// Self-reference through a return type.
8+
class Node {
9+
public:
10+
virtual ~Node() = default;
11+
virtual Node* clone() = 0;
12+
};
13+
14+
// Self-reference through a parameter type.
15+
class Merger {
16+
public:
17+
virtual void merge(Merger* other) = 0;
18+
};
19+
20+
// Self-reference through a smart pointer return type.
21+
class Owner {
22+
public:
23+
virtual std::unique_ptr<Owner> take() = 0;
24+
};
25+
26+
// Mutual recursion between two classes.
27+
class Branch;
28+
29+
class Leaf {
30+
public:
31+
virtual Branch* parent() = 0;
32+
};
33+
34+
class Branch {
35+
public:
36+
virtual Leaf* firstLeaf() = 0;
37+
};
38+
39+
Node* node_create(void);
40+
Merger* merger_create(void);
41+
Owner* owner_create(void);
42+
Leaf* leaf_create(void);
43+
Branch* branch_create(void);
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
import 'package:ffigen/ffigen.dart';
8+
import 'package:ffigen/src/code_generator.dart' as cg;
9+
import 'package:ffigen/src/header_parser.dart' as parser;
10+
import 'package:test/test.dart';
11+
12+
import '../test_utils.dart';
13+
14+
const _cyclicClasses = {'Node', 'Merger', 'Owner', 'Leaf', 'Branch'};
15+
16+
void main() {
17+
group('cpp_cyclic_class', () {
18+
late cg.Library library;
19+
20+
setUpAll(() {
21+
library = parser.parse(
22+
testContext(
23+
FfiGenerator(
24+
output: Output(dart: DartOutput(path: Uri.file('unused'))),
25+
input: Input(
26+
entryPoints: [
27+
Uri.file(
28+
absPath('test/header_parser_tests/cpp_cyclic_class.h'),
29+
),
30+
],
31+
compilerOptions: [
32+
'-x',
33+
'c++',
34+
'-std=c++17',
35+
if (Platform.isMacOS) ...['-isysroot', macSdkPath],
36+
],
37+
),
38+
cpp: const Cpp(),
39+
visitors: [
40+
Visitor(
41+
cppClass: (node) => node.isIncluded = _cyclicClasses.contains(
42+
node.originalName,
43+
),
44+
),
45+
],
46+
),
47+
),
48+
);
49+
});
50+
51+
test('classes that name themselves are parsed exactly once', () {
52+
final names =
53+
library.bindings
54+
.whereType<cg.CppClass>()
55+
.map((c) => c.originalName)
56+
.toList()
57+
..sort();
58+
59+
// Every class of the header, once each: a member type that closes a cycle
60+
// must resolve to the class already being parsed, not to a second copy of
61+
// it.
62+
expect(names, _cyclicClasses.toList()..sort());
63+
});
64+
65+
test('a cyclic member resolves to the class already being parsed', () {
66+
cg.CppClass classNamed(String name) => library.bindings
67+
.whereType<cg.CppClass>()
68+
.firstWhere((c) => c.originalName == name);
69+
70+
cg.CppClass returnedClass(cg.CppClass cls, String methodName) {
71+
final method = cls.methods.firstWhere(
72+
(m) => m.originalName == methodName,
73+
orElse: () => fail('${cls.originalName} has no method $methodName'),
74+
);
75+
expect(method.returnType, isA<cg.CppClassPointerType>());
76+
return (method.returnType as cg.CppClassPointerType).cppClass;
77+
}
78+
79+
final node = classNamed('Node');
80+
final leaf = classNamed('Leaf');
81+
final branch = classNamed('Branch');
82+
83+
expect(
84+
returnedClass(node, 'clone'),
85+
same(node),
86+
reason: 'a cyclic return type must not be a second copy of the class',
87+
);
88+
expect(returnedClass(leaf, 'parent'), same(branch));
89+
expect(returnedClass(branch, 'firstLeaf'), same(leaf));
90+
});
91+
});
92+
}

0 commit comments

Comments
 (0)