[clang] c267292 - clang-format: [JS] fix uninitialized memory.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 28 03:37:53 PST 2022


Author: Martin Probst
Date: 2022-01-28T12:37:45+01:00
New Revision: c26729251588cb6e9e20c3edf67d14ac9b549f9b

URL: https://github.com/llvm/llvm-project/commit/c26729251588cb6e9e20c3edf67d14ac9b549f9b
DIFF: https://github.com/llvm/llvm-project/commit/c26729251588cb6e9e20c3edf67d14ac9b549f9b.diff

LOG: clang-format: [JS] fix uninitialized memory.

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

Added: 
    

Modified: 
    clang/lib/Format/SortJavaScriptImports.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/SortJavaScriptImports.cpp b/clang/lib/Format/SortJavaScriptImports.cpp
index 5d1ad9ae470f..71326163f45a 100644
--- a/clang/lib/Format/SortJavaScriptImports.cpp
+++ b/clang/lib/Format/SortJavaScriptImports.cpp
@@ -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,
@@ -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;
     }
   }


        


More information about the cfe-commits mailing list