[llvm] r237833 - AsmParser: Require a terminating null character when creating memory buffer.

Alex Lorenz arphaman at gmail.com
Wed May 20 13:41:28 PDT 2015


Author: arphaman
Date: Wed May 20 15:41:27 2015
New Revision: 237833

URL: http://llvm.org/viewvc/llvm-project?rev=237833&view=rev
Log:
AsmParser: Require a terminating null character when creating memory buffer.

This commit modifies the memory buffer creation in the AsmParser library so 
that it requires a terminating null character. The LLLexer in the AsmParser
library checks for EOF only when it sees a null character, thus it would
be best to require it when creating a memory buffer so that the memory
buffer constructor can verify that a terminating null character is indeed
present.

Reviewers: Duncan P. N. Exon Smith, Matthias Braun

Differential Revision: http://reviews.llvm.org/D9883

Added:
    llvm/trunk/unittests/AsmParser/
    llvm/trunk/unittests/AsmParser/AsmParserTest.cpp
    llvm/trunk/unittests/AsmParser/CMakeLists.txt
    llvm/trunk/unittests/AsmParser/Makefile
Modified:
    llvm/trunk/lib/AsmParser/Parser.cpp
    llvm/trunk/unittests/CMakeLists.txt
    llvm/trunk/unittests/Makefile

Modified: llvm/trunk/lib/AsmParser/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/Parser.cpp?rev=237833&r1=237832&r2=237833&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/Parser.cpp (original)
+++ llvm/trunk/lib/AsmParser/Parser.cpp Wed May 20 15:41:27 2015
@@ -24,7 +24,7 @@ using namespace llvm;
 
 bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err) {
   SourceMgr SM;
-  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F, false);
+  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
   SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
 
   return LLParser(F.getBuffer(), SM, Err, &M).Run();

Added: llvm/trunk/unittests/AsmParser/AsmParserTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/AsmParser/AsmParserTest.cpp?rev=237833&view=auto
==============================================================================
--- llvm/trunk/unittests/AsmParser/AsmParserTest.cpp (added)
+++ llvm/trunk/unittests/AsmParser/AsmParserTest.cpp Wed May 20 15:41:27 2015
@@ -0,0 +1,47 @@
+//===- llvm/unittest/AsmParser/AsmParserTest.cpp - asm parser unittests ---===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/SourceMgr.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+TEST(AsmParserTest, NullTerminatedInput) {
+  LLVMContext &Ctx = getGlobalContext();
+  StringRef Source = "; Empty module \n";
+  SMDiagnostic Error;
+  auto Mod = parseAssemblyString(Source, Error, Ctx);
+
+  EXPECT_TRUE(Mod != nullptr);
+  EXPECT_TRUE(Error.getMessage().empty());
+}
+
+#ifdef GTEST_HAS_DEATH_TEST
+#ifndef NDEBUG
+
+TEST(AsmParserTest, NonNullTerminatedInput) {
+  LLVMContext &Ctx = getGlobalContext();
+  StringRef Source = "; Empty module \n\1\2";
+  SMDiagnostic Error;
+  std::unique_ptr<Module> Mod;
+  EXPECT_DEATH(Mod = parseAssemblyString(Source.substr(0, Source.size() - 2),
+                                         Error, Ctx),
+               "Buffer is not null terminated!");
+}
+
+#endif
+#endif
+
+} // end anonymous namespace

Added: llvm/trunk/unittests/AsmParser/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/AsmParser/CMakeLists.txt?rev=237833&view=auto
==============================================================================
--- llvm/trunk/unittests/AsmParser/CMakeLists.txt (added)
+++ llvm/trunk/unittests/AsmParser/CMakeLists.txt Wed May 20 15:41:27 2015
@@ -0,0 +1,9 @@
+set(LLVM_LINK_COMPONENTS
+  AsmParser
+  Core
+  Support
+  )
+
+add_llvm_unittest(AsmParserTests
+  AsmParserTest.cpp
+  )

Added: llvm/trunk/unittests/AsmParser/Makefile
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/AsmParser/Makefile?rev=237833&view=auto
==============================================================================
--- llvm/trunk/unittests/AsmParser/Makefile (added)
+++ llvm/trunk/unittests/AsmParser/Makefile Wed May 20 15:41:27 2015
@@ -0,0 +1,15 @@
+##===- unittests/AsmParser/Makefile ------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../..
+TESTNAME = AsmParser
+LINK_COMPONENTS := AsmParser Core Support
+
+include $(LEVEL)/Makefile.config
+include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest

Modified: llvm/trunk/unittests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/CMakeLists.txt?rev=237833&r1=237832&r2=237833&view=diff
==============================================================================
--- llvm/trunk/unittests/CMakeLists.txt (original)
+++ llvm/trunk/unittests/CMakeLists.txt Wed May 20 15:41:27 2015
@@ -13,6 +13,7 @@ endfunction()
 
 add_subdirectory(ADT)
 add_subdirectory(Analysis)
+add_subdirectory(AsmParser)
 add_subdirectory(Bitcode)
 add_subdirectory(CodeGen)
 add_subdirectory(DebugInfo)

Modified: llvm/trunk/unittests/Makefile
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Makefile?rev=237833&r1=237832&r2=237833&view=diff
==============================================================================
--- llvm/trunk/unittests/Makefile (original)
+++ llvm/trunk/unittests/Makefile Wed May 20 15:41:27 2015
@@ -9,8 +9,9 @@
 
 LEVEL = ..
 
-PARALLEL_DIRS = ADT Analysis Bitcode CodeGen DebugInfo ExecutionEngine IR \
-		LineEditor Linker MC Option ProfileData Support Transforms
+PARALLEL_DIRS = ADT Analysis AsmParser Bitcode CodeGen DebugInfo \
+                ExecutionEngine IR LineEditor Linker MC Option ProfileData \
+                Support Transforms
 
 include $(LEVEL)/Makefile.config
 include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest





More information about the llvm-commits mailing list