Skip to content

[clang] fix use after free in clang/tools/c-index-test/c-index-test.c #127063

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

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions clang/tools/c-index-test/c-index-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1213,28 +1213,34 @@ static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) {
}
}

static const char* GetCursorSource(CXCursor Cursor) {
static CXString createCXString(const char *CS) {
CXString Str;
Str.data = (const void *)CS;
Str.private_flags = 0;
return Str;
}

static CXString duplicateCXString(const char *CS) {
CXString Str;
Str.data = strdup(CS);
Str.private_flags = 1; // CXS_Malloc
return Str;
}

static CXString GetCursorSource(CXCursor Cursor) {
CXSourceLocation Loc = clang_getCursorLocation(Cursor);
CXString source;
CXFile file;
clang_getExpansionLocation(Loc, &file, 0, 0, 0);
source = clang_getFileName(file);
if (!clang_getCString(source)) {
clang_disposeString(source);
return "<invalid loc>";
return createCXString("<invalid loc>");
}
else {
const char *b = basename(clang_getCString(source));
clang_disposeString(source);
return b;
}
}

static CXString createCXString(const char *CS) {
CXString Str;
Str.data = (const void *) CS;
Str.private_flags = 0;
return Str;
const char *b = basename(clang_getCString(source));
CXString result = duplicateCXString(b);
clang_disposeString(source);
return result;
}

/******************************************************************************/
Expand Down Expand Up @@ -1358,8 +1364,10 @@ enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
CXSourceLocation Loc = clang_getCursorLocation(Cursor);
unsigned line, column;
clang_getFileLocation(Loc, 0, &line, &column, 0);
printf("// %s: %s:%d:%d: ", FileCheckPrefix,
GetCursorSource(Cursor), line, column);
CXString source = GetCursorSource(Cursor);
printf("// %s: %s:%d:%d: ", FileCheckPrefix, clang_getCString(source), line,
column);
clang_disposeString(source);
PrintCursor(Cursor, Data->CommentSchemaFile);
PrintCursorExtent(Cursor);
if (clang_isDeclaration(Cursor.kind)) {
Expand Down Expand Up @@ -1428,8 +1436,10 @@ static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
if (Ref.kind == CXCursor_NoDeclFound) {
/* Nothing found here; that's fine. */
} else if (Ref.kind != CXCursor_FunctionDecl) {
printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
curLine, curColumn);
CXString CursorSource = GetCursorSource(Ref);
printf("// %s: %s:%d:%d: ", FileCheckPrefix,
clang_getCString(CursorSource), curLine, curColumn);
clang_disposeString(CursorSource);
PrintCursor(Ref, Data->CommentSchemaFile);
printf("\n");
}
Expand All @@ -1455,7 +1465,10 @@ enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
clang_disposeString(USR);
return CXChildVisit_Recurse;
}
printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
CXString CursorSource = GetCursorSource(C);
printf("// %s: %s %s", FileCheckPrefix, clang_getCString(CursorSource),
cstr);
clang_disposeString(CursorSource);

PrintCursorExtent(C);
printf("\n");
Expand Down