Skip to content

Commit 4ec0b08

Browse files
author
George Rimar
committedOct 21, 2019
[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 llvm-svn: 375404
1 parent d6e6aa8 commit 4ec0b08

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
 

‎llvm/include/llvm/Support/YAMLTraits.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,8 @@ inline bool isBool(StringRef S) {
649649
inline QuotingType needsQuotes(StringRef S) {
650650
if (S.empty())
651651
return QuotingType::Single;
652-
if (isspace(S.front()) || isspace(S.back()))
652+
if (isspace(static_cast<unsigned char>(S.front())) ||
653+
isspace(static_cast<unsigned char>(S.back())))
653654
return QuotingType::Single;
654655
if (isNull(S))
655656
return QuotingType::Single;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Check we do not crash/assert when dumping a broken section name.
2+
## Here we replace "foo" name with a sequence of characters that
3+
## do are not representable as unsigned char.
4+
## We used to have an assert for this case before.
5+
6+
# RUN: yaml2obj %s -o %t
7+
# RUN: obj2yaml %t | FileCheck %s
8+
9+
# CHECK: --- !ELF
10+
# CHECK-NEXT: FileHeader:
11+
# CHECK-NEXT: Class: ELFCLASS64
12+
# CHECK-NEXT: Data: ELFDATA2LSB
13+
# CHECK-NEXT: Type: ET_REL
14+
# CHECK-NEXT: Machine: EM_X86_64
15+
# CHECK-NEXT: Sections:
16+
# CHECK-NEXT: - Name: "{{.*}}"
17+
# CHECK-NEXT: Type: SHT_PROGBITS
18+
# CHECK-NEXT: ...
19+
20+
--- !ELF
21+
FileHeader:
22+
Class: ELFCLASS64
23+
Data: ELFDATA2LSB
24+
Type: ET_REL
25+
Machine: EM_X86_64
26+
Sections:
27+
- Name: foo
28+
Type: SHT_PROGBITS
29+
- Name: .shstrtab
30+
Type: SHT_STRTAB
31+
Content: "00FEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFE00"

0 commit comments

Comments
 (0)
Please sign in to comment.