[PATCH] D61059: [yaml2obj] - Don't crash on invalid inputs.
George Rimar via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 25 02:59:07 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rL359178: [yaml2obj] - Don't crash on invalid inputs. (authored by grimar, committed by ).
Herald added a subscriber: kristina.
Herald added a project: LLVM.
Changed prior to commit:
https://reviews.llvm.org/D61059?vs=196416&id=196596#toc
Repository:
rL LLVM
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D61059/new/
https://reviews.llvm.org/D61059
Files:
llvm/trunk/lib/ObjectYAML/ObjectYAML.cpp
llvm/trunk/lib/Support/YAMLTraits.cpp
llvm/trunk/test/tools/yaml2obj/empty-or-invalid-doc.yaml
llvm/trunk/test/tools/yaml2obj/empty.yaml
llvm/trunk/tools/yaml2obj/yaml2obj.cpp
Index: llvm/trunk/lib/ObjectYAML/ObjectYAML.cpp
===================================================================
--- llvm/trunk/lib/ObjectYAML/ObjectYAML.cpp
+++ llvm/trunk/lib/ObjectYAML/ObjectYAML.cpp
@@ -32,6 +32,7 @@
MappingTraits<MachOYAML::UniversalBinary>::mapping(IO,
*ObjectFile.FatMachO);
} else {
+ Input &In = (Input &)IO;
if (IO.mapTag("!ELF")) {
ObjectFile.Elf.reset(new ELFYAML::Object());
MappingTraits<ELFYAML::Object>::mapping(IO, *ObjectFile.Elf);
@@ -51,15 +52,12 @@
} else if (IO.mapTag("!WASM")) {
ObjectFile.Wasm.reset(new WasmYAML::Object());
MappingTraits<WasmYAML::Object>::mapping(IO, *ObjectFile.Wasm);
- } else {
- Input &In = (Input &)IO;
- std::string Tag = In.getCurrentNode()->getRawTag();
- if (Tag.empty())
+ } else if (const Node *N = In.getCurrentNode()) {
+ if (N->getRawTag().empty())
IO.setError("YAML Object File missing document type tag!");
else
- IO.setError(
- Twine("YAML Object File unsupported document type tag '") +
- Twine(Tag) + Twine("'!"));
+ IO.setError("YAML Object File unsupported document type tag '" +
+ N->getRawTag() + "'!");
}
}
}
Index: llvm/trunk/lib/Support/YAMLTraits.cpp
===================================================================
--- llvm/trunk/lib/Support/YAMLTraits.cpp
+++ llvm/trunk/lib/Support/YAMLTraits.cpp
@@ -113,6 +113,11 @@
}
bool Input::mapTag(StringRef Tag, bool Default) {
+ // CurrentNode can be null if setCurrentDocument() was unable to
+ // parse the document because it was invalid or empty.
+ if (!CurrentNode)
+ return false;
+
std::string foundTag = CurrentNode->_node->getVerbatimTag();
if (foundTag.empty()) {
// If no tag found and 'Tag' is the default, say it was found.
Index: llvm/trunk/tools/yaml2obj/yaml2obj.cpp
===================================================================
--- llvm/trunk/tools/yaml2obj/yaml2obj.cpp
+++ llvm/trunk/tools/yaml2obj/yaml2obj.cpp
@@ -86,11 +86,7 @@
if (!Buf)
return 1;
- StringRef Buffer = Buf.get()->getBuffer();
- if (Buffer.trim().size() == 0)
- error("yaml2obj: Error opening '" + Input + "': Empty File.");
- yaml::Input YIn(Buffer);
-
+ yaml::Input YIn(Buf.get()->getBuffer());
int Res = convertYAML(YIn, Out->os());
if (Res == 0)
Out->keep();
Index: llvm/trunk/test/tools/yaml2obj/empty-or-invalid-doc.yaml
===================================================================
--- llvm/trunk/test/tools/yaml2obj/empty-or-invalid-doc.yaml
+++ llvm/trunk/test/tools/yaml2obj/empty-or-invalid-doc.yaml
@@ -0,0 +1,8 @@
+# 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: Unknown document type!
+
+# RUN: echo -e -n "\xff" | not yaml2obj 2>&1 | FileCheck %s --check-prefix=INVALID
+# INVALID: yaml2obj: Failed to parse YAML file!
Index: llvm/trunk/test/tools/yaml2obj/empty.yaml
===================================================================
--- llvm/trunk/test/tools/yaml2obj/empty.yaml
+++ llvm/trunk/test/tools/yaml2obj/empty.yaml
@@ -1,5 +0,0 @@
-# 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61059.196596.patch
Type: text/x-patch
Size: 3605 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190425/fa7564c0/attachment.bin>
More information about the llvm-commits
mailing list