llvm.org GIT mirror llvm / 3f1b2bd
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 Alex Lorenz 5 years ago
6 changed file(s) with 76 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
2323
2424 bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err) {
2525 SourceMgr SM;
26 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(F, false);
26 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(F);
2727 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
2828
2929 return LLParser(F.getBuffer(), SM, Err, &M).Run();
0 //===- llvm/unittest/AsmParser/AsmParserTest.cpp - asm parser unittests ---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/StringRef.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/Support/SourceMgr.h"
14 #include "gtest/gtest.h"
15
16 using namespace llvm;
17
18 namespace {
19
20 TEST(AsmParserTest, NullTerminatedInput) {
21 LLVMContext &Ctx = getGlobalContext();
22 StringRef Source = "; Empty module \n";
23 SMDiagnostic Error;
24 auto Mod = parseAssemblyString(Source, Error, Ctx);
25
26 EXPECT_TRUE(Mod != nullptr);
27 EXPECT_TRUE(Error.getMessage().empty());
28 }
29
30 #ifdef GTEST_HAS_DEATH_TEST
31 #ifndef NDEBUG
32
33 TEST(AsmParserTest, NonNullTerminatedInput) {
34 LLVMContext &Ctx = getGlobalContext();
35 StringRef Source = "; Empty module \n\1\2";
36 SMDiagnostic Error;
37 std::unique_ptr Mod;
38 EXPECT_DEATH(Mod = parseAssemblyString(Source.substr(0, Source.size() - 2),
39 Error, Ctx),
40 "Buffer is not null terminated!");
41 }
42
43 #endif
44 #endif
45
46 } // end anonymous namespace
0 set(LLVM_LINK_COMPONENTS
1 AsmParser
2 Core
3 Support
4 )
5
6 add_llvm_unittest(AsmParserTests
7 AsmParserTest.cpp
8 )
0 ##===- unittests/AsmParser/Makefile ------------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL = ../..
10 TESTNAME = AsmParser
11 LINK_COMPONENTS := AsmParser Core Support
12
13 include $(LEVEL)/Makefile.config
14 include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest
1212
1313 add_subdirectory(ADT)
1414 add_subdirectory(Analysis)
15 add_subdirectory(AsmParser)
1516 add_subdirectory(Bitcode)
1617 add_subdirectory(CodeGen)
1718 add_subdirectory(DebugInfo)
88
99 LEVEL = ..
1010
11 PARALLEL_DIRS = ADT Analysis Bitcode CodeGen DebugInfo ExecutionEngine IR \
12 LineEditor Linker MC Option ProfileData Support Transforms
11 PARALLEL_DIRS = ADT Analysis AsmParser Bitcode CodeGen DebugInfo \
12 ExecutionEngine IR LineEditor Linker MC Option ProfileData \
13 Support Transforms
1314
1415 include $(LEVEL)/Makefile.config
1516 include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest