diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp index 7e94d01a97153..24f426a9aa1f7 100644 --- a/llvm/lib/ObjectYAML/ELFYAML.cpp +++ b/llvm/lib/ObjectYAML/ELFYAML.cpp @@ -1747,7 +1747,9 @@ void MappingTraits>::mapping( std::string MappingTraits>::validate( IO &io, std::unique_ptr &C) { if (const auto *F = dyn_cast(C.get())) { - if (F->Pattern && F->Pattern->binary_size() != 0 && !F->Size) + // Can't check the `Size`, as it's required and may be left uninitialized by + // previous error. + if (!io.error() && F->Pattern && F->Pattern->binary_size() != 0 && !F->Size) return "\"Size\" can't be 0 when \"Pattern\" is not empty"; return ""; } diff --git a/llvm/lib/ObjectYAML/MachOYAML.cpp b/llvm/lib/ObjectYAML/MachOYAML.cpp index 4857b5911ff2e..b7eda97c22ae0 100644 --- a/llvm/lib/ObjectYAML/MachOYAML.cpp +++ b/llvm/lib/ObjectYAML/MachOYAML.cpp @@ -346,7 +346,10 @@ void MappingTraits::mapping(IO &IO, std::string MappingTraits::validate(IO &IO, MachOYAML::Section &Section) { - if (Section.content && Section.size < Section.content->binary_size()) + // Can't check the `size`, as it's required and may be left uninitialized by + // previous error. + if (!IO.error() && Section.content && + Section.size < Section.content->binary_size()) return "Section size must be greater than or equal to the content size"; return ""; } diff --git a/llvm/test/ObjectYAML/MachO/section_data.yaml b/llvm/test/ObjectYAML/MachO/section_data.yaml index 87c5bc803ee1a..a2d9a3b7e1675 100644 --- a/llvm/test/ObjectYAML/MachO/section_data.yaml +++ b/llvm/test/ObjectYAML/MachO/section_data.yaml @@ -159,3 +159,44 @@ LoadCommands: reserved2: 0x00000000 reserved3: 0x00000000 content: AA + +## Case 4: Don't validate if size is missing. +# RUN: not yaml2obj --docnum=4 %s -o %t1 2>&1 | FileCheck %s --check-prefix=CASE4 --implicit-check-not=error: +# CASE4: error: missing required key 'size' +# CASE4: error: failed to parse YAML + +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x01000007 + cpusubtype: 0x00000003 + filetype: 0x00000001 + ncmds: 1 + sizeofcmds: 232 + flags: 0x00002000 + reserved: 0x00000000 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 232 + segname: '' + vmaddr: 0 + vmsize: 4 + fileoff: 392 + filesize: 4 + maxprot: 7 + initprot: 7 + nsects: 1 + flags: 0 + Sections: + - sectname: __data + segname: __DATA + addr: 0x0000000000000000 + content: AA + offset: 0x00000188 + align: 2 + reloff: 0x00000000 + nreloc: 0 + flags: 0x00000000 + reserved1: 0x00000000 + reserved2: 0x00000000 + reserved3: 0x00000000 diff --git a/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml b/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml index d770fdb982532..cdb9a97889ac1 100644 --- a/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml +++ b/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml @@ -156,9 +156,10 @@ Sections: Pattern: "BB" ## Check that the "Size" field is mandatory. -# RUN: not yaml2obj --docnum=5 2>&1 %s | FileCheck %s --check-prefix=NOSIZE +# RUN: not yaml2obj --docnum=5 2>&1 %s | FileCheck %s --check-prefix=NOSIZE --implicit-check-not=error: -## NOSIZE: error: missing required key 'Size' +# NOSIZE: error: missing required key 'Size' +# NOSIZE: error: failed to parse YAML --- !ELF FileHeader: