[PATCH] D64262: Bitstream reader: Fix undefined behavior seen after rL364464

Bjorn Pettersson via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 5 13:25:52 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL365239: Bitstream reader: Fix undefined behavior seen after rL364464 (authored by bjope, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D64262?vs=208222&id=208226#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D64262/new/

https://reviews.llvm.org/D64262

Files:
  cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp
  clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp


Index: clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp
===================================================================
--- clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp
+++ clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp
@@ -615,10 +615,12 @@
       return Cursor::BadBlock;
     }
 
-    // FIXME check that the enum is in range.
-    auto Code = static_cast<llvm::bitc::FixedAbbrevIDs>(MaybeCode.get());
-
-    switch (Code) {
+    unsigned Code = MaybeCode.get();
+    if (Code >= static_cast<unsigned>(llvm::bitc::FIRST_APPLICATION_ABBREV)) {
+      BlockOrRecordID = Code;
+      return Cursor::Record;
+    }
+    switch (static_cast<llvm::bitc::FixedAbbrevIDs>(Code)) {
     case llvm::bitc::ENTER_SUBBLOCK:
       if (Expected<unsigned> MaybeID = Stream.ReadSubBlockID())
         BlockOrRecordID = MaybeID.get();
@@ -639,9 +641,8 @@
       continue;
     case llvm::bitc::UNABBREV_RECORD:
       return Cursor::BadBlock;
-    default:
-      BlockOrRecordID = Code;
-      return Cursor::Record;
+    case llvm::bitc::FIRST_APPLICATION_ABBREV:
+      llvm_unreachable("Unexpected abbrev id.");
     }
   }
   llvm_unreachable("Premature stream end.");
Index: cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp
===================================================================
--- cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp
+++ cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp
@@ -124,7 +124,12 @@
     else
       return llvm::errorToErrorCode(Res.takeError());
 
-    switch ((llvm::bitc::FixedAbbrevIDs)Code) {
+    if (Code >= static_cast<unsigned>(llvm::bitc::FIRST_APPLICATION_ABBREV)) {
+      // We found a record.
+      BlockOrRecordID = Code;
+      return Cursor::Record;
+    }
+    switch (static_cast<llvm::bitc::FixedAbbrevIDs>(Code)) {
     case llvm::bitc::ENTER_SUBBLOCK:
       if (Expected<unsigned> Res = Stream.ReadSubBlockID())
         BlockOrRecordID = Res.get();
@@ -145,10 +150,8 @@
     case llvm::bitc::UNABBREV_RECORD:
       return SDError::UnsupportedConstruct;
 
-    default:
-      // We found a record.
-      BlockOrRecordID = Code;
-      return Cursor::Record;
+    case llvm::bitc::FIRST_APPLICATION_ABBREV:
+      llvm_unreachable("Unexpected abbrev id.");
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64262.208226.patch
Type: text/x-patch
Size: 2260 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190705/b854638e/attachment-0001.bin>


More information about the cfe-commits mailing list