Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix for CompPhdr::operator() that could cause sort to fail. #139

Merged
merged 2 commits into from
Jan 8, 2018
Merged
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ class ElfFile
ElfFile * elfFile;
bool operator ()(const Elf_Phdr & x, const Elf_Phdr & y)
{
if (x.p_type == PT_PHDR) return true;
if (x.p_type == PT_PHDR) {
if (y.p_type == PT_PHDR) return false;
return true;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified a bit (mostly removing the duplicated check of y.p_type == PT_PHDR:

// Nothing is less than a PHDR:
if (y.p_type == PT_PHDR) return false;

// PHDR's are less than everything (that's not a PHDR)
if (x.p_type == PT_PHDR) return true;

// Sort non-PHDR's by address:
...

Comments are just suggestions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it does seem worth fixing this, according to documents such as [1][2] it seems that only a single PT_PHDR entry may legally exist in a file.

Thinking about what PT_PHDR actually means, this makes sense :). Also my comments are indeed a bit silly, I didn't have my "ELF-knowledge hat" on.

So this is probably harmless as-is: for any legal set of segments this operator will define a valid (strict weak) ordering.

Might be worth ensuring we reject files that attempt to have multiple PT_PHDR segments?

[1] http://www.sco.com/developers/gabi/latest/ch5.pheader.html
[2] http://www.skyfree.org/linux/references/ELF_Format.pdf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simplified the code now. Thanks.

Yes, this was found using a debug version of std::sort, not by obviously incorrect results. Since there's only a single PT_PHDR entry, It probably doesn't any difference on any sane implementation of std::sort, but it is undefined behavior, so there's no guarantees about that.

if (y.p_type == PT_PHDR) return false;
return elfFile->rdi(x.p_paddr) < elfFile->rdi(y.p_paddr);
}
Expand Down