From 63af0cd7523b66327cda4318c8e607d5cf3ca39d Mon Sep 17 00:00:00 2001 From: Zequan Wu Date: Thu, 15 Dec 2022 12:27:22 +0800 Subject: [PATCH] Cleanup strncmp and use string_view in elf_reader.cc. Change-Id: I74c755f1ade80bb4313e4fd14e5dc6bab419a0a6 Reviewed-on: https://chromium-review.googlesource.com/c/breakpad/breakpad/+/4099505 Reviewed-by: Mark Mentovai --- src/common/dwarf/elf_reader.cc | 36 ++++++++++++++++++++++------------ src/common/dwarf/elf_reader.h | 4 +++- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/common/dwarf/elf_reader.cc b/src/common/dwarf/elf_reader.cc index 5fd1cfb9..7664377c 100644 --- a/src/common/dwarf/elf_reader.cc +++ b/src/common/dwarf/elf_reader.cc @@ -39,9 +39,9 @@ #include #include -#include #include #include +#include #include // TODO(saugustine): Add support for compressed debug. // Also need to add configure tests for zlib. @@ -107,6 +107,12 @@ const int kAARCH64PLT0Size = 0x20; // Suffix for PLT functions when it needs to be explicitly identified as such. const char kPLTFunctionSuffix[] = "@plt"; +// Replace callsites of this function to std::string_view::starts_with after +// adopting C++20. +bool StringViewStartsWith(std::string_view sv, std::string_view prefix) { + return sv.compare(0, prefix.size(), prefix) == 0; +} + } // namespace namespace google_breakpad { @@ -215,7 +221,7 @@ class ElfSectionReader { (header_.sh_offset - offset_aligned); // Check for and handle any compressed contents. - //if (name == ".zdebug_") + //if (StringViewStartsWith(name, ".zdebug_")) // DecompressZlibContents(); // TODO(saugustine): Add support for proposed elf-section flag // "SHF_COMPRESS". @@ -359,8 +365,8 @@ class ElfReaderImpl { // "opd_section_" must always be checked for NULL before use. opd_section_ = GetSectionInfoByName(".opd", &opd_info_); for (unsigned int k = 0u; k < GetNumSections(); ++k) { - const char* name = GetSectionName(section_headers_[k].sh_name); - if (strncmp(name, ".text", strlen(".text")) == 0) { + std::string_view name{GetSectionName(section_headers_[k].sh_name)}; + if (StringViewStartsWith(name, ".text")) { base_for_text_ = section_headers_[k].sh_addr - section_headers_[k].sh_offset; break; @@ -809,9 +815,11 @@ class ElfReaderImpl { // Debug sections are likely to be near the end, so reverse the // direction of iteration. for (int k = GetNumSections() - 1; k >= 0; --k) { - const char* name = GetSectionName(section_headers_[k].sh_name); - if (strncmp(name, ".debug", strlen(".debug")) == 0) return true; - if (strncmp(name, ".zdebug", strlen(".zdebug")) == 0) return true; + std::string_view name{GetSectionName(section_headers_[k].sh_name)}; + if (StringViewStartsWith(name, ".debug") || + StringViewStartsWith(name, ".zdebug")) { + return true; + } } return false; } @@ -1213,11 +1221,15 @@ const char* ElfReader::GetSectionInfoByName(const string& section_name, } } -bool ElfReader::SectionNamesMatch(const string& name, const string& sh_name) { - if ((name.find(".debug_", 0) == 0) && (sh_name.find(".zdebug_", 0) == 0)) { - const string name_suffix(name, strlen(".debug_")); - const string sh_name_suffix(sh_name, strlen(".zdebug_")); - return name_suffix == sh_name_suffix; +bool ElfReader::SectionNamesMatch(std::string_view name, + std::string_view sh_name) { + std::string_view debug_prefix{".debug_"}; + std::string_view zdebug_prefix{".zdebug_"}; + if (StringViewStartsWith(name, debug_prefix) && + StringViewStartsWith(sh_name, zdebug_prefix)) { + name.remove_prefix(debug_prefix.length()); + sh_name.remove_prefix(zdebug_prefix.length()); + return name == sh_name; } return name == sh_name; } diff --git a/src/common/dwarf/elf_reader.h b/src/common/dwarf/elf_reader.h index 672969d8..a6dec755 100644 --- a/src/common/dwarf/elf_reader.h +++ b/src/common/dwarf/elf_reader.h @@ -16,6 +16,7 @@ #define COMMON_DWARF_ELF_READER_H__ #include +#include #include #include "common/dwarf/types.h" @@ -145,7 +146,8 @@ class ElfReader { // appears in the elf-file, adjusting for compressed debug section // names. For example, returns true if name == ".debug_abbrev" and // sh_name == ".zdebug_abbrev" - static bool SectionNamesMatch(const string& name, const string& sh_name); + static bool SectionNamesMatch(std::string_view name, + std::string_view sh_name); private: // Lazily initialize impl32_ and return it.