llvm.org GIT mirror llvm / 774be28
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359178 91177308-0d34-0410-b5e6-96231b3b80d8 George Rimar 5 months ago
5 changed file(s) with 19 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
3131 MappingTraits::mapping(IO,
3232 *ObjectFile.FatMachO);
3333 } else {
34 Input &In = (Input &)IO;
3435 if (IO.mapTag("!ELF")) {
3536 ObjectFile.Elf.reset(new ELFYAML::Object());
3637 MappingTraits::mapping(IO, *ObjectFile.Elf);
5051 } else if (IO.mapTag("!WASM")) {
5152 ObjectFile.Wasm.reset(new WasmYAML::Object());
5253 MappingTraits::mapping(IO, *ObjectFile.Wasm);
53 } else {
54 Input &In = (Input &)IO;
55 std::string Tag = In.getCurrentNode()->getRawTag();
56 if (Tag.empty())
54 } else if (const Node *N = In.getCurrentNode()) {
55 if (N->getRawTag().empty())
5756 IO.setError("YAML Object File missing document type tag!");
5857 else
59 IO.setError(
60 Twine("YAML Object File unsupported document type tag '") +
61 Twine(Tag) + Twine("'!"));
58 IO.setError("YAML Object File unsupported document type tag '" +
59 N->getRawTag() + "'!");
6260 }
6361 }
6462 }
112112 }
113113
114114 bool Input::mapTag(StringRef Tag, bool Default) {
115 // CurrentNode can be null if setCurrentDocument() was unable to
116 // parse the document because it was invalid or empty.
117 if (!CurrentNode)
118 return false;
119
115120 std::string foundTag = CurrentNode->_node->getVerbatimTag();
116121 if (foundTag.empty()) {
117122 // If no tag found and 'Tag' is the default, say it was found.
0 # RUN: echo "" | not yaml2obj 2>&1 | FileCheck %s
1 # RUN: echo -n "" | not yaml2obj 2>&1 | FileCheck %s
2 # RUN: echo " " | not yaml2obj 2>&1 | FileCheck %s
3 # RUN: echo " " | not yaml2obj 2>&1 | FileCheck %s
4 # CHECK: yaml2obj: Unknown document type!
5
6 # RUN: echo -e -n "\xff" | not yaml2obj 2>&1 | FileCheck %s --check-prefix=INVALID
7 # INVALID: yaml2obj: Failed to parse YAML file!
+0
-5
test/tools/yaml2obj/empty.yaml less more
None # RUN: echo "" | not yaml2obj 2>&1 | FileCheck %s
1 # RUN: echo -n "" | not yaml2obj 2>&1 | FileCheck %s
2 # RUN: echo " " | not yaml2obj 2>&1 | FileCheck %s
3 # RUN: echo " " | not yaml2obj 2>&1 | FileCheck %s
4 # CHECK: yaml2obj: Error opening '-': Empty File.
8585 if (!Buf)
8686 return 1;
8787
88 StringRef Buffer = Buf.get()->getBuffer();
89 if (Buffer.trim().size() == 0)
90 error("yaml2obj: Error opening '" + Input + "': Empty File.");
91 yaml::Input YIn(Buffer);
92
88 yaml::Input YIn(Buf.get()->getBuffer());
9389 int Res = convertYAML(YIn, Out->os());
9490 if (Res == 0)
9591 Out->keep();