[PATCH] D118448: clang-format: [JS] fix uninitialized memory.

Martin Probst via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 28 03:18:10 PST 2022


mprobst created this revision.
mprobst added reviewers: jankuehle, krasimir.
mprobst requested review of this revision.
Herald added a project: clang.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118448

Files:
  clang/lib/Format/SortJavaScriptImports.cpp


Index: clang/lib/Format/SortJavaScriptImports.cpp
===================================================================
--- clang/lib/Format/SortJavaScriptImports.cpp
+++ clang/lib/Format/SortJavaScriptImports.cpp
@@ -133,7 +133,10 @@
 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 @@
     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;
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118448.403949.patch
Type: text/x-patch
Size: 969 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220128/9c1c679c/attachment-0001.bin>


More information about the cfe-commits mailing list