From 6d093fd78af3ce8b0d6abf533c4d83e2767c6c6a Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Tue, 11 Nov 2014 21:03:09 +0000 Subject: [PATCH] [llvm-mc] Fixing case where if a file ended with non-newline whitespace or a comma it would access invalid memory. Cleaned up parse loop. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221707 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/tools/llvm-mc/line_end_with_space.test | 2 ++ tools/llvm-mc/Disassembler.cpp | 24 ++++++++------------- 2 files changed, 11 insertions(+), 15 deletions(-) create mode 100644 test/tools/llvm-mc/line_end_with_space.test diff --git a/test/tools/llvm-mc/line_end_with_space.test b/test/tools/llvm-mc/line_end_with_space.test new file mode 100644 index 00000000000..2ce313990af --- /dev/null +++ b/test/tools/llvm-mc/line_end_with_space.test @@ -0,0 +1,2 @@ +RUN: llvm-mc -disassemble %s + \ No newline at end of file diff --git a/tools/llvm-mc/Disassembler.cpp b/tools/llvm-mc/Disassembler.cpp index 2c560ba4bc3..a3f9191d50e 100644 --- a/tools/llvm-mc/Disassembler.cpp +++ b/tools/llvm-mc/Disassembler.cpp @@ -81,29 +81,23 @@ static bool PrintInsts(const MCDisassembler &DisAsm, } static bool SkipToToken(StringRef &Str) { - while (!Str.empty() && Str.find_first_not_of(" \t\r\n#,") != 0) { + for (;;) { + if (Str.empty()) + return false; + // Strip horizontal whitespace and commas. - if (size_t Pos = Str.find_first_not_of(" \t\r,")) { + if (size_t Pos = Str.find_first_not_of(" \t\r\n,")) { Str = Str.substr(Pos); + continue; } - // If this is the end of a line or start of a comment, remove the rest of - // the line. - if (Str[0] == '\n' || Str[0] == '#') { - // Strip to the end of line if we already processed any bytes on this - // line. This strips the comment and/or the \n. - if (Str[0] == '\n') { - Str = Str.substr(1); - } else { + // If this is the start of a comment, remove the rest of the line. + if (Str[0] == '#') { Str = Str.substr(Str.find_first_of('\n')); - if (!Str.empty()) - Str = Str.substr(1); - } continue; } + return true; } - - return !Str.empty(); } -- 2.34.1