[PATCH] D37331: [ELF] Prevent crash with binary inputs with non-ascii file names

James Henderson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 31 05:56:44 PDT 2017


jhenderson created this revision.
Herald added a subscriber: emaste.

If using --format=binary with an input file name that has one or more non-ascii characters in, LLD has undefined behaviour (it crashes on my Windows Debug build) when calling isalnum with these non-ascii characters. The fix follows the recommended way of passing char values into isalnum by first casting them to unsigned chars (see the notes on http://en.cppreference.com/w/cpp/string/byte/isalnum).


https://reviews.llvm.org/D37331

Files:
  ELF/InputFiles.cpp
  test/ELF/format-binary-non-ascii.s


Index: test/ELF/format-binary-non-ascii.s
===================================================================
--- test/ELF/format-binary-non-ascii.s
+++ test/ELF/format-binary-non-ascii.s
@@ -0,0 +1,15 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t£.o
+
+# RUN: ld.lld -o %t.elf %t£.o --format=binary %t£.o
+# RUN: llvm-readobj -symbols %t.elf | FileCheck %s
+
+# CHECK: Name: _binary_{{[a-zA-Z0-9_]+}}test_ELF_Output_format_binary_non_ascii_s_tmp___o_start
+# CHECK: Name: _binary_{{[a-zA-Z0-9_]+}}test_ELF_Output_format_binary_non_ascii_s_tmp___o_end
+# CHECK: Name: _binary_{{[a-zA-Z0-9_]+}}test_ELF_Output_format_binary_non_ascii_s_tmp___o_size
+
+.text
+.align 4
+.globl _start
+_start:
+    nop
Index: ELF/InputFiles.cpp
===================================================================
--- ELF/InputFiles.cpp
+++ ELF/InputFiles.cpp
@@ -931,7 +931,7 @@
   // characters in a filename are replaced with underscore.
   std::string S = "_binary_" + MB.getBufferIdentifier().str();
   for (size_t I = 0; I < S.size(); ++I)
-    if (!isalnum(S[I]))
+    if (!isalnum(static_cast<unsigned char>(S[I])))
       S[I] = '_';
 
   Symtab->addRegular<ELFT>(Saver.save(S + "_start"), STV_DEFAULT, STT_OBJECT,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37331.113391.patch
Type: text/x-patch
Size: 1272 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170831/c5cab1d2/attachment.bin>


More information about the llvm-commits mailing list