Skip to content

Conversation

@ziqingluo-90
Copy link
Contributor

MeasureTokenLength may return an unsigned 0 representing failure in obtaining length of a token. The analysis now gives up on such cases. Otherwise, there might be issues caused by unsigned integer "overflow".

…#126334

`MeasureTokenLength` may return an unsigned 0 representing
failure in obtaining length of a token.  The analysis now gives
up on such cases. Otherwise, there might be issues caused by
unsigned integer "overflow".
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:analysis labels Feb 28, 2025
@ziqingluo-90
Copy link
Contributor Author

CC: @dtarditi

@llvmbot
Copy link
Member

llvmbot commented Feb 28, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-analysis

Author: Ziqing Luo (ziqingluo-90)

Changes

MeasureTokenLength may return an unsigned 0 representing failure in obtaining length of a token. The analysis now gives up on such cases. Otherwise, there might be issues caused by unsigned integer "overflow".


Full diff: https://github.com/llvm/llvm-project/pull/129169.diff

1 Files Affected:

  • (modified) clang/lib/Analysis/UnsafeBufferUsage.cpp (+6-5)
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index ff4f940a596e3..12e99143cb148 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -2364,12 +2364,13 @@ template <typename NodeTy>
 static std::optional<SourceLocation>
 getEndCharLoc(const NodeTy *Node, const SourceManager &SM,
               const LangOptions &LangOpts) {
-  unsigned TkLen = Lexer::MeasureTokenLength(Node->getEndLoc(), SM, LangOpts);
-  SourceLocation Loc = Node->getEndLoc().getLocWithOffset(TkLen - 1);
-
-  if (Loc.isValid())
-    return Loc;
+  if (unsigned TkLen =
+          Lexer::MeasureTokenLength(Node->getEndLoc(), SM, LangOpts)) {
+    SourceLocation Loc = Node->getEndLoc().getLocWithOffset(TkLen - 1);
 
+    if (Loc.isValid())
+      return Loc;
+  }
   return std::nullopt;
 }
 

Copy link
Contributor

@dtarditi dtarditi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ziqingluo-90 ziqingluo-90 merged commit 7446601 into llvm:main Feb 28, 2025
14 checks passed
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for the quick fix!

cheezeburglar pushed a commit to cheezeburglar/llvm-project that referenced this pull request Feb 28, 2025
…#126334 (llvm#129169)

`MeasureTokenLength` may return an unsigned 0 representing failure in
obtaining length of a token. The analysis now gives up on such cases.
Otherwise, there might be issues caused by unsigned integer "overflow".
@ziqingluo-90 ziqingluo-90 deleted the dev/ziqing/issue-126334 branch March 4, 2025 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:analysis clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants