[llvm] r375404 - [obj2yaml] - Stop triggering UB when dumping corrupted strings.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 21 03:38:03 PDT 2019


Author: grimar
Date: Mon Oct 21 03:38:03 2019
New Revision: 375404

URL: http://llvm.org/viewvc/llvm-project?rev=375404&view=rev
Log:
[obj2yaml] - Stop triggering UB when dumping corrupted strings.

We have a following code to find quote type:

if (isspace(S.front()) || isspace(S.back()))
...

Problem is that:

"int isspace( int ch ): The behavior is undefined if the value of
ch is not representable as unsigned char and is not equal to EOF."
(https://en.cppreference.com/w/cpp/string/byte/isspace)

This patch shows how this UB can be triggered and fixes an issue.

Differential revision: https://reviews.llvm.org/D69160

Added:
    llvm/trunk/test/tools/obj2yaml/invalid-section-name.yaml
Modified:
    llvm/trunk/include/llvm/Support/YAMLTraits.h

Modified: llvm/trunk/include/llvm/Support/YAMLTraits.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/YAMLTraits.h?rev=375404&r1=375403&r2=375404&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/YAMLTraits.h (original)
+++ llvm/trunk/include/llvm/Support/YAMLTraits.h Mon Oct 21 03:38:03 2019
@@ -649,7 +649,8 @@ inline bool isBool(StringRef S) {
 inline QuotingType needsQuotes(StringRef S) {
   if (S.empty())
     return QuotingType::Single;
-  if (isspace(S.front()) || isspace(S.back()))
+  if (isspace(static_cast<unsigned char>(S.front())) ||
+      isspace(static_cast<unsigned char>(S.back())))
     return QuotingType::Single;
   if (isNull(S))
     return QuotingType::Single;

Added: llvm/trunk/test/tools/obj2yaml/invalid-section-name.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/obj2yaml/invalid-section-name.yaml?rev=375404&view=auto
==============================================================================
--- llvm/trunk/test/tools/obj2yaml/invalid-section-name.yaml (added)
+++ llvm/trunk/test/tools/obj2yaml/invalid-section-name.yaml Mon Oct 21 03:38:03 2019
@@ -0,0 +1,31 @@
+## Check we do not crash/assert when dumping a broken section name.
+## Here we replace "foo" name with a sequence of characters that
+## do are not representable as unsigned char.
+## We used to have an assert for this case before. 
+
+# RUN: yaml2obj %s -o %t
+# RUN: obj2yaml %t | FileCheck %s
+
+# CHECK:      --- !ELF
+# CHECK-NEXT: FileHeader:
+# CHECK-NEXT:   Class:   ELFCLASS64
+# CHECK-NEXT:   Data:    ELFDATA2LSB
+# CHECK-NEXT:   Type:    ET_REL
+# CHECK-NEXT:   Machine: EM_X86_64
+# CHECK-NEXT: Sections:
+# CHECK-NEXT:   - Name: "{{.*}}"
+# CHECK-NEXT:     Type: SHT_PROGBITS
+# CHECK-NEXT: ...
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+  - Name: foo
+    Type: SHT_PROGBITS
+  - Name: .shstrtab
+    Type: SHT_STRTAB
+    Content: "00FEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFE00"




More information about the llvm-commits mailing list