[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
Wed Sep 6 06:32:58 PDT 2017


jhenderson updated this revision to Diff 114001.
jhenderson edited the summary of this revision.
jhenderson added a comment.

I discovered an existing "isAlnum" function in LLD's Strings.cpp, so I have exposed this and used it instead. It does have a slight difference in behaviour to std::isalnum, because it returns true for underscore characters, but that is harmless in our case. I might consider renaming the function to make it clear it includes the underscore - maybe to isValidCIdentifierChar. What do you think?


https://reviews.llvm.org/D37331

Files:
  ELF/InputFiles.cpp
  ELF/Strings.cpp
  ELF/Strings.h
  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/Strings.h
===================================================================
--- ELF/Strings.h
+++ ELF/Strings.h
@@ -22,6 +22,7 @@
 namespace elf {
 
 std::vector<uint8_t> parseHex(StringRef S);
+bool isAlnum(char C);
 bool isValidCIdentifier(StringRef S);
 
 // This is a lazy version of StringRef. String size is computed lazily
Index: ELF/Strings.cpp
===================================================================
--- ELF/Strings.cpp
+++ ELF/Strings.cpp
@@ -58,7 +58,9 @@
   return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z') || C == '_';
 }
 
-static bool isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
+// Returns true if C is a valid letter, digit or underscore as defined in the
+// "C" locale.
+bool elf::isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
 
 // Returns true if S is valid as a C language identifier.
 bool elf::isValidCIdentifier(StringRef S) {
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 (!elf::isAlnum(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.114001.patch
Type: text/x-patch
Size: 2190 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170906/290f95c3/attachment-0001.bin>


More information about the llvm-commits mailing list