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 <mark@chromium.org>
This commit is contained in:
Zequan Wu 2022-12-15 12:27:22 +08:00 committed by Mark Mentovai
parent 6b7e8a80ba
commit 63af0cd752
2 changed files with 27 additions and 13 deletions

View file

@ -39,9 +39,9 @@
#include <unistd.h>
#include <algorithm>
#include <cstring>
#include <map>
#include <string>
#include <string_view>
#include <vector>
// 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;
}

View file

@ -16,6 +16,7 @@
#define COMMON_DWARF_ELF_READER_H__
#include <string>
#include <string_view>
#include <vector>
#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.