Skip to content

Commit 1c02c8f

Browse files
authored
[clang] fix use after free in clang/tools/c-index-test/c-index-test.c (#127063)
recent change e76739e has exposed use after free in GetCursorSource() function that returned pointer to a disposed CXString
1 parent fb39445 commit 1c02c8f

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

clang/tools/c-index-test/c-index-test.c

+32-19
Original file line numberDiff line numberDiff line change
@@ -1213,28 +1213,34 @@ static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) {
12131213
}
12141214
}
12151215

1216-
static const char* GetCursorSource(CXCursor Cursor) {
1216+
static CXString createCXString(const char *CS) {
1217+
CXString Str;
1218+
Str.data = (const void *)CS;
1219+
Str.private_flags = 0;
1220+
return Str;
1221+
}
1222+
1223+
static CXString duplicateCXString(const char *CS) {
1224+
CXString Str;
1225+
Str.data = strdup(CS);
1226+
Str.private_flags = 1; // CXS_Malloc
1227+
return Str;
1228+
}
1229+
1230+
static CXString GetCursorSource(CXCursor Cursor) {
12171231
CXSourceLocation Loc = clang_getCursorLocation(Cursor);
12181232
CXString source;
12191233
CXFile file;
12201234
clang_getExpansionLocation(Loc, &file, 0, 0, 0);
12211235
source = clang_getFileName(file);
12221236
if (!clang_getCString(source)) {
12231237
clang_disposeString(source);
1224-
return "<invalid loc>";
1238+
return createCXString("<invalid loc>");
12251239
}
1226-
else {
1227-
const char *b = basename(clang_getCString(source));
1228-
clang_disposeString(source);
1229-
return b;
1230-
}
1231-
}
1232-
1233-
static CXString createCXString(const char *CS) {
1234-
CXString Str;
1235-
Str.data = (const void *) CS;
1236-
Str.private_flags = 0;
1237-
return Str;
1240+
const char *b = basename(clang_getCString(source));
1241+
CXString result = duplicateCXString(b);
1242+
clang_disposeString(source);
1243+
return result;
12381244
}
12391245

12401246
/******************************************************************************/
@@ -1358,8 +1364,10 @@ enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
13581364
CXSourceLocation Loc = clang_getCursorLocation(Cursor);
13591365
unsigned line, column;
13601366
clang_getFileLocation(Loc, 0, &line, &column, 0);
1361-
printf("// %s: %s:%d:%d: ", FileCheckPrefix,
1362-
GetCursorSource(Cursor), line, column);
1367+
CXString source = GetCursorSource(Cursor);
1368+
printf("// %s: %s:%d:%d: ", FileCheckPrefix, clang_getCString(source), line,
1369+
column);
1370+
clang_disposeString(source);
13631371
PrintCursor(Cursor, Data->CommentSchemaFile);
13641372
PrintCursorExtent(Cursor);
13651373
if (clang_isDeclaration(Cursor.kind)) {
@@ -1428,8 +1436,10 @@ static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
14281436
if (Ref.kind == CXCursor_NoDeclFound) {
14291437
/* Nothing found here; that's fine. */
14301438
} else if (Ref.kind != CXCursor_FunctionDecl) {
1431-
printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
1432-
curLine, curColumn);
1439+
CXString CursorSource = GetCursorSource(Ref);
1440+
printf("// %s: %s:%d:%d: ", FileCheckPrefix,
1441+
clang_getCString(CursorSource), curLine, curColumn);
1442+
clang_disposeString(CursorSource);
14331443
PrintCursor(Ref, Data->CommentSchemaFile);
14341444
printf("\n");
14351445
}
@@ -1455,7 +1465,10 @@ enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
14551465
clang_disposeString(USR);
14561466
return CXChildVisit_Recurse;
14571467
}
1458-
printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1468+
CXString CursorSource = GetCursorSource(C);
1469+
printf("// %s: %s %s", FileCheckPrefix, clang_getCString(CursorSource),
1470+
cstr);
1471+
clang_disposeString(CursorSource);
14591472

14601473
PrintCursorExtent(C);
14611474
printf("\n");

0 commit comments

Comments
 (0)