[compiler-rt] Some clean up / refactoring in sanitizer_symbolizer_libcdep.cpp.

Summary:
Nothing critical, just a few potential improvements I've noticed while reading
the code:
- return `false` when symbolizer buffer is too small to read all data
- invert some conditions to reduce indentation
- prefer `nullptr` over `0` for pointers; init some pointers on stack;
- remove minor code duplication

Reviewers: eugenis, vitalybuka

Subscribers: dberris, #sanitizers, llvm-commits, kcc

Tags: #sanitizers, #llvm

Differential Revision: https://reviews.llvm.org/D74137
This commit is contained in:
Max Moroz 2020-02-06 08:44:42 -08:00
parent b1c7bfe6da
commit 5ad62d3b7f
1 changed files with 45 additions and 48 deletions

View File

@ -39,9 +39,9 @@ const char *ExtractToken(const char *str, const char *delims, char **result) {
}
const char *ExtractInt(const char *str, const char *delims, int *result) {
char *buff;
char *buff = nullptr;
const char *ret = ExtractToken(str, delims, &buff);
if (buff != 0) {
if (buff) {
*result = (int)internal_atoll(buff);
}
InternalFree(buff);
@ -49,9 +49,9 @@ const char *ExtractInt(const char *str, const char *delims, int *result) {
}
const char *ExtractUptr(const char *str, const char *delims, uptr *result) {
char *buff;
char *buff = nullptr;
const char *ret = ExtractToken(str, delims, &buff);
if (buff != 0) {
if (buff) {
*result = (uptr)internal_atoll(buff);
}
InternalFree(buff);
@ -59,9 +59,9 @@ const char *ExtractUptr(const char *str, const char *delims, uptr *result) {
}
const char *ExtractSptr(const char *str, const char *delims, sptr *result) {
char *buff;
char *buff = nullptr;
const char *ret = ExtractToken(str, delims, &buff);
if (buff != 0) {
if (buff) {
*result = (sptr)internal_atoll(buff);
}
InternalFree(buff);
@ -83,7 +83,7 @@ const char *ExtractTokenUpToDelimiter(const char *str, const char *delimiter,
SymbolizedStack *Symbolizer::SymbolizePC(uptr addr) {
BlockingMutexLock l(&mu_);
const char *module_name;
const char *module_name = nullptr;
uptr module_offset;
ModuleArch arch;
SymbolizedStack *res = SymbolizedStack::New(addr);
@ -103,7 +103,7 @@ SymbolizedStack *Symbolizer::SymbolizePC(uptr addr) {
bool Symbolizer::SymbolizeData(uptr addr, DataInfo *info) {
BlockingMutexLock l(&mu_);
const char *module_name;
const char *module_name = nullptr;
uptr module_offset;
ModuleArch arch;
if (!FindModuleNameAndOffsetForAddress(addr, &module_name, &module_offset,
@ -124,7 +124,7 @@ bool Symbolizer::SymbolizeData(uptr addr, DataInfo *info) {
bool Symbolizer::SymbolizeFrame(uptr addr, FrameInfo *info) {
BlockingMutexLock l(&mu_);
const char *module_name;
const char *module_name = nullptr;
if (!FindModuleNameAndOffsetForAddress(
addr, &module_name, &info->module_offset, &info->module_arch))
return false;
@ -175,7 +175,7 @@ bool Symbolizer::FindModuleNameAndOffsetForAddress(uptr address,
uptr *module_offset,
ModuleArch *module_arch) {
const LoadedModule *module = FindModuleForAddress(address);
if (module == nullptr)
if (!module)
return false;
*module_name = module->full_name();
*module_offset = address - module->base_address();
@ -292,7 +292,7 @@ LLVMSymbolizer::LLVMSymbolizer(const char *path, LowLevelAllocator *allocator)
// Windows, so extract tokens from the right hand side first. The column info is
// also optional.
static const char *ParseFileLineInfo(AddressInfo *info, const char *str) {
char *file_line_info = 0;
char *file_line_info = nullptr;
str = ExtractToken(str, "\n", &file_line_info);
CHECK(file_line_info);
@ -323,7 +323,7 @@ void ParseSymbolizePCOutput(const char *str, SymbolizedStack *res) {
bool top_frame = true;
SymbolizedStack *last = res;
while (true) {
char *function_name = 0;
char *function_name = nullptr;
str = ExtractToken(str, "\n", &function_name);
CHECK(function_name);
if (function_name[0] == '\0') {
@ -402,32 +402,29 @@ bool LLVMSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
AddressInfo *info = &stack->info;
const char *buf = FormatAndSendCommand(
"CODE", info->module, info->module_offset, info->module_arch);
if (buf) {
ParseSymbolizePCOutput(buf, stack);
return true;
}
return false;
if (!buf)
return false;
ParseSymbolizePCOutput(buf, stack);
return true;
}
bool LLVMSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
const char *buf = FormatAndSendCommand(
"DATA", info->module, info->module_offset, info->module_arch);
if (buf) {
ParseSymbolizeDataOutput(buf, info);
info->start += (addr - info->module_offset); // Add the base address.
return true;
}
return false;
if (!buf)
return false;
ParseSymbolizeDataOutput(buf, info);
info->start += (addr - info->module_offset); // Add the base address.
return true;
}
bool LLVMSymbolizer::SymbolizeFrame(uptr addr, FrameInfo *info) {
const char *buf = FormatAndSendCommand(
"FRAME", info->module, info->module_offset, info->module_arch);
if (buf) {
ParseSymbolizeFrameOutput(buf, &info->locals);
return true;
}
return false;
if (!buf)
return false;
ParseSymbolizeFrameOutput(buf, &info->locals);
return true;
}
const char *LLVMSymbolizer::FormatAndSendCommand(const char *command_prefix,
@ -435,21 +432,21 @@ const char *LLVMSymbolizer::FormatAndSendCommand(const char *command_prefix,
uptr module_offset,
ModuleArch arch) {
CHECK(module_name);
if (arch == kModuleArchUnknown) {
if (internal_snprintf(buffer_, kBufferSize, "%s \"%s\" 0x%zx\n",
command_prefix, module_name,
module_offset) >= static_cast<int>(kBufferSize)) {
Report("WARNING: Command buffer too small");
return nullptr;
}
} else {
if (internal_snprintf(buffer_, kBufferSize, "%s \"%s:%s\" 0x%zx\n",
command_prefix, module_name, ModuleArchToString(arch),
module_offset) >= static_cast<int>(kBufferSize)) {
Report("WARNING: Command buffer too small");
return nullptr;
}
int size_needed = 0;
if (arch == kModuleArchUnknown)
size_needed = internal_snprintf(buffer_, kBufferSize, "%s \"%s\" 0x%zx\n",
command_prefix, module_name, module_offset);
else
size_needed = internal_snprintf(buffer_, kBufferSize,
"%s \"%s:%s\" 0x%zx\n", command_prefix,
module_name, ModuleArchToString(arch),
module_offset);
if (size_needed >= static_cast<int>(kBufferSize)) {
Report("WARNING: Command buffer too small");
return nullptr;
}
return symbolizer_process_->SendCommand(buffer_);
}
@ -492,16 +489,16 @@ const char *SymbolizerProcess::SendCommand(const char *command) {
Report("WARNING: Failed to use and restart external symbolizer!\n");
failed_to_start_ = true;
}
return 0;
return nullptr;
}
const char *SymbolizerProcess::SendCommandImpl(const char *command) {
if (input_fd_ == kInvalidFd || output_fd_ == kInvalidFd)
return 0;
return nullptr;
if (!WriteToSymbolizer(command, internal_strlen(command)))
return 0;
return nullptr;
if (!ReadFromSymbolizer(buffer_, kBufferSize))
return 0;
return nullptr;
return buffer_;
}
@ -532,8 +529,8 @@ bool SymbolizerProcess::ReadFromSymbolizer(char *buffer, uptr max_length) {
break;
if (read_len + 1 == max_length) {
Report("WARNING: Symbolizer buffer too small\n");
read_len = 0;
break;
buffer[0] = '\0';
return false;
}
}
buffer[read_len] = '\0';