Skip to content

Commit 8a71854

Browse files
committed
[Bitcode] Handle invalid abbrev number error more gracefully
Avoid report_fatal_error(), propagate the error upwards instead.
1 parent 82ef888 commit 8a71854

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

llvm/include/llvm/Bitstream/BitstreamReader.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,11 @@ class BitstreamCursor : SimpleBitstreamCursor {
521521

522522
public:
523523
/// Return the abbreviation for the specified AbbrevId.
524-
const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
524+
Expected<const BitCodeAbbrev *> getAbbrev(unsigned AbbrevID) {
525525
unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV;
526526
if (AbbrevNo >= CurAbbrevs.size())
527-
report_fatal_error("Invalid abbrev number");
527+
return createStringError(
528+
std::errc::illegal_byte_sequence, "Invalid abbrev number");
528529
return CurAbbrevs[AbbrevNo].get();
529530
}
530531

llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,10 @@ Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
864864
O->OS << " codeid=" << Code;
865865
const BitCodeAbbrev *Abbv = nullptr;
866866
if (Entry.ID != bitc::UNABBREV_RECORD) {
867-
Abbv = Stream.getAbbrev(Entry.ID);
867+
Expected<const BitCodeAbbrev *> MaybeAbbv = Stream.getAbbrev(Entry.ID);
868+
if (!MaybeAbbv)
869+
return MaybeAbbv.takeError();
870+
Abbv = MaybeAbbv.get();
868871
O->OS << " abbrevid=" << Entry.ID;
869872
}
870873

llvm/lib/Bitstream/Reader/BitstreamReader.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ Expected<unsigned> BitstreamCursor::skipRecord(unsigned AbbrevID) {
111111
return Code;
112112
}
113113

114-
const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
114+
Expected<const BitCodeAbbrev *> MaybeAbbv = getAbbrev(AbbrevID);
115+
if (!MaybeAbbv)
116+
return MaybeAbbv.takeError();
117+
118+
const BitCodeAbbrev *Abbv = MaybeAbbv.get();
115119
const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
116120
unsigned Code;
117121
if (CodeOp.isLiteral())
@@ -228,7 +232,10 @@ Expected<unsigned> BitstreamCursor::readRecord(unsigned AbbrevID,
228232
return Code;
229233
}
230234

231-
const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
235+
Expected<const BitCodeAbbrev *> MaybeAbbv = getAbbrev(AbbrevID);
236+
if (!MaybeAbbv)
237+
return MaybeAbbv.takeError();
238+
const BitCodeAbbrev *Abbv = MaybeAbbv.get();
232239

233240
// Read the record code first.
234241
assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BC��e---C

llvm/test/Bitcode/invalid.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,8 @@ RUN: not llvm-dis -disable-output %p/Inputs/invalid-cmpxchg-ordering-4.bc 2>&1 |
246246
RUN: FileCheck --check-prefix=CMPXCHG-ORDERING %s
247247

248248
CMPXCHG-ORDERING: Invalid cmpxchg {{failure|success}} ordering
249+
250+
RUN: not llvm-dis -disable-output %p/Inputs/invalid-abbrev-number.bc 2>&1 | \
251+
RUN: FileCheck --check-prefix=INVALID-ABBREV-NUMBER %s
252+
253+
INVALID-ABBREV-NUMBER: Invalid abbrev number

0 commit comments

Comments
 (0)