[clang] [Clang] Fix an out of bound access in -verify comment parsing (PR #141940)

via cfe-commits cfe-commits at lists.llvm.org
Thu May 29 09:40:41 PDT 2025


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

>From ca2ea7359ac2f0f0ea41396990847479b5a3e2b7 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at 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 at -1 {{}} \
\ No newline at end of file



More information about the cfe-commits mailing list