[llvm] r230935 - Teach the LLParser to fail gracefully when it encounters an invalid label name.

Owen Anderson resistor at mac.com
Sun Mar 1 21:25:10 PST 2015


Author: resistor
Date: Sun Mar  1 23:25:09 2015
New Revision: 230935

URL: http://llvm.org/viewvc/llvm-project?rev=230935&view=rev
Log:
Teach the LLParser to fail gracefully when it encounters an invalid label name.

Previous it would either assert in +Asserts, or crash in -Asserts. Found by fuzzing LLParser.

Added:
    llvm/trunk/test/Assembler/invalid-label.ll
Modified:
    llvm/trunk/lib/AsmParser/LLParser.cpp

Modified: llvm/trunk/lib/AsmParser/LLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.cpp?rev=230935&r1=230934&r2=230935&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLParser.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLParser.cpp Sun Mar  1 23:25:09 2015
@@ -2270,13 +2270,13 @@ bool LLParser::PerFunctionState::SetInst
 /// forward reference record if needed.
 BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
                                               LocTy Loc) {
-  return cast_or_null<BasicBlock>(GetVal(Name,
-                                        Type::getLabelTy(F.getContext()), Loc));
+  return dyn_cast_or_null<BasicBlock>(GetVal(Name,
+                                      Type::getLabelTy(F.getContext()), Loc));
 }
 
 BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
-  return cast_or_null<BasicBlock>(GetVal(ID,
-                                        Type::getLabelTy(F.getContext()), Loc));
+  return dyn_cast_or_null<BasicBlock>(GetVal(ID,
+                                      Type::getLabelTy(F.getContext()), Loc));
 }
 
 /// DefineBB - Define the specified basic block, which is either named or
@@ -4299,7 +4299,9 @@ bool LLParser::ParseBasicBlock(PerFuncti
   }
 
   BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
-  if (!BB) return true;
+  if (!BB)
+    return Error(NameLoc,
+                 "unable to create block named '" + Name + "'");
 
   std::string NameStr;
 

Added: llvm/trunk/test/Assembler/invalid-label.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/invalid-label.ll?rev=230935&view=auto
==============================================================================
--- llvm/trunk/test/Assembler/invalid-label.ll (added)
+++ llvm/trunk/test/Assembler/invalid-label.ll Sun Mar  1 23:25:09 2015
@@ -0,0 +1,11 @@
+; RUN: not llvm-as < %s >/dev/null 2> %t
+; RUN: FileCheck %s < %t
+; Test the case where an invalid label name is used
+
+; CHECK: unable to create block named 'bb'
+
+define void @test(label %bb) {
+bb:
+  ret void
+}
+





More information about the llvm-commits mailing list