[PATCH] D56870: [adt] Twine(nullptr) derefs the nullptr. Add a deleted Twine(std::nullptr_t)

Daniel Sanders via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 17 11:35:20 PST 2019


dsanders created this revision.
dsanders added a reviewer: jyknight.
Herald added subscribers: kristina, dexonsmith.

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.


Repository:
  rL LLVM

https://reviews.llvm.org/D56870

Files:
  include/llvm/ADT/Twine.h
  include/llvm/IR/IRBuilder.h


Index: include/llvm/IR/IRBuilder.h
===================================================================
--- include/llvm/IR/IRBuilder.h
+++ include/llvm/IR/IRBuilder.h
@@ -1004,7 +1004,7 @@
   }
 
   Value *foldConstant(Instruction::BinaryOps Opc, Value *L,
-                      Value *R, const Twine &Name = nullptr) const {
+                      Value *R, const Twine &Name) const {
     auto *LC = dyn_cast<Constant>(L);
     auto *RC = dyn_cast<Constant>(R);
     return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;
Index: include/llvm/ADT/Twine.h
===================================================================
--- include/llvm/ADT/Twine.h
+++ include/llvm/ADT/Twine.h
@@ -274,6 +274,9 @@
 
       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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56870.182363.patch
Type: text/x-patch
Size: 1053 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190117/89ff1d8d/attachment.bin>


More information about the llvm-commits mailing list