Skip to content

Commit 7446601

Browse files
authored
[-Wunsafe-buffer-usage] Fix a potential overflow bug reported by #126334 (#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".
1 parent 818bca8 commit 7446601

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,12 +2364,13 @@ template <typename NodeTy>
23642364
static std::optional<SourceLocation>
23652365
getEndCharLoc(const NodeTy *Node, const SourceManager &SM,
23662366
const LangOptions &LangOpts) {
2367-
unsigned TkLen = Lexer::MeasureTokenLength(Node->getEndLoc(), SM, LangOpts);
2368-
SourceLocation Loc = Node->getEndLoc().getLocWithOffset(TkLen - 1);
2369-
2370-
if (Loc.isValid())
2371-
return Loc;
2367+
if (unsigned TkLen =
2368+
Lexer::MeasureTokenLength(Node->getEndLoc(), SM, LangOpts)) {
2369+
SourceLocation Loc = Node->getEndLoc().getLocWithOffset(TkLen - 1);
23722370

2371+
if (Loc.isValid())
2372+
return Loc;
2373+
}
23732374
return std::nullopt;
23742375
}
23752376

0 commit comments

Comments
 (0)