summaryrefslogtreecommitdiffstats
path: root/unittests/AsmParser
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2015-05-20 20:41:27 +0000
committerAlex Lorenz <arphaman@gmail.com>2015-05-20 20:41:27 +0000
commit3f1b2bdf5d379cb9dc29f359033f567b921926ff (patch)
tree7035e86b74bf5ca63bce4b75a97301ccf8b51869 /unittests/AsmParser
parente86ba4b5710d51359fb9e66c529ca4a56c9e59fb (diff)
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237833 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/AsmParser')
-rw-r--r--unittests/AsmParser/AsmParserTest.cpp47
-rw-r--r--unittests/AsmParser/CMakeLists.txt9
-rw-r--r--unittests/AsmParser/Makefile15
3 files changed, 71 insertions, 0 deletions
diff --git a/unittests/AsmParser/AsmParserTest.cpp b/unittests/AsmParser/AsmParserTest.cpp
new file mode 100644
index 000000000000..8847b1871673
--- /dev/null
+++ b/unittests/AsmParser/AsmParserTest.cpp
@@ -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
diff --git a/unittests/AsmParser/CMakeLists.txt b/unittests/AsmParser/CMakeLists.txt
new file mode 100644
index 000000000000..1920bfaa3aac
--- /dev/null
+++ b/unittests/AsmParser/CMakeLists.txt
@@ -0,0 +1,9 @@
+set(LLVM_LINK_COMPONENTS
+ AsmParser
+ Core
+ Support
+ )
+
+add_llvm_unittest(AsmParserTests
+ AsmParserTest.cpp
+ )
diff --git a/unittests/AsmParser/Makefile b/unittests/AsmParser/Makefile
new file mode 100644
index 000000000000..41eadd430433
--- /dev/null
+++ b/unittests/AsmParser/Makefile
@@ -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