Skip to content

Commit db9322b

Browse files
committed
[LLDB][NFC] Reliability fixes for ObjectFileMachO.cpp
Static code inspection guided fixes for the following issues: - dead code - buffer not null-terminated - null-dereference - out-of-bounds access Differential Revision: https://reviews.llvm.org/D131554
1 parent 3510082 commit db9322b

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp

+23-8
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,13 @@ class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm {
533533
case GPRRegSet:
534534
// On ARM, the CPSR register is also included in the count but it is
535535
// not included in gpr.r so loop until (count-1).
536-
for (uint32_t i = 0; i < (count - 1); ++i) {
537-
gpr.r[i] = data.GetU32(&offset);
536+
537+
// Prevent static analysis warnings by explicitly contstraining 'count'
538+
// to acceptable range. Handle possible underflow of count-1
539+
if (count > 0 && count <= sizeof(gpr.r) / sizeof(gpr.r[0])) {
540+
for (uint32_t i = 0; i < (count - 1); ++i) {
541+
gpr.r[i] = data.GetU32(&offset);
542+
}
538543
}
539544
// Save cpsr explicitly.
540545
gpr.cpsr = data.GetU32(&offset);
@@ -544,7 +549,7 @@ class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm {
544549
break;
545550

546551
case FPURegSet: {
547-
uint8_t *fpu_reg_buf = (uint8_t *)&fpu.floats.s[0];
552+
uint8_t *fpu_reg_buf = (uint8_t *)&fpu.floats;
548553
const int fpu_reg_buf_size = sizeof(fpu.floats);
549554
if (data.ExtractBytes(offset, fpu_reg_buf_size, eByteOrderLittle,
550555
fpu_reg_buf) == fpu_reg_buf_size) {
@@ -4116,8 +4121,9 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
41164121
sym[sym_idx].SetReExportedSymbolName(reexport_name);
41174122
set_value = false;
41184123
reexport_shlib_needs_fixup[sym_idx] = reexport_name;
4119-
indirect_symbol_names.insert(
4120-
ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
4124+
indirect_symbol_names.insert(ConstString(
4125+
symbol_name +
4126+
((symbol_name && (symbol_name[0] == '_')) ? 1 : 0)));
41214127
} else
41224128
type = eSymbolTypeUndefined;
41234129
} break;
@@ -6335,6 +6341,11 @@ static offset_t CreateAllImageInfosPayload(
63356341
continue;
63366342
ConstString name = section->GetName();
63376343
segment_vmaddr seg_vmaddr;
6344+
// This is the uncommon case where strncpy is exactly
6345+
// the right one, doesn't need to be nul terminated.
6346+
// The segment name in a Mach-O LC_SEGMENT/LC_SEGMENT_64 is char[16] and
6347+
// is not guaranteed to be nul-terminated if all 16 characters are
6348+
// used.
63386349
strncpy(seg_vmaddr.segname, name.AsCString(),
63396350
sizeof(seg_vmaddr.segname));
63406351
seg_vmaddr.vmaddr = vmaddr;
@@ -6726,8 +6737,10 @@ bool ObjectFileMachO::SaveCore(const lldb::ProcessSP &process_sp,
67266737
buffer.PutHex32(sizeof(llvm::MachO::note_command));
67276738
char namebuf[16];
67286739
memset(namebuf, 0, sizeof(namebuf));
6729-
// this is the uncommon case where strncpy is exactly
6740+
// This is the uncommon case where strncpy is exactly
67306741
// the right one, doesn't need to be nul terminated.
6742+
// LC_NOTE name field is char[16] and is not guaranteed to be
6743+
// nul-terminated.
67316744
strncpy(namebuf, lcnote->name.c_str(), sizeof(namebuf));
67326745
buffer.PutRawBytes(namebuf, sizeof(namebuf));
67336746
buffer.PutHex64(lcnote->payload_file_offset);
@@ -6885,8 +6898,10 @@ ObjectFileMachO::GetCorefileAllImageInfos() {
68856898
}
68866899
uint32_t imgcount = m_data.GetU32(&offset);
68876900
uint64_t entries_fileoff = m_data.GetU64(&offset);
6888-
offset += 4; // uint32_t entries_size;
6889-
offset += 4; // uint32_t unused;
6901+
/* leaving the following dead code as comments for spec documentation
6902+
offset += 4; // uint32_t entries_size;
6903+
offset += 4; // uint32_t unused;
6904+
*/
68906905

68916906
offset = entries_fileoff;
68926907
for (uint32_t i = 0; i < imgcount; i++) {

0 commit comments

Comments
 (0)