diff options
author | George Rimar <grimar@accesssoftek.com> | 2019-04-25 09:59:55 +0000 |
---|---|---|
committer | George Rimar <grimar@accesssoftek.com> | 2019-04-25 09:59:55 +0000 |
commit | 45d042ed961a04b641eac99584b045de27388708 (patch) | |
tree | 670437cf48e6a91a3efadbd6b07681738b60ec9d | |
parent | 4b7d3c48317c064e6a7048f7eee14677f1fddcc5 (diff) | |
download | bcm5719-llvm-45d042ed961a04b641eac99584b045de27388708.tar.gz bcm5719-llvm-45d042ed961a04b641eac99584b045de27388708.zip |
[yaml2obj] - Don't crash on invalid inputs.
yaml2obj might crash on invalid input when unable to parse the YAML.
Recently a crash with a very similar nature was fixed for an empty files.
This patch revisits the fix and does it in yaml::Input instead.
It seems to be more correct way to handle such situation.
With that crash for invalid inputs is also fixed now.
Differential revision: https://reviews.llvm.org/D61059
llvm-svn: 359178
-rw-r--r-- | llvm/lib/ObjectYAML/ObjectYAML.cpp | 12 | ||||
-rw-r--r-- | llvm/lib/Support/YAMLTraits.cpp | 5 | ||||
-rw-r--r-- | llvm/test/tools/yaml2obj/empty-or-invalid-doc.yaml (renamed from llvm/test/tools/yaml2obj/empty.yaml) | 5 | ||||
-rw-r--r-- | llvm/tools/yaml2obj/yaml2obj.cpp | 6 |
4 files changed, 15 insertions, 13 deletions
diff --git a/llvm/lib/ObjectYAML/ObjectYAML.cpp b/llvm/lib/ObjectYAML/ObjectYAML.cpp index daadde98365..7f636f4eaba 100644 --- a/llvm/lib/ObjectYAML/ObjectYAML.cpp +++ b/llvm/lib/ObjectYAML/ObjectYAML.cpp @@ -32,6 +32,7 @@ void MappingTraits<YamlObjectFile>::mapping(IO &IO, 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 @@ void MappingTraits<YamlObjectFile>::mapping(IO &IO, } 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() + "'!"); } } } diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp index e7932fe4239..183da5b3900 100644 --- a/llvm/lib/Support/YAMLTraits.cpp +++ b/llvm/lib/Support/YAMLTraits.cpp @@ -113,6 +113,11 @@ const Node *Input::getCurrentNode() const { } 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. diff --git a/llvm/test/tools/yaml2obj/empty.yaml b/llvm/test/tools/yaml2obj/empty-or-invalid-doc.yaml index 2debd187df2..450f2f7f380 100644 --- a/llvm/test/tools/yaml2obj/empty.yaml +++ b/llvm/test/tools/yaml2obj/empty-or-invalid-doc.yaml @@ -2,4 +2,7 @@ # 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. +# 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! diff --git a/llvm/tools/yaml2obj/yaml2obj.cpp b/llvm/tools/yaml2obj/yaml2obj.cpp index ef35458a8f0..f2266401da7 100644 --- a/llvm/tools/yaml2obj/yaml2obj.cpp +++ b/llvm/tools/yaml2obj/yaml2obj.cpp @@ -86,11 +86,7 @@ int main(int argc, char **argv) { 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(); |