From 159feac1c0a26dccc6dd8726bd71d073cda5faea Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 14 Jul 2022 16:24:37 +0200 Subject: [PATCH] [Bitcode] Report metadata decoding error more gracefully --- llvm/lib/Bitcode/Reader/MetadataLoader.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp index 0d57ae4..13d53a3 100644 --- a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp +++ b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp @@ -1226,10 +1226,12 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata( break; } - MetadataList.assignValue( - LocalAsMetadata::get(ValueList.getValueFwdRef( - Record[1], Ty, TyID, /*ConstExprInsertBB*/ nullptr)), - NextMetadataNo); + Value *V = ValueList.getValueFwdRef(Record[1], Ty, TyID, + /*ConstExprInsertBB*/ nullptr); + if (!V) + return error("Invalid value reference from old fn metadata"); + + MetadataList.assignValue(LocalAsMetadata::get(V), NextMetadataNo); NextMetadataNo++; break; } @@ -1248,8 +1250,11 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata( if (Ty->isMetadataTy()) Elts.push_back(getMD(Record[i + 1])); else if (!Ty->isVoidTy()) { - auto *MD = ValueAsMetadata::get(ValueList.getValueFwdRef( - Record[i + 1], Ty, TyID, /*ConstExprInsertBB*/ nullptr)); + Value *V = ValueList.getValueFwdRef(Record[i + 1], Ty, TyID, + /*ConstExprInsertBB*/ nullptr); + if (!V) + return error("Invalid value reference from old metadata"); + auto *MD = ValueAsMetadata::get(V); assert(isa(MD) && "Expected non-function-local metadata"); Elts.push_back(MD); @@ -1269,10 +1274,12 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata( if (Ty->isMetadataTy() || Ty->isVoidTy()) return error("Invalid record"); - MetadataList.assignValue( - ValueAsMetadata::get(ValueList.getValueFwdRef( - Record[1], Ty, TyID, /*ConstExprInsertBB*/ nullptr)), - NextMetadataNo); + Value *V = ValueList.getValueFwdRef(Record[1], Ty, TyID, + /*ConstExprInsertBB*/ nullptr); + if (!V) + return error("Invalid value reference from metadata"); + + MetadataList.assignValue(ValueAsMetadata::get(V), NextMetadataNo); NextMetadataNo++; break; } -- 2.7.4