From 6a21a82f30e3a3021e9b5eaaad56c23f2701fe05 Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Wed, 12 May 2021 12:20:34 -0700 Subject: [PATCH] Fix `tautological-constant-out-of-range-compare` with Clang on macOS PiperOrigin-RevId: 373424469 Change-Id: Ia6e89e1872f98adc5f1c1b36a601df8d351080ab --- ida/flow_analysis.cc | 4 +--- x86_nop.cc | 21 ++++++++++++++------- x86_nop.h | 10 ++++++---- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/ida/flow_analysis.cc b/ida/flow_analysis.cc index af97d4ee..1e61cd71 100644 --- a/ida/flow_analysis.cc +++ b/ida/flow_analysis.cc @@ -366,9 +366,7 @@ void AnalyzeFlowIda(EntryPoints* entry_points, const ModuleMap& modules, if (mark_x86_nops) { // FLAG_NOP is only important when reconstructing functions, thus we can // set if after AnalyzeFlow(). - const auto& new_instruction_bytes = new_instruction.GetBytes(); - new_instruction.SetFlag(FLAG_NOP, IsNopX86(new_instruction_bytes.data(), - new_instruction_bytes.size())); + new_instruction.SetFlag(FLAG_NOP, IsNopX86(new_instruction.GetBytes())); } instructions->push_back(new_instruction); diff --git a/x86_nop.cc b/x86_nop.cc index 24e721e7..aa56fa10 100644 --- a/x86_nop.cc +++ b/x86_nop.cc @@ -14,7 +14,12 @@ #include "third_party/zynamics/binexport/x86_nop.h" -bool IsNopX86(const char* m, size_t size) { +#include + +bool IsNopX86(absl::string_view bytes) { + auto* m = reinterpret_cast(bytes.data()); + size_t size = bytes.size(); + // Consume up to six prefix bytes: for (int i = 6; i > 0 && size > 0 && m[0] == 0x66; --i, --size, ++m) { } @@ -52,7 +57,8 @@ bool IsNopX86(const char* m, size_t size) { if (m[1] == 0x74) { if (m[2] == 0x00) { return true; // 8d 74 00 lea esi, esi - } else if (m[2] == 0x26) { + } + if (m[2] == 0x26) { if (size >= 4 && m[3] == 0x00) { return true; // 8d 74 26 00 lea esi, [esi + eiz * 1 + 0] } @@ -64,8 +70,9 @@ bool IsNopX86(const char* m, size_t size) { if (m[1] == 0xb4) { if (m[2] == 0x00 && m[3] == 0x00) { return true; // 8d b4 00 00 lea - } else if (size >= 7 && m[2] == 0x26 && m[3] == 0x00 && - m[4] == 0x00 && m[5] == 0x00 && m[6] == 0x00) { + } + if (size >= 7 && m[2] == 0x26 && m[3] == 0x00 && m[4] == 0x00 && + m[5] == 0x00 && m[6] == 0x00) { return true; // 8d b4 26 00 00 00 00 lea } } else if (m[1] == 0xbd && m[2] == 0x00 && m[3] == 0x00) { @@ -75,8 +82,9 @@ bool IsNopX86(const char* m, size_t size) { if (m[1] == 0xb6 && m[2] == 0x00 && m[3] == 0x00 && m[4] == 0x00 && m[5] == 0x00) { return true; // 8d b6 00 00 00 00 lea - } else if (m[1] == 0xbf && m[2] == 0x00 && m[3] == 0x00 && - m[4] == 0x00 && m[5] == 0x00) { + } + if (m[1] == 0xbf && m[2] == 0x00 && m[3] == 0x00 && m[4] == 0x00 && + m[5] == 0x00) { return true; // 8d bf 00 00 00 00 lea } if (size >= 7 && m[1] == 0xbc && m[2] == 0x27 && m[3] == 0x00 && @@ -95,4 +103,3 @@ bool IsNopX86(const char* m, size_t size) { } return false; } - diff --git a/x86_nop.h b/x86_nop.h index 356135b1..2ef27eb3 100644 --- a/x86_nop.h +++ b/x86_nop.h @@ -17,10 +17,12 @@ #include -// Returns true if the instruction starting at "memory" (which contains at least -// "size" bytes) is a NOP instruction. NOPs have been taken from the GNU -// assembler tc-i386.c i386_align_code(). +#include "third_party/absl/strings/string_view.h" + +// Returns true if the instruction starting at the first byte in "bytes" is a +// NOP instruction. NOPs have been taken from the GNU assembler +// i386_align_code() (in tc-i386.c). // For details, see b/24084521#comment7 -bool IsNopX86(const char* m, size_t size); +bool IsNopX86(absl::string_view bytes); #endif // X86_NOP_H_