-
Notifications
You must be signed in to change notification settings - Fork 12.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Clang][Frontend] Fix a crash when -Wdocumentation is used #68525
Conversation
@llvm/pr-subscribers-clang ChangesThis commit resolves a crash issue in Clang's frontend caused while using the The flaw was due to the lack of necessary checks before the extraction of text between the comment and the declaration in the This could lead to an invalid length being passed to the Fixes #68524. Full diff: https://github.com/llvm/llvm-project/pull/68525.diff 1 Files Affected:
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);
|
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 llvm#68524.
83977fd
to
b272173
Compare
Is there a way we could come up with a test for this? |
Unfortunately, I don't think so. I cannot reduce the 600k lines of preprocessed code to a small test case that will crash the clang frontend. |
Have you tried using creduce or other such tool? (We generally don't accept patches without test coverage unless it really isn't possible to test the changes, that doesn't appear to be the situation here though.) |
Since I'm not an expert in clang AST, it is hard to reduce the failing cases. According to my analysis, this crash only happens when the multiple files are involved, so code reduction tools like creduce doesn't helpful a lot. In my local environment, I was building Apple's LLVM with dac71d2e8c4cdc9e0a1254dbf3716252c302d6a5 commit. (Note that I'm not making changes against Apple's LLVM. I'm just building Apple's LLVM(and Swift compiler) using the original LLVM ToT commit.) To explain the crash, I've made modifications to It seems that However, So the crash is happened because the result of The best way to fix this issue is to find out why they are not from the same source code and fix it. However, I'm not sure how to fix it, so I've made a patch to avoid the crash. This logic is behind by |
Double so for me.
While your fix resolves the specific crash we noticed, it does not fix the underlying problem. I found that the source of the problem is noted in this comment. I tried to “fix” this by removing a few lines starting here (maybe you could also remove lines 561-574?) and moving them a few lines down to get:
That way it no longer crashes … I think this loads the correct comments for each decl but I am not sure if this is the correct way to fix it (or even if it still does what it is supposed to do). |
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.