Skip to content

Commit

Permalink
relaxing a few casts from reinterpret to static
Browse files Browse the repository at this point in the history
improving some code to not need as many casts
  • Loading branch information
eteran committed Mar 21, 2024
1 parent dbb8975 commit 61aec75
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
6 changes: 3 additions & 3 deletions plugins/BinaryInfo/DialogHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ DialogHeader::DialogHeader(const std::shared_ptr<IRegion> &region, QWidget *pare

if (auto elf32 = dynamic_cast<ELF32 *>(binary_info.get())) {

auto header = reinterpret_cast<const elf32_header *>(elf32->header());
auto header = static_cast<const elf32_header *>(elf32->header());

auto root = new QTreeWidgetItem;
root->setText(0, tr("ELF32"));
Expand All @@ -471,7 +471,7 @@ DialogHeader::DialogHeader(const std::shared_ptr<IRegion> &region, QWidget *pare

if (auto elf64 = dynamic_cast<ELF64 *>(binary_info.get())) {

auto header = reinterpret_cast<const elf64_header *>(elf64->header());
auto header = static_cast<const elf64_header *>(elf64->header());

auto root = new QTreeWidgetItem;
root->setText(0, tr("ELF64"));
Expand All @@ -493,7 +493,7 @@ DialogHeader::DialogHeader(const std::shared_ptr<IRegion> &region, QWidget *pare
if (auto pe32 = dynamic_cast<PE32 *>(binary_info.get())) {
Q_UNUSED(pe32)
#if 0
auto header = reinterpret_cast<const pe32_header *>(pe32->header());
auto header = static_cast<const pe32_header *>(pe32->header());
#endif
auto root = new QTreeWidgetItem;
root->setText(0, tr("PE32"));
Expand Down
10 changes: 5 additions & 5 deletions plugins/BinaryInfo/symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ struct elf64_model : elf_model<64> {
};

[[nodiscard]] bool is_elf32(const void *ptr) {
auto elf32_hdr = reinterpret_cast<const elf32_header *>(ptr);
auto elf32_hdr = static_cast<const elf32_header *>(ptr);
if (std::memcmp(elf32_hdr->e_ident, ELFMAG, SELFMAG) == 0) {
return elf32_hdr->e_ident[EI_CLASS] == ELFCLASS32;
}
return false;
}

[[nodiscard]] bool is_elf64(const void *ptr) {
auto elf64_hdr = reinterpret_cast<const elf64_header *>(ptr);
auto elf64_hdr = static_cast<const elf64_header *>(ptr);
if (std::memcmp(elf64_hdr->e_ident, ELFMAG, SELFMAG) == 0) {
return elf64_hdr->e_ident[EI_CLASS] == ELFCLASS64;
}
Expand Down Expand Up @@ -434,7 +434,7 @@ void output_symbols(std::vector<Symbol> &symbols, std::ostream &os) {
// Desc:
//--------------------------------------------------------------------------
bool generate_symbols_internal(QFile &file, std::shared_ptr<QFile> &debugFile, std::ostream &os) {
if (auto file_ptr = reinterpret_cast<void *>(file.map(0, file.size(), QFile::NoOptions))) {
if (auto file_ptr = static_cast<void *>(file.map(0, file.size(), QFile::NoOptions))) {
if (is_elf64(file_ptr)) {

using symbol = typename elf64_model::symbol;
Expand All @@ -448,7 +448,7 @@ bool generate_symbols_internal(QFile &file, std::shared_ptr<QFile> &debugFile, s
if (debugFile->open(QIODevice::ReadOnly)) {

// map it and include it with the symbols
if (auto debug_ptr = reinterpret_cast<void *>(debugFile->map(0, debugFile->size(), QFile::NoOptions))) {
if (auto debug_ptr = static_cast<void *>(debugFile->map(0, debugFile->size(), QFile::NoOptions))) {

// this should never fail... but just being sure
if (is_elf64(debug_ptr)) {
Expand All @@ -475,7 +475,7 @@ bool generate_symbols_internal(QFile &file, std::shared_ptr<QFile> &debugFile, s
if (debugFile->open(QIODevice::ReadOnly)) {

// map it and include it with the symbols
if (auto debug_ptr = reinterpret_cast<void *>(debugFile->map(0, debugFile->size(), QFile::NoOptions))) {
if (auto debug_ptr = static_cast<void *>(debugFile->map(0, debugFile->size(), QFile::NoOptions))) {

// this should never fail... but just being sure
if (is_elf32(debug_ptr)) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/DebuggerCore/unix/linux/PlatformProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ std::size_t PlatformProcess::readBytes(edb::address_t address, void *buf, std::s
}

// store it
reinterpret_cast<char *>(buf)[index] = x;
static_cast<char *>(buf)[index] = x;

++read;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/InstructionInspector/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ std::string printBytes(const void *ptr, std::size_t size, bool printZeros = true

std::ostringstream str;
str << std::setfill('0') << std::uppercase << std::hex;
const auto bytes = reinterpret_cast<const unsigned char *>(ptr);
const auto bytes = static_cast<const unsigned char *>(ptr);

for (std::size_t i = 0; i < size; ++i) {
if (!str.str().empty()) {
Expand Down
18 changes: 10 additions & 8 deletions src/arch/x86-generic/ArchProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,15 @@ QString formatBCD(const edb::value80 &v) {
}

template <class ValueType>
QString formatPackedFloat(const char *data, std::size_t size) {
QString formatPackedFloat(const void *data, std::size_t size) {

auto ptr = static_cast<const char *>(data);

QString str;
for (std::size_t offset = 0; offset < size; offset += sizeof(ValueType)) {

ValueType value;
std::memcpy(&value, data + offset, sizeof(value));
std::memcpy(&value, ptr + offset, sizeof(value));
if (!str.isEmpty()) {
str += ", ";
}
Expand Down Expand Up @@ -773,9 +775,9 @@ void analyze_operands(const State &state, const edb::Instruction &inst, QStringL
valueStr += " (decimal)";
}
} else if (is_SIMD_PS(operand)) {
valueStr = formatPackedFloat<edb::value32>(reinterpret_cast<const char *>(&target), sizeof(edb::value64));
valueStr = formatPackedFloat<edb::value32>(&target, sizeof(edb::value64));
} else if (is_SIMD_PD(operand)) {
valueStr = formatPackedFloat<edb::value64>(reinterpret_cast<const char *>(&target), sizeof(edb::value64));
valueStr = formatPackedFloat<edb::value64>(&target, sizeof(edb::value64));
} else {
valueStr = "0x" + value.toHexString();
}
Expand Down Expand Up @@ -814,9 +816,9 @@ void analyze_operands(const State &state, const edb::Instruction &inst, QStringL
case 16: {
QString valueString;
if (is_SIMD_PS(operand)) {
valueString = formatPackedFloat<edb::value32>(reinterpret_cast<const char *>(&target), sizeof(edb::value128));
valueString = formatPackedFloat<edb::value32>(&target, sizeof(edb::value128));
} else if (is_SIMD_PD(operand)) {
valueString = formatPackedFloat<edb::value64>(reinterpret_cast<const char *>(&target), sizeof(edb::value128));
valueString = formatPackedFloat<edb::value64>(&target, sizeof(edb::value128));
} else {
valueString = "0x" + edb::value128(target).toHexString();
}
Expand All @@ -826,9 +828,9 @@ void analyze_operands(const State &state, const edb::Instruction &inst, QStringL
case 32: {
QString valueString;
if (is_SIMD_PS(operand)) {
valueString = formatPackedFloat<edb::value32>(reinterpret_cast<const char *>(&target), sizeof(edb::value256));
valueString = formatPackedFloat<edb::value32>(&target, sizeof(edb::value256));
} else if (is_SIMD_PD(operand)) {
valueString = formatPackedFloat<edb::value64>(reinterpret_cast<const char *>(&target), sizeof(edb::value256));
valueString = formatPackedFloat<edb::value64>(&target, sizeof(edb::value256));
} else {
valueString = "0x" + edb::value256(target).toHexString();
}
Expand Down

0 comments on commit 61aec75

Please sign in to comment.