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:
    cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp

Modified: cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp?rev=365239&r1=365238&r2=365239&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp (original)
+++ cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp Fri Jul  5 13:22:40 2019
@@ -124,7 +124,12 @@ SerializedDiagnosticReader::skipUntilRec
     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 @@ SerializedDiagnosticReader::skipUntilRec
     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.");
     }
   }
 




More information about the cfe-commits mailing list