Skip to content

Commit e2fc338

Browse files
committed
Address reviewer feedback.
- Refactor warning message - Add testcase for unknown load command
1 parent 356799b commit e2fc338

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

llvm/lib/ObjectYAML/MachOEmitter.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,18 @@ void MachOWriter::writeLoadCommands(raw_ostream &OS) {
300300
// specified test cases.
301301
// Prevent integer underflow if BytesWritten exceeds cmdsize.
302302
if (BytesWritten > LC.Data.load_command_data.cmdsize) {
303-
const char *name = getLoadCommandName(LC.Data.load_command_data.cmd);
304-
if (name)
305-
WithColor::warning()
306-
<< "load command " << i << " " << name << " cmdsize too small ("
307-
<< LC.Data.load_command_data.cmdsize << " bytes) for actual size ("
308-
<< BytesWritten << " bytes)\n";
303+
std::string Name;
304+
const char *NameCStr = getLoadCommandName(LC.Data.load_command_data.cmd);
305+
if (NameCStr)
306+
Name = NameCStr;
309307
else
310-
WithColor::warning()
311-
<< "load command " << i << " (0x"
312-
<< Twine::utohexstr(LC.Data.load_command_data.cmd)
313-
<< ") cmdsize too small (" << LC.Data.load_command_data.cmdsize
314-
<< " bytes) for actual size (" << BytesWritten << " bytes)\n";
308+
Name = "(0x" + Twine::utohexstr(LC.Data.load_command_data.cmd).str() + ")";
309+
310+
WithColor::warning() << "load command " << i << " " << Name
311+
<< " cmdsize too small ("
312+
<< LC.Data.load_command_data.cmdsize
313+
<< " bytes) for actual size (" << BytesWritten
314+
<< " bytes)\n";
315315
}
316316
auto BytesRemaining = (BytesWritten < LC.Data.load_command_data.cmdsize)
317317
? LC.Data.load_command_data.cmdsize - BytesWritten

llvm/test/ObjectYAML/MachO/load-cmdsize-too-small.yaml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
## Test that yaml2obj handles load commands with cmdsize smaller than the
22
## actual structure size without crashing (due to integer underflow).
33

4-
# RUN: yaml2obj %s -o %t 2>&1 | FileCheck %s --check-prefix=WARNING
5-
# RUN: not llvm-readobj --file-headers %t 2>&1 | FileCheck %s --check-prefix=MALFORMED
4+
# RUN: yaml2obj %s --docnum=1 -o %t1 2>&1 | FileCheck %s --check-prefix=WARNING-KNOWN
5+
# RUN: not llvm-readobj --file-headers %t1 2>&1 | FileCheck %s --check-prefix=MALFORMED
66

7-
# WARNING: warning: load command 0 LC_SEGMENT_64 cmdsize too small (56 bytes) for actual size (72 bytes)
7+
# RUN: yaml2obj %s --docnum=2 -o %t2 2>&1 | FileCheck %s --check-prefix=WARNING-UNKNOWN
8+
9+
# WARNING-KNOWN: warning: load command 0 LC_SEGMENT_64 cmdsize too small (56 bytes) for actual size (72 bytes)
810

911
# MALFORMED: error: {{.*}}: truncated or malformed object (load command 0 LC_SEGMENT_64 cmdsize too small)
1012

13+
# WARNING-UNKNOWN: warning: load command 0 (0xdeadbeef) cmdsize too small (8 bytes) for actual size (20 bytes)
14+
15+
## Test with a known load command (LC_SEGMENT_64)
1116
--- !mach-o
1217
FileHeader:
1318
magic: 0xFEEDFACF
@@ -31,3 +36,20 @@ LoadCommands:
3136
nsects: 0
3237
flags: 0
3338
...
39+
40+
## Test with an unknown load command value
41+
--- !mach-o
42+
FileHeader:
43+
magic: 0xFEEDFACF
44+
cputype: 0x01000007
45+
cpusubtype: 0x00000003
46+
filetype: 0x00000001
47+
ncmds: 1
48+
sizeofcmds: 20
49+
flags: 0x00002000
50+
reserved: 0x00000000
51+
LoadCommands:
52+
- cmd: 0xDEADBEEF
53+
cmdsize: 8
54+
PayloadBytes: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C]
55+
...

0 commit comments

Comments
 (0)