Skip to content

Commit 0b4dfdb

Browse files
committed
fix: double-free when disposing CXPlatformAvailability
1 parent 42a9a34 commit 0b4dfdb

File tree

5 files changed

+49
-21
lines changed

5 files changed

+49
-21
lines changed

deno.lock

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/mod.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,9 +2739,11 @@ export class CXCursor {
27392739
}
27402740
const platform = cxstringToString(
27412741
availabilityBuffer.subarray(0, 16),
2742+
false,
27422743
);
27432744
const message = cxstringToString(
27442745
availabilityBuffer.subarray(72 - 16),
2746+
false,
27452747
);
27462748
const view = new DataView(
27472749
availabilityBuffer.buffer,

lib/utils.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,25 @@ export const cstrArray = (strings: string[]) => {
5858
}
5959
};
6060

61-
export const cxstringToString = (cxstring: Uint8Array): string => {
61+
export const cxstringToString = (
62+
cxstring: Uint8Array,
63+
dispose = true,
64+
): string => {
6265
const cstring = libclang.symbols.clang_getCString(cxstring);
63-
if (cstring === NULL) {
64-
libclang.symbols.clang_disposeString(cxstring);
65-
return "";
66+
let string = "";
67+
if (cstring !== NULL) {
68+
try {
69+
string = Deno.UnsafePointerView.getCString(cstring);
70+
} catch {
71+
const buf = new Uint8Array(
72+
Deno.UnsafePointerView.getArrayBuffer(cstring, 1024),
73+
);
74+
string = cstrToString(buf.subarray(buf.indexOf(0)));
75+
}
6676
}
67-
let string: string;
68-
try {
69-
string = Deno.UnsafePointerView.getCString(cstring);
70-
} catch {
71-
const buf = new Uint8Array(
72-
Deno.UnsafePointerView.getArrayBuffer(cstring, 1024),
73-
);
74-
string = cstrToString(buf.subarray(buf.indexOf(0)));
77+
if (dispose) {
78+
libclang.symbols.clang_disposeString(cxstring);
7579
}
76-
libclang.symbols.clang_disposeString(cxstring);
7780
return string;
7881
};
7982
export const cstrToString = (cstr: Uint8Array): string =>

test/CXTranslationUnit.test.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import {
33
assertNotEquals,
44
assertThrows,
55
} from "https://deno.land/[email protected]/testing/asserts.ts";
6-
import { CXChildVisitResult } from "../lib/include/typeDefinitions.ts";
6+
import {
7+
CXChildVisitResult,
8+
CXCursorKind,
9+
} from "../lib/include/typeDefinitions.ts";
710
import { CXDiagnosticSet, CXIndex } from "../lib/mod.ts";
811

912
Deno.test("class CXIndex", async (t) => {
@@ -118,29 +121,42 @@ Deno.test("class CXTranslationUnit", async (t) => {
118121
const index = new CXIndex();
119122
const tu = index.parseTranslationUnit("./test/assets/test.h");
120123
const tu2 = index.parseTranslationUnit("./test/assets/test.hpp");
121-
console.log("Diag count:", tu2.getNumberOfDiagnostics());
124+
//console.log("Diag count:", tu2.getNumberOfDiagnostics());
122125
const file = tu.getFile("./test/assets/test.h");
123126
assertNotEquals(file, null);
124127
const contents = file!.getContents();
125128
assertEquals(contents.startsWith("// my_class.h"), true);
126129
assertEquals(contents.length, 171);
127130
const cursor = tu.getCursor();
128131
assertEquals(cursor.getBriefCommentText(), "");
129-
console.log(cursor.kind);
132+
//console.log(cursor.kind);
130133

131134
cursor.visitChildren((cursor) => {
132-
console.log(cursor.kind);
135+
//console.log(cursor.kind);
133136
return CXChildVisitResult.CXChildVisit_Recurse;
134137
});
135138

139+
tu2.getCursor().visitChildren((cursor) => {
140+
switch (cursor.kind) {
141+
case CXCursorKind.CXCursor_Namespace:
142+
case CXCursorKind.CXCursor_ClassDecl:
143+
return CXChildVisitResult.CXChildVisit_Recurse;
144+
case CXCursorKind.CXCursor_CXXMethod:
145+
console.log(cursor.getPlatformAvailability());
146+
console.log(cursor.getCXXManglings());
147+
}
148+
console.log(cursor.getKindSpelling());
149+
return CXChildVisitResult.CXChildVisit_Continue;
150+
});
151+
136152
assertThrows(() => CXDiagnosticSet.loadDiagnostics("test-diag.foo"));
137153

138-
console.log(tu.getAllSkippedRanges());
154+
//console.log(tu.getAllSkippedRanges());
139155
const resourceUsage = tu.getResourceUsage();
140156
const length = resourceUsage.length;
141-
console.log("ResourceUsage length:", length);
157+
//console.log("ResourceUsage length:", length);
142158
for (let i = 0; i < length; i++) {
143-
console.log(resourceUsage.at(i));
159+
//console.log(resourceUsage.at(i));
144160
}
145161
resourceUsage.dispose();
146162
tu.dispose();

test/assets/test.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace N {
66
class my_class {
77
public:
8+
#ifdef __linux__
9+
[[deprecated("Boost it")]]
10+
#endif
811
void do_something();
912
private:
1013
int foo;

0 commit comments

Comments
 (0)