[clang] [Clang][Frontend] Fix a crash when -Wdocumentation is used (PR #68525)

Byoungchan Lee via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 8 06:11:50 PDT 2023


https://github.com/bc-lee created https://github.com/llvm/llvm-project/pull/68525

This commit resolves a crash issue in Clang's frontend caused while using the `-Wdocumentation` compiler flag.

The flaw was due to the lack of necessary checks before the extraction of text between the comment and the declaration in the `ASTContext.cpp` file. Specifically, there was no verification to ensure that the second component of the declaration location's decomposition is not less than the comment's end offset.

This could lead to an invalid length being passed to the `StringRef` constructor, triggering the crash. I have added a check to prevent this crash from occurring.

Fixes #68524.

>From 83977fda4860a6b2a99c9f5ad166fd62a8735da1 Mon Sep 17 00:00:00 2001
From: Byoungchan Lee <byoungchan.lee at gmx.com>
Date: Sun, 8 Oct 2023 21:47:05 +0900
Subject: [PATCH] [Clang][Frontend] Fix a crash when -Wdocumentation is used

This commit resolves a crash issue in Clang's frontend caused while using
the `-Wdocumentation` compiler flag.

The flaw was due to the lack of necessary checks before the extraction of
text between the comment and the declaration in the `ASTContext.cpp` file.
Specifically, there was no verification to ensure that the second component
of the declaration location's decomposition is not less than the comment's
end offset.

This could lead to an invalid length being passed to the `StringRef`
constructor, triggering the crash. I have added a check to prevent this
crash from occurring.

Fixes #68524.
---
 clang/lib/AST/ASTContext.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index cdc3d62bca00873..7b4a4202921281c 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -344,6 +344,9 @@ RawComment *ASTContext::getRawCommentForDeclNoCacheImpl(
   if (Invalid)
     return nullptr;
 
+  if (DeclLocDecomp.second < CommentEndOffset)
+    return nullptr;
+
   // Extract text between the comment and declaration.
   StringRef Text(Buffer + CommentEndOffset,
                  DeclLocDecomp.second - CommentEndOffset);



More information about the cfe-commits mailing list