Author: Serafean
Date: 2026-02-20T17:49:22Z
New Revision: da3ddab6a53ba56b077b80f7db5c3e91b7da52a0

URL: 
https://github.com/llvm/llvm-project/commit/da3ddab6a53ba56b077b80f7db5c3e91b7da52a0
DIFF: 
https://github.com/llvm/llvm-project/commit/da3ddab6a53ba56b077b80f7db5c3e91b7da52a0.diff

LOG: Fix crash in clang_getUnaryOperatorKindSpelling() (#182247)

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.

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/include/clang-c/Index.h
    clang/tools/libclang/CIndex.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 371fb1625cb05..b1d7c77e65958 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -408,6 +408,7 @@ clang-format
 
 libclang
 --------
+- Fix crash in clang_getBinaryOperatorKindSpelling and 
clang_getUnaryOperatorKindSpelling
 
 Code Completion
 ---------------

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 865f6520816a2..06b86b51ee36d 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -10181,7 +10181,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(
@@ -10203,6 +10203,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)));
 }


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to