[lld] 9dd2d50 - [LLD] [COFF] Use the new encodeSectionName() helper for long section names

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 1 01:35:26 PST 2022


Author: Martin Storsjö
Date: 2022-03-01T11:33:02+02:00
New Revision: 9dd2d50984f5b02e19d3576812dc9b09ddcc9483

URL: https://github.com/llvm/llvm-project/commit/9dd2d50984f5b02e19d3576812dc9b09ddcc9483
DIFF: https://github.com/llvm/llvm-project/commit/9dd2d50984f5b02e19d3576812dc9b09ddcc9483.diff

LOG: [LLD] [COFF] Use the new encodeSectionName() helper for long section 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

Added: 
    

Modified: 
    lld/COFF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index f43853e32075a..eec7e9d497205 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -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);


        


More information about the llvm-commits mailing list