https://github.com/Serafean created 
https://github.com/llvm/llvm-project/pull/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.

>From fc04104d2e3abde6412405eaa9e9b45328513024 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Bedn=C3=A1r?= <[email protected]>
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..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)));
 }

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

Reply via email to