https://github.com/cor3ntin created 
https://github.com/llvm/llvm-project/pull/141940

When the comment ends with a splice at EOF.

Fixes #141221

>From ca2ea7359ac2f0f0ea41396990847479b5a3e2b7 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinja...@gmail.com>
Date: Thu, 29 May 2025 15:14:55 +0200
Subject: [PATCH] [Clang] Fix an out of bound access in -verify comment parsing

When the comment ends with a splice at EOF.

Fixes #141221
---
 clang/lib/Frontend/VerifyDiagnosticConsumer.cpp | 2 +-
 clang/test/Frontend/verify-gh141221.c           | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Frontend/verify-gh141221.c

diff --git a/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp 
b/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp
index 89fda3e839cb9..eb241f34d095e 100644
--- a/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp
+++ b/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp
@@ -812,7 +812,7 @@ bool VerifyDiagnosticConsumer::HandleComment(Preprocessor 
&PP,
     C2 += C.substr(last, loc-last);
     last = loc + 1;
 
-    if (C[last] == '\n' || C[last] == '\r') {
+    if (last < C.size() && (C[last] == '\n' || C[last] == '\r')) {
       ++last;
 
       // Escape \r\n  or \n\r, but not \n\n.
diff --git a/clang/test/Frontend/verify-gh141221.c 
b/clang/test/Frontend/verify-gh141221.c
new file mode 100644
index 0000000000000..eb0be46a3da8b
--- /dev/null
+++ b/clang/test/Frontend/verify-gh141221.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -verify %s
+
+// Check that we don't crash if the file ends in a splice
+// This file should *NOT* end with a new line
+a;
+// expected-error@-1 {{}} \
\ No newline at end of file

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to