From 5b5dceeb0dce6d4d428a699e689144fcde96fbbf Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Wed, 19 Oct 2022 13:15:56 +0530 Subject: [PATCH 1/9] fix: make sure that postject-api.h compiles without warnings Fixes the following error I came across while integrating Postject in Node.js: ```console ../deps/postject/src/dist/postject-api.h:30:13: error: unused function 'postject_options_init' [-Werror,-Wunused-function] static void postject_options_init(struct postject_options* options) { ^ 1 error generated. ``` Refs: https://github.com/nodejs/node/pull/45038 Signed-off-by: Darshan Sen --- postject-api.h | 2 +- test/CMakeLists.txt | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/postject-api.h b/postject-api.h index 14899ee..104aa34 100644 --- a/postject-api.h +++ b/postject-api.h @@ -27,7 +27,7 @@ struct postject_options { const char* pe_resource_name; }; -static void postject_options_init(struct postject_options* options) { +inline void postject_options_init(struct postject_options* options) { options->elf_section_name = NULL; options->macho_framework_name = NULL; options->macho_section_name = NULL; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 45f78e6..eacb7a5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,9 @@ cmake_minimum_required(VERSION 3.9) set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_FLAGS "-Wall -Werror") +set(CMAKE_C_FLAGS "-Wall -Werror") + project(postject-tests) add_executable(c_test test.c) From 687cc5605b4a6115cc05e7504a357acd59c98603 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Wed, 19 Oct 2022 13:42:09 +0530 Subject: [PATCH 2/9] fix: resolve another compiler warning ```console In file included from /root/project/test/test.c:4: /root/project/test/../dist/postject-api.h: In function 'postject_find_resource': /root/project/test/../dist/postject-api.h:96:9: error: unused variable 'ptr' [-Werror=unused-variable] 96 | void* ptr = NULL; | ^~~ cc1: all warnings being treated as errors ``` Refs: https://app.circleci.com/pipelines/github/postmanlabs/postject/180/workflows/d0eb47c0-5482-4c85-9c63-aa854ddb0221/jobs/1398?invite=true#step-110-678 Signed-off-by: Darshan Sen --- postject-api.h | 1 - 1 file changed, 1 deletion(-) diff --git a/postject-api.h b/postject-api.h index 104aa34..e6f837f 100644 --- a/postject-api.h +++ b/postject-api.h @@ -93,7 +93,6 @@ static const void* postject_find_resource( return ptr; #elif defined(__linux__) - void* ptr = NULL; if (options != NULL && options->elf_section_name != NULL) { name = options->elf_section_name; From fdb73d42d33a2086b665bc4db1ff82e9c61a3ecc Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Wed, 19 Oct 2022 14:50:31 +0530 Subject: [PATCH 3/9] fix: use -Wall -WX on Windows Signed-off-by: Darshan Sen --- test/CMakeLists.txt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index eacb7a5..e69774d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,8 +1,13 @@ cmake_minimum_required(VERSION 3.9) set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_FLAGS "-Wall -Werror") -set(CMAKE_C_FLAGS "-Wall -Werror") +if(WIN32) + set(CMAKE_CXX_FLAGS "-Wall -WX") + set(CMAKE_C_FLAGS "-Wall -WX") +else() + set(CMAKE_CXX_FLAGS "-Wall -Werror") + set(CMAKE_C_FLAGS "-Wall -Werror") +endif() project(postject-tests) From 9809ff4556e54f944e1b78fda03b7b8febee95fd Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Wed, 19 Oct 2022 17:52:15 +0530 Subject: [PATCH 4/9] chore: use -W4 instead of -Wall -Wall produces too many warnings. Refs: https://app.circleci.com/pipelines/github/postmanlabs/postject/185/workflows/48b0f126-8000-41d9-b39e-cb8b9e4bc9d6/jobs/1441?invite=true#step-107-693 Signed-off-by: Darshan Sen --- test/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e69774d..aea882d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.9) set(CMAKE_CXX_STANDARD 11) if(WIN32) - set(CMAKE_CXX_FLAGS "-Wall -WX") - set(CMAKE_C_FLAGS "-Wall -WX") + set(CMAKE_CXX_FLAGS "-W4 -WX") + set(CMAKE_C_FLAGS "-W4 -WX") else() set(CMAKE_CXX_FLAGS "-Wall -Werror") set(CMAKE_C_FLAGS "-Wall -Werror") From 3c4ff65d6accf1757285bca5535f6e5f9bd3e63b Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Wed, 19 Oct 2022 18:22:51 +0530 Subject: [PATCH 5/9] fix: another compilation warning on Windows ```console C:\Users\circleci\project\test\../dist/postject-api.h(153,5): error C2220: the following warning is treated as an error [C:\Users\circleci\project\build\test\c_test.vcxproj] C:\Users\circleci\project\test\../dist/postject-api.h(153,5): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [C:\Users\circleci\project\build\test\c_test.vcxproj] C:\Users\circleci\project\test\test.c(13,5): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [C:\Users\circleci\project\build\test\c_test.vcxproj] ``` Refs: https://app.circleci.com/pipelines/github/postmanlabs/postject/186/workflows/f1389b9f-c958-4a24-9d6e-28af856ff776/jobs/1455?invite=true#step-107-694 Signed-off-by: Darshan Sen --- postject-api.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postject-api.h b/postject-api.h index e6f837f..aa17e8d 100644 --- a/postject-api.h +++ b/postject-api.h @@ -150,7 +150,7 @@ static const void* postject_find_resource( if (resource_name == NULL) { return NULL; } - strcpy(resource_name, name); + strcpy_s(resource_name, strlen(name) + 1, name); CharUpperA(resource_name); // Uppercases inplace } From 2bb8618a318a6b17a8f4b10337c2905f65a85a03 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Wed, 19 Oct 2022 18:44:25 +0530 Subject: [PATCH 6/9] fix: compiler warning on Windows ```console C:\Users\circleci\project\test\test.c(13,5): error C2220: the following warning is treated as an error [C:\Users\circleci\project\build\test\c_test.vcxproj] C:\Users\circleci\project\test\test.c(13,5): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [C:\Users\circleci\project\build\test\c_test.vcxproj] ``` Refs: https://app.circleci.com/pipelines/github/postmanlabs/postject/187/workflows/8bcdcb96-646d-4008-93bf-294e92469b3d/jobs/1464?invite=true#step-107-694 Signed-off-by: Darshan Sen --- test/test.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test.c b/test/test.c index e43d44d..5eb5f4f 100644 --- a/test/test.c +++ b/test/test.c @@ -10,7 +10,11 @@ int main() { if (ptr && size > 0) { char* str = (char*)malloc(size + 1); memset(str, 0, size + 1); +#if defined(_WIN32) + strncpy_s(str, size + 1, ptr, size); +#else strncpy(str, ptr, size); +#endif printf("%s\n", str); } else { printf("Hello world\n"); From fd31d7f3fd4fc9a99acd6cf04478d66e9e0813b6 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Wed, 19 Oct 2022 19:04:14 +0530 Subject: [PATCH 7/9] fix: use -EHsc for test.cpp ```console C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\ostream(743,1): error C2220: the following warning is treated as an error [C:\Users\circleci\project\build\test\cpp_test.vcxproj] C:\Users\circleci\project\test\test.cpp(13): message : see reference to function template instantiation 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,const char *)' being compiled [C:\Users\circleci\project\build\test\cpp_test.vcxproj] C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\ostream(743,1): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc [C:\Users\circleci\project\build\test\cpp_test.vcxproj] ``` Refs: https://app.circleci.com/pipelines/github/postmanlabs/postject/188/workflows/9107adc5-61a3-41ad-bd60-dd3eb0996765/jobs/1470?invite=true#step-107-696 Signed-off-by: Darshan Sen --- test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index aea882d..5e00a0d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.9) set(CMAKE_CXX_STANDARD 11) if(WIN32) - set(CMAKE_CXX_FLAGS "-W4 -WX") + set(CMAKE_CXX_FLAGS "-W4 -WX -EHsc") set(CMAKE_C_FLAGS "-W4 -WX") else() set(CMAKE_CXX_FLAGS "-Wall -Werror") From 9af316ee8248bd642f4259db7187aea4a5017be4 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Wed, 19 Oct 2022 19:05:29 +0530 Subject: [PATCH 8/9] fix: use / instead of - for Windows compiler options Signed-off-by: Darshan Sen --- test/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5e00a0d..eeadc0c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.9) set(CMAKE_CXX_STANDARD 11) if(WIN32) - set(CMAKE_CXX_FLAGS "-W4 -WX -EHsc") - set(CMAKE_C_FLAGS "-W4 -WX") + set(CMAKE_CXX_FLAGS "/W4 /WX /EHsc") + set(CMAKE_C_FLAGS "/W4 /WX") else() set(CMAKE_CXX_FLAGS "-Wall -Werror") set(CMAKE_C_FLAGS "-Wall -Werror") From 4a4e386656bf11242d3bc192fa05ac9af4af0c6a Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Thu, 20 Oct 2022 17:54:20 +0530 Subject: [PATCH 9/9] chore: use target_compile_options instead of set Signed-off-by: Darshan Sen --- test/CMakeLists.txt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index eeadc0c..6ee70bd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,15 +1,15 @@ cmake_minimum_required(VERSION 3.9) set(CMAKE_CXX_STANDARD 11) -if(WIN32) - set(CMAKE_CXX_FLAGS "/W4 /WX /EHsc") - set(CMAKE_C_FLAGS "/W4 /WX") -else() - set(CMAKE_CXX_FLAGS "-Wall -Werror") - set(CMAKE_C_FLAGS "-Wall -Werror") -endif() - project(postject-tests) add_executable(c_test test.c) add_executable(cpp_test test.cpp) + +if(WIN32) + target_compile_options(c_test PRIVATE /W4 /WX) + target_compile_options(cpp_test PRIVATE /W4 /WX /EHsc) +else() + target_compile_options(c_test PRIVATE -Wall -Werror) + target_compile_options(cpp_test PRIVATE -Wall -Werror) +endif()