Skip to content

Commit

Permalink
clang-format: [JS] fix uninitialized memory.
Browse files Browse the repository at this point in the history
SortJavaScriptImports attempts to set its currently parsed token to an
invalid token when it reaches the end of the line. However in doing so,
it used a `FormatToken`, which contains a `Token Tok`. `Token` does not
have a constructor, so its fields start out as uninitialized memory.

`Token::startToken()` initializes all fields. Calling it in
`JavaScriptImportSorter`'s constructor thus fixes the problem.

Differential Revision: https://reviews.llvm.org/D118448
  • Loading branch information
mprobst committed Jan 28, 2022
1 parent cd79ca6 commit c267292
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clang/lib/Format/SortJavaScriptImports.cpp
Expand Up @@ -133,7 +133,10 @@ class JavaScriptImportSorter : public TokenAnalyzer {
public:
JavaScriptImportSorter(const Environment &Env, const FormatStyle &Style)
: TokenAnalyzer(Env, Style),
FileContents(Env.getSourceManager().getBufferData(Env.getFileID())) {}
FileContents(Env.getSourceManager().getBufferData(Env.getFileID())) {
// FormatToken.Tok starts out in an uninitialized state.
invalidToken.Tok.startToken();
}

std::pair<tooling::Replacements, unsigned>
analyze(TokenAnnotator &Annotator,
Expand Down Expand Up @@ -232,7 +235,6 @@ class JavaScriptImportSorter : public TokenAnalyzer {
if (!Current || Current == LineEnd->Next) {
// Set the current token to an invalid token, so that further parsing on
// this line fails.
invalidToken.Tok.setKind(tok::unknown);
Current = &invalidToken;
}
}
Expand Down

0 comments on commit c267292

Please sign in to comment.