[clang-tools-extra] r365239 - Bitstream reader: Fix undefined behavior seen after rL364464

Bjorn Pettersson via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 5 13:22:40 PDT 2019


Author: bjope
Date: Fri Jul  5 13:22:40 2019
New Revision: 365239

URL: http://llvm.org/viewvc/llvm-project?rev=365239&view=rev
Log:
Bitstream reader: Fix undefined behavior seen after rL364464

Summary:
After rL364464 the following tests started to fail when
running the clang-doc tests with an ubsan instrumented
build of clang-doc:
    Clang Tools :: clang-doc/single-file-public.cpp
    Extra Tools Unit Tests :: clang-doc/./ClangDocTests/BitcodeTest.emitEnumInfoBitcode
    Extra Tools Unit Tests :: clang-doc/./ClangDocTests/BitcodeTest.emitMethodInfoBitcode
    Extra Tools Unit Tests :: clang-doc/./ClangDocTests/BitcodeTest.emitRecordInfoBitcode
    Extra Tools Unit Tests :: clang-doc/./ClangDocTests/SerializeTest.emitInfoWithCommentBitcode

We need to check that the read value is in range for being
casted to the llvm::bitc::FixedAbbrevIDs enum, before the
cast in ClangDocBitcodeReader::skipUntilRecordOrBlock.

SerializedDiagnosticReader::skipUntilRecordOrBlock was updated
in the same way.

Reviewers: jfb

Reviewed By: jfb

Subscribers: Bigcheese, vsapsai, bruno, ilya-biryukov, dexonsmith, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D64262

Modified:
    clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp

Modified: clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp?rev=365239&r1=365238&r2=365239&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp (original)
+++ clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp Fri Jul  5 13:22:40 2019
@@ -615,10 +615,12 @@ ClangDocBitcodeReader::skipUntilRecordOr
       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 @@ ClangDocBitcodeReader::skipUntilRecordOr
       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.");




More information about the cfe-commits mailing list