[llvm-branch-commits] [lld] dc94773 - [ELF] Don't cause assertion failure if --dynamic-list or --version-script takes an empty file

Fangrui Song via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Jun 16 22:32:53 PDT 2020


Author: Fangrui Song
Date: 2020-06-16T22:31:33-07:00
New Revision: dc94773a91c85a05f4f249153cb1e9522b3beb5e

URL: https://github.com/llvm/llvm-project/commit/dc94773a91c85a05f4f249153cb1e9522b3beb5e
DIFF: https://github.com/llvm/llvm-project/commit/dc94773a91c85a05f4f249153cb1e9522b3beb5e.diff

LOG: [ELF] Don't cause assertion failure if --dynamic-list or --version-script takes an empty file

Fixes PR46184
Report line 1 of the last memory buffer.

(cherry picked from commit ac6abc99e2794e4674a8498f817fda19b176bbfe)

Added: 
    

Modified: 
    lld/ELF/ScriptLexer.cpp
    lld/test/ELF/invalid-dynamic-list.test
    lld/test/ELF/version-script-err.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/ScriptLexer.cpp b/lld/ELF/ScriptLexer.cpp
index e0ff56fec3f3..1fed3d06227e 100644
--- a/lld/ELF/ScriptLexer.cpp
+++ b/lld/ELF/ScriptLexer.cpp
@@ -52,6 +52,8 @@ StringRef ScriptLexer::getLine() {
 
 // Returns 1-based line number of the current token.
 size_t ScriptLexer::getLineNumber() {
+  if (pos == 0)
+    return 1;
   StringRef s = getCurrentMB().getBuffer();
   StringRef tok = tokens[pos - 1];
   return s.substr(0, tok.data() - s.data()).count('\n') + 1;
@@ -292,7 +294,9 @@ static bool encloses(StringRef s, StringRef t) {
 
 MemoryBufferRef ScriptLexer::getCurrentMB() {
   // Find input buffer containing the current token.
-  assert(!mbs.empty() && pos > 0);
+  assert(!mbs.empty());
+  if (pos == 0)
+    return mbs.back();
   for (MemoryBufferRef mb : mbs)
     if (encloses(mb.getBuffer(), tokens[pos - 1]))
       return mb;

diff  --git a/lld/test/ELF/invalid-dynamic-list.test b/lld/test/ELF/invalid-dynamic-list.test
index f560ceed0f8b..3a2f9de6f5ce 100644
--- a/lld/test/ELF/invalid-dynamic-list.test
+++ b/lld/test/ELF/invalid-dynamic-list.test
@@ -9,6 +9,10 @@
 
 # RUN: mkdir -p %t.dir
 
+# RUN: echo > %tempty.list
+# RUN: not ld.lld --dynamic-list %tempty.list 2>&1 | FileCheck --check-prefix=EMPTY %s
+# EMPTY: error: {{.*}}.list:1: unexpected EOF
+
 # RUN: echo foobar > %t1
 # RUN: not ld.lld --dynamic-list %t1 2>&1 | FileCheck -check-prefix=ERR1 %s
 # ERR1: {{.*}}:1: { expected, but got foobar

diff  --git a/lld/test/ELF/version-script-err.s b/lld/test/ELF/version-script-err.s
index bd786d913369..b7039444cd97 100644
--- a/lld/test/ELF/version-script-err.s
+++ b/lld/test/ELF/version-script-err.s
@@ -8,3 +8,8 @@
 // RUN: not ld.lld --version-script %terr1.script -shared %t.o -o %t.so 2>&1 | \
 // RUN:   FileCheck -check-prefix=ERR1 %s
 // ERR1: {{.*}}:1: unclosed quote
+
+// RUN: echo > %tempty.ver
+// RUN: not ld.lld --version-script %tempty.ver 2>&1 | \
+// RUN:   FileCheck --check-prefix=ERR2 %s
+// ERR2: error: {{.*}}.ver:1: unexpected EOF


        


More information about the llvm-branch-commits mailing list