[llvm] 0766981 - [llvm-objcopy] Fix crash for binary input files with non-ascii names

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 5 01:03:24 PST 2021


Author: James Henderson
Date: 2021-03-05T08:57:40Z
New Revision: 076698154ad7cd36c590379ed09fdd14047b8244

URL: https://github.com/llvm/llvm-project/commit/076698154ad7cd36c590379ed09fdd14047b8244
DIFF: https://github.com/llvm/llvm-project/commit/076698154ad7cd36c590379ed09fdd14047b8244.diff

LOG: [llvm-objcopy] Fix crash for binary input files with non-ascii names

The code was using the standard isalnum function which doesn't handle
values outside the non-ascii range. Switching to using llvm::isAlnum
instead ensures we don't provoke undefined behaviour, which can in some
cases result in crashes.

Reviewed by: MaskRay

Differential Revision: https://reviews.llvm.org/D97663

Added: 
    

Modified: 
    llvm/test/tools/llvm-objcopy/ELF/binary-input.test
    llvm/tools/llvm-objcopy/ELF/Object.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-objcopy/ELF/binary-input.test b/llvm/test/tools/llvm-objcopy/ELF/binary-input.test
index f232296ded82..a924a726d182 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/binary-input.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/binary-input.test
@@ -118,3 +118,10 @@
 # ALIGN:      Name: .data
 # ALIGN:      AddressAlignment:
 # ALIGN-SAME:                   8{{$}}
+
+## Show that a filename with non-ASCII characters can be handled appropriately.
+## The exact encoding of the non-ASCII character will determine what characters
+## are used, so don't check for them specifically.
+# RUN: cp %t.x-txt %t€.x-txt
+# RUN: llvm-objcopy -I binary -O elf64-x86-64 %t€.x-txt %t3.o
+# RUN: llvm-readobj --sections --symbols %t3.o | FileCheck %s

diff  --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp
index fe68c510c91a..7fa9621e40e3 100644
--- a/llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -1290,8 +1290,9 @@ void BinaryELFBuilder::addData(SymbolTableSection *SymTab) {
   DataSection.Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
 
   std::string SanitizedFilename = MemBuf->getBufferIdentifier().str();
-  std::replace_if(std::begin(SanitizedFilename), std::end(SanitizedFilename),
-                  [](char C) { return !isalnum(C); }, '_');
+  std::replace_if(
+      std::begin(SanitizedFilename), std::end(SanitizedFilename),
+      [](char C) { return !isAlnum(C); }, '_');
   Twine Prefix = Twine("_binary_") + SanitizedFilename;
 
   SymTab->addSymbol(Prefix + "_start", STB_GLOBAL, STT_NOTYPE, &DataSection,


        


More information about the llvm-commits mailing list