summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Kratochvil <jan.kratochvil@redhat.com>2019-02-06 08:44:13 +0000
committerJan Kratochvil <jan.kratochvil@redhat.com>2019-02-06 08:44:13 +0000
commitbb3609e49d806904c80a6a6047e83ed5a65b0f24 (patch)
treeb8b9ede9fbb24c1a546cbfa8ce72a858a2392fad
parentb8ee8c8517b67be8f7837b7773a9325222aff5b0 (diff)
downloadbcm5719-llvm-bb3609e49d806904c80a6a6047e83ed5a65b0f24.tar.gz
bcm5719-llvm-bb3609e49d806904c80a6a6047e83ed5a65b0f24.zip
Fix strlen() of unbound array undefined behavior
LLDB testsuite fails when built by GCC8 on: LLDB :: SymbolFile/DWARF/find-basic-namespace.cpp This is because this code in LLDB codebase has undefined behavior: #include <algorithm> #include <string.h> // lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp:1731 static struct section_64 { char sectname[16]; char segname[16]; } sect64 = { {'_','_','a','p','p','l','e','_','n','a','m','e','s','p','a','c'}, "__DWARF" }; int main() { return std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)); } It has been discussed as a (false) bugreport to GCC: wrong-code: LLDB testcase fails: SymbolFile/DWARF/find-basic-namespace.cpp https://bugzilla.redhat.com/show_bug.cgi?id=1672436 Differential Revision: https://reviews.llvm.org/D57781 llvm-svn: 353280
-rw-r--r--lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp6
-rw-r--r--lldb/source/Utility/ConstString.cpp2
2 files changed, 3 insertions, 5 deletions
diff --git a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
index afe3777c458..b4c320f12fe 100644
--- a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
+++ b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
@@ -1609,8 +1609,7 @@ void ObjectFileMachO::ProcessSegmentCommand(const load_command &load_cmd_,
bool add_section = true;
bool add_to_unified = true;
ConstString const_segname(
- load_cmd.segname,
- std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
+ load_cmd.segname, strnlen(load_cmd.segname, sizeof(load_cmd.segname)));
SectionSP unified_section_sp(
context.UnifiedList.FindSectionByName(const_segname));
@@ -1729,8 +1728,7 @@ void ObjectFileMachO::ProcessSegmentCommand(const load_command &load_cmd_,
if (add_section) {
ConstString section_name(
- sect64.sectname,
- std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
+ sect64.sectname, strnlen(sect64.sectname, sizeof(sect64.sectname)));
if (!const_segname) {
// We have a segment with no name so we need to conjure up segments
// that correspond to the section's segname if there isn't already such
diff --git a/lldb/source/Utility/ConstString.cpp b/lldb/source/Utility/ConstString.cpp
index c12e930303d..7ab98ef5ce7 100644
--- a/lldb/source/Utility/ConstString.cpp
+++ b/lldb/source/Utility/ConstString.cpp
@@ -143,7 +143,7 @@ public:
const char *GetConstTrimmedCStringWithLength(const char *cstr,
size_t cstr_len) {
if (cstr != nullptr) {
- const size_t trimmed_len = std::min<size_t>(strlen(cstr), cstr_len);
+ const size_t trimmed_len = strnlen(cstr, cstr_len);
return GetConstCStringWithLength(cstr, trimmed_len);
}
return nullptr;
OpenPOWER on IntegriCloud