Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion llvm/include/llvm/Support/YAMLTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ class IO {
virtual NodeKind getNodeKind() = 0;

virtual void setError(const Twine &) = 0;
virtual std::error_code error() = 0;
virtual void setAllowUnknownKeys(bool Allow);

template <typename T>
Expand Down Expand Up @@ -1448,7 +1449,7 @@ class Input : public IO {
~Input() override;

// Check if there was an syntax or semantic error during parsing.
std::error_code error();
std::error_code error() override;

private:
bool outputting() const override;
Expand Down Expand Up @@ -1631,6 +1632,7 @@ class Output : public IO {
void scalarTag(std::string &) override;
NodeKind getNodeKind() override;
void setError(const Twine &message) override;
std::error_code error() override;
bool canElideEmptySequence() override;

// These are only used by operator<<. They could be private
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/ObjectYAML/ELFYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,9 @@ void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
std::string MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::validate(
IO &io, std::unique_ptr<ELFYAML::Chunk> &C) {
if (const auto *F = dyn_cast<ELFYAML::Fill>(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 "";
}
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/ObjectYAML/MachOYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@ void MappingTraits<MachOYAML::Section>::mapping(IO &IO,
std::string
MappingTraits<MachOYAML::Section>::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 "";
}
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Support/YAMLTraits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@ void Output::scalarTag(std::string &Tag) {
void Output::setError(const Twine &message) {
}

std::error_code Output::error() { return {}; }

bool Output::canElideEmptySequence() {
// Normally, with an optional key/value where the value is an empty sequence,
// the whole key/value can be not written. But, that produces wrong yaml
Expand Down
41 changes: 41 additions & 0 deletions llvm/test/ObjectYAML/MachO/section_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions llvm/test/tools/yaml2obj/ELF/custom-fill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading