Skip to content

Commit

Permalink
Increase warning level and treat warnings as errors. (#7)
Browse files Browse the repository at this point in the history
* Build: Add higher warn levels and fix warnings

* Build: Treat warnings as errors
  • Loading branch information
cursey committed Feb 13, 2023
1 parent 84c704a commit 3393ced
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 10 deletions.
31 changes: 31 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,37 @@ target_compile_features(safetyhook PUBLIC
cxx_std_17
)

if(MSVC) # msvc
target_compile_options(safetyhook PUBLIC
"/WX"
"/permissive-"
"/W4"
"/w14640"
)
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "Clang") # clang
target_compile_options(safetyhook PUBLIC
-Werror
-Wall
-Wextra
-Wshadow
-Wnon-virtual-dtor
-pedantic
)
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "GNU") # gcc
target_compile_options(safetyhook PUBLIC
-Werror
-Wall
-Wextra
-Wshadow
-Wnon-virtual-dtor
-pedantic
)
endif()

target_include_directories(safetyhook PUBLIC
"include/"
)
Expand Down
3 changes: 3 additions & 0 deletions cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ compile-features = ["cxx_std_17"]
compile-definitions = ["NOMINMAX"]
link-libraries = ["bddisasm::bddisasm"]
alias = "safetyhook::safetyhook"
msvc.compile-options = ["/WX", "/permissive-", "/W4", "/w14640"]
clang.compile-options = ["-Werror", "-Wall", "-Wextra", "-Wshadow", "-Wnon-virtual-dtor", "-pedantic"]
gcc.compile-options = ["-Werror", "-Wall", "-Wextra", "-Wshadow", "-Wnon-virtual-dtor", "-pedantic"]

[target.test0]
condition = "build-tests"
Expand Down
12 changes: 6 additions & 6 deletions src/InlineHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct JmpFF {
static auto make_jmp_ff(uintptr_t src, uintptr_t dst, uintptr_t data) {
JmpFF jmp{};

jmp.offset = data - src - sizeof(jmp);
jmp.offset = static_cast<uint32_t>(data - src - sizeof(jmp));
*(uintptr_t*)data = dst;

return jmp;
Expand All @@ -65,7 +65,7 @@ static void emit_jmp_ff(uintptr_t src, uintptr_t dst, uintptr_t data, size_t siz
constexpr auto make_jmp_e9(uintptr_t src, uintptr_t dst) {
JmpE9 jmp{};

jmp.offset = dst - src - sizeof(jmp);
jmp.offset = static_cast<uint32_t>(dst - src - sizeof(jmp));

return jmp;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ InlineHook::~InlineHook() {

std::copy_n(m_original_bytes.data(), m_original_bytes.size(), (uint8_t*)m_target);

for (auto i = 0; i < m_trampoline_size; ++i) {
for (size_t i = 0; i < m_trampoline_size; ++i) {
builder.fix_ip(m_trampoline + i, m_target + i);
}

Expand Down Expand Up @@ -217,7 +217,7 @@ void InlineHook::e9_hook() {
emit_jmp_e9(src, dst);
#endif

for (auto i = 0; i < m_trampoline_size; ++i) {
for (size_t i = 0; i < m_trampoline_size; ++i) {
builder.fix_ip(m_target + i, m_trampoline + i);
}
}
Expand Down Expand Up @@ -267,8 +267,8 @@ void InlineHook::ff_hook() {
data = src + sizeof(JmpFF);
emit_jmp_ff(src, dst, data, m_trampoline_size);

for (auto i = 0; i < m_trampoline_size; ++i) {
for (size_t i = 0; i < m_trampoline_size; ++i) {
builder.fix_ip(m_target + i, m_trampoline + i);
}
}
} // namespace safetyhook
} // namespace safetyhook
2 changes: 1 addition & 1 deletion tests/test0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void hook3_fn(const std::string& name) {
hook3->call<void, const std::string&>(name + " and carol");
}

int main(int argc, char* argv[]) {
int main() {
{
auto builder = SafetyHookFactory::acquire();
hook0 = builder.create_inline((void*)say_hi, (void*)hook0_fn);
Expand Down
2 changes: 1 addition & 1 deletion tests/test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int hook_add(int x, int y) {
return g_add_hook->call<int>(x * 2, y * 2);
}

int main(int argc, char* argv[]) {
int main() {
std::cout << "unhooked add(2, 3) = " << add(2, 3) << "\n";

{
Expand Down
2 changes: 1 addition & 1 deletion tests/test2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void hooked_add_42(SafetyHookContext& ctx) {

SafetyMidHook g_hook{};

int main(int argc, char* argv[]) {
int main() {
std::cout << add_42(2) << "\n";

{
Expand Down
2 changes: 1 addition & 1 deletion tests/test3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void hook3_fn(const std::string& name) {
hook3->call<void, const std::string&>(name + " and carol");
}

int main(int argc, char* argv[]) {
int main() {
{
// Don't do this, it's just for testing. Try to just use one builder to create all your hooks.
hook0 = SafetyHookFactory::acquire().create_inline((void*)say_hi, (void*)hook0_fn);
Expand Down

0 comments on commit 3393ced

Please sign in to comment.