Skip to content

Commit b9356bf

Browse files
authored
fix: make sure that postject-api.h compiles without warnings (#54)
* 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: nodejs/node#45038 Signed-off-by: Darshan Sen <[email protected]> * 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 <[email protected]> * fix: use -Wall -WX on Windows Signed-off-by: Darshan Sen <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * 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<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,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 <[email protected]> * fix: use / instead of - for Windows compiler options Signed-off-by: Darshan Sen <[email protected]> * chore: use target_compile_options instead of set Signed-off-by: Darshan Sen <[email protected]> Signed-off-by: Darshan Sen <[email protected]>
1 parent 1f1d160 commit b9356bf

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

postject-api.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct postject_options {
2727
const char* pe_resource_name;
2828
};
2929

30-
static void postject_options_init(struct postject_options* options) {
30+
inline void postject_options_init(struct postject_options* options) {
3131
options->elf_section_name = NULL;
3232
options->macho_framework_name = NULL;
3333
options->macho_section_name = NULL;
@@ -93,7 +93,6 @@ static const void* postject_find_resource(
9393

9494
return ptr;
9595
#elif defined(__linux__)
96-
void* ptr = NULL;
9796

9897
if (options != NULL && options->elf_section_name != NULL) {
9998
name = options->elf_section_name;
@@ -151,7 +150,7 @@ static const void* postject_find_resource(
151150
if (resource_name == NULL) {
152151
return NULL;
153152
}
154-
strcpy(resource_name, name);
153+
strcpy_s(resource_name, strlen(name) + 1, name);
155154
CharUpperA(resource_name); // Uppercases inplace
156155
}
157156

test/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ project(postject-tests)
55

66
add_executable(c_test test.c)
77
add_executable(cpp_test test.cpp)
8+
9+
if(WIN32)
10+
target_compile_options(c_test PRIVATE /W4 /WX)
11+
target_compile_options(cpp_test PRIVATE /W4 /WX /EHsc)
12+
else()
13+
target_compile_options(c_test PRIVATE -Wall -Werror)
14+
target_compile_options(cpp_test PRIVATE -Wall -Werror)
15+
endif()

test/test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ int main() {
1010
if (ptr && size > 0) {
1111
char* str = (char*)malloc(size + 1);
1212
memset(str, 0, size + 1);
13+
#if defined(_WIN32)
14+
strncpy_s(str, size + 1, ptr, size);
15+
#else
1316
strncpy(str, ptr, size);
17+
#endif
1418
printf("%s\n", str);
1519
} else {
1620
printf("Hello world\n");

0 commit comments

Comments
 (0)