[clang] Fix crash in clang_getUnaryOperatorKindSpelling() (PR #182247)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 19 03:29:25 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: None (Serafean)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/182247.diff
2 Files Affected:
- (modified) clang/include/clang-c/Index.h (+2-1)
- (modified) clang/tools/libclang/CIndex.cpp (+4-1)
``````````diff
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..c81a3cf51522d 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)));
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/182247
More information about the cfe-commits
mailing list