Skip to content

Commit

Permalink
[LLD] [COFF] Use the new encodeSectionName() helper for long section …
Browse files Browse the repository at this point in the history
…names

The previous code used an unbounded sprintf, which in theory can
overflow, writing either the null terminator or the last digits
into the next struct member.

In practice, in LLD, all long section names are written sequentially
first at the start of the string table, followed by all the long
symbol names. Due to this, even if the total string table would
end up large, the long section names have fairly short offsets,
which is why this hasn't been an issue in practice.

I don't think it's worth trying to write a test that produces an
executable with enough long section names to make the section names
themselves exceed 10^6 bytes, which is currently necessary to trigger
faults with the previous form.

Differential Revision: https://reviews.llvm.org/D120676
  • Loading branch information
mstorsjo committed Mar 1, 2022
1 parent 7c77d41 commit 9dd2d50
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lld/COFF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ void OutputSection::writeHeaderTo(uint8_t *buf) {
*hdr = header;
if (stringTableOff) {
// If name is too long, write offset into the string table as a name.
sprintf(hdr->Name, "/%d", stringTableOff);
encodeSectionName(hdr->Name, stringTableOff);
} else {
assert(!config->debug || name.size() <= COFF::NameSize ||
(hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
Expand Down

0 comments on commit 9dd2d50

Please sign in to comment.