]> granicus.if.org Git - llvm/commitdiff
[yaml2obj] Fixing opening empty yaml files.
authorPuyan Lotfi <puyan@puyan.org>
Thu, 28 Mar 2019 22:55:08 +0000 (22:55 +0000)
committerPuyan Lotfi <puyan@puyan.org>
Thu, 28 Mar 2019 22:55:08 +0000 (22:55 +0000)
Essentially echo "" | yaml2obj crashes. This patch attempts to trim whitespace
and determine if the yaml string in the file is empty or not. If the input is
empty then it will not properly print out an error message and return an error
code.

Differential Revision: https://reviews.llvm.org/D59964

A    test/tools/yaml2obj/empty.yaml
M    tools/yaml2obj/yaml2obj.cpp

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357219 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/yaml2obj/empty.yaml [new file with mode: 0644]
tools/yaml2obj/yaml2obj.cpp

diff --git a/test/tools/yaml2obj/empty.yaml b/test/tools/yaml2obj/empty.yaml
new file mode 100644 (file)
index 0000000..2debd18
--- /dev/null
@@ -0,0 +1,5 @@
+# RUN: echo "" | not yaml2obj 2>&1 | FileCheck %s
+# RUN: echo -n "" | not yaml2obj 2>&1 | FileCheck %s
+# RUN: echo " " | not yaml2obj 2>&1 | FileCheck %s
+# RUN: echo "  " | not yaml2obj 2>&1 | FileCheck %s
+# CHECK: yaml2obj: Error opening '-': Empty File.
index 58e69fd5a5b6400c715b52cddef15410968433c9..ef35458a8f004ae001d81eb9f6e314bfd63c8396 100644 (file)
@@ -86,7 +86,10 @@ int main(int argc, char **argv) {
   if (!Buf)
     return 1;
 
-  yaml::Input YIn(Buf.get()->getBuffer());
+  StringRef Buffer = Buf.get()->getBuffer();
+  if (Buffer.trim().size() == 0)
+    error("yaml2obj: Error opening '" + Input + "': Empty File.");
+  yaml::Input YIn(Buffer);
 
   int Res = convertYAML(YIn, Out->os());
   if (Res == 0)