diff options
author | Ella Ma <alansnape3058@gmail.com> | 2020-11-21 21:04:12 -0800 |
---|---|---|
committer | Fangrui Song <i@maskray.me> | 2020-11-21 21:04:12 -0800 |
commit | 1756d67934bb5fe3b12bdb5fa55d61f61bd70bc5 (patch) | |
tree | e7d120ab09527863c6179918e32d1aaef9c874e0 /llvm/lib/LTO/LTOBackend.cpp | |
parent | 914f6c4ff8a42d384cad0bbb07de4dd1a96c78d4 (diff) | |
download | llvm-1756d67934bb5fe3b12bdb5fa55d61f61bd70bc5.zip llvm-1756d67934bb5fe3b12bdb5fa55d61f61bd70bc5.tar.gz llvm-1756d67934bb5fe3b12bdb5fa55d61f61bd70bc5.tar.bz2 |
[llvm][clang][mlir] Add checks for the return values from Target::createXXX to prevent protential null deref
All these potential null pointer dereferences are reported by my static analyzer for null smart pointer dereferences, which has a different implementation from `alpha.cplusplus.SmartPtr`.
The checked pointers in this patch are initialized by Target::createXXX functions. When the creator function pointer is not correctly set, a null pointer will be returned, or the creator function may originally return a null pointer.
Some of them may not make sense as they may be checked before entering the function, but I fixed them all in this patch. I submit this fix because 1) similar checks are found in some other places in the LLVM codebase for the same return value of the function; and, 2) some of the pointers are dereferenced before they are checked, which may definitely trigger a null pointer dereference if the return value is nullptr.
Reviewed By: tejohnson, MaskRay, jpienaar
Differential Revision: https://reviews.llvm.org/D91410
Diffstat (limited to 'llvm/lib/LTO/LTOBackend.cpp')
-rw-r--r-- | llvm/lib/LTO/LTOBackend.cpp | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index 635584a..3365880 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -199,9 +199,11 @@ createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) { else CodeModel = M.getCodeModel(); - return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine( + std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine( TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel, CodeModel, Conf.CGOptLevel)); + assert(TM && "Failed to create target machine"); + return TM; } static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM, |