[clang] Fix crash in clang_getUnaryOperatorKindSpelling() (PR #182247)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 19 05:06:13 PST 2026
https://github.com/Serafean updated https://github.com/llvm/llvm-project/pull/182247
>From de5d621d6ed8b85a491f05cdeb0a99591f7767ea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Bedn=C3=A1r?= <martin at serafean.cz>
Date: Wed, 18 Feb 2026 18:12:59 +0100
Subject: [PATCH] Fix crash in clang_getUnaryOperatorKindSpelling()
When clang_getUnaryOperatorKindSpelling() is called with
CXUnaryOperator_Invalid as argument, UnaryOperator::getOpcodeStr() gets
called with an invalid Opcode of -1, leading to a crash.
Fix it by checking for the last valid opcode as is done in
clang_getBinaryOperatorKindSpelling().
Do the same for clang_getBinaryOperatorKindSpelling(), as its logic is
the same.
---
clang/include/clang-c/Index.h | 3 ++-
clang/tools/libclang/CIndex.cpp | 5 ++++-
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h
index f13d9c9307b40..203634c80d82a 100644
--- a/clang/include/clang-c/Index.h
+++ b/clang/include/clang-c/Index.h
@@ -6932,7 +6932,8 @@ enum CXUnaryOperatorKind {
/** __extension__ marker operator. */
CXUnaryOperator_Extension,
/** C++ co_await operator. */
- CXUnaryOperator_Coawait
+ CXUnaryOperator_Coawait,
+ CXUnaryOperator_Last = CXUnaryOperator_Coawait
};
/**
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index 15eec87652451..29cd3973da1c5 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -10173,7 +10173,7 @@ cxindex::Logger::~Logger() {
}
CXString clang_getBinaryOperatorKindSpelling(enum CXBinaryOperatorKind kind) {
- if (kind > CXBinaryOperator_Last)
+ if (kind <= CXBinaryOperator_Invalid || kind > CXBinaryOperator_Last)
return cxstring::createEmpty();
return cxstring::createDup(
@@ -10195,6 +10195,9 @@ enum CXBinaryOperatorKind clang_getCursorBinaryOperatorKind(CXCursor cursor) {
}
CXString clang_getUnaryOperatorKindSpelling(enum CXUnaryOperatorKind kind) {
+ if (kind <= CXUnaryOperator_Invalid || kind > CXUnaryOperator_Last)
+ return cxstring::createEmpty();
+
return cxstring::createRef(
UnaryOperator::getOpcodeStr(static_cast<UnaryOperatorKind>(kind - 1)));
}
More information about the cfe-commits
mailing list