From: Daniel Sanders Date: Fri, 18 Jan 2019 18:40:35 +0000 (+0000) Subject: [adt] Twine(nullptr) derefs the nullptr. Add a deleted Twine(std::nullptr_t) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ecd3de774e89c33aa1bdcc4d3f0376c432ff3322;p=llvm [adt] Twine(nullptr) derefs the nullptr. Add a deleted Twine(std::nullptr_t) Summary: nullptr can implicitly convert to Twine as Twine(nullptr) in which case it resolves to Twine(const char *). This constructor derefs the pointer and therefore doesn't work. Add a Twine(std::nullptr_t) = delete to make it a compile time error. It turns out that in-tree usage of Twine(nullptr) is confined to a single private method in IRBuilder where foldConstant(... const Twine &Name = nullptr) and this method is only ever called with an explicit Name argument as making it a mandatory argument doesn't cause compile-time or run-time errors. Reviewers: jyknight Reviewed By: jyknight Subscribers: dexonsmith, kristina, llvm-commits Differential Revision: https://reviews.llvm.org/D56870 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351572 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/Twine.h b/include/llvm/ADT/Twine.h index b60fd098139..004ec072ffb 100644 --- a/include/llvm/ADT/Twine.h +++ b/include/llvm/ADT/Twine.h @@ -274,6 +274,9 @@ namespace llvm { assert(isValid() && "Invalid twine!"); } + /// Delete the implicit conversion from nullptr as Twine(const char *) + /// cannot take nullptr. + /*implicit*/ Twine(std::nullptr_t) = delete; /// Construct from an std::string. /*implicit*/ Twine(const std::string &Str) : LHSKind(StdStringKind) { diff --git a/include/llvm/IR/IRBuilder.h b/include/llvm/IR/IRBuilder.h index fac2ff46c45..914752f3ce6 100644 --- a/include/llvm/IR/IRBuilder.h +++ b/include/llvm/IR/IRBuilder.h @@ -1004,7 +1004,7 @@ private: } Value *foldConstant(Instruction::BinaryOps Opc, Value *L, - Value *R, const Twine &Name = nullptr) const { + Value *R, const Twine &Name) const { auto *LC = dyn_cast(L); auto *RC = dyn_cast(R); return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;