diff --git a/CMakeLists.txt b/CMakeLists.txt index 72386ae..2b8d788 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,14 @@ ELSE() add_library(base16384_s STATIC file.c base1432.c) ENDIF() +if (MSVC) + # do nothing +else() + # add hardening + target_compile_options(base16384 INTERFACE -Wl,-z,relro,-z,now -Wdate-time) + add_definitions(-D_FORTIFY_SOURCE=2) +endif() + set_target_properties(base16384_b PROPERTIES OUTPUT_NAME base16384) set_target_properties(base16384_s PROPERTIES OUTPUT_NAME base16384) set_target_properties(base16384 PROPERTIES VERSION ${CMAKE_PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}) diff --git a/file.c b/file.c index 2d6fc4c..af19b7a 100644 --- a/file.c +++ b/file.c @@ -21,6 +21,7 @@ #include #include #include +#include #ifdef _WIN32 #include #else @@ -73,10 +74,8 @@ base16384_err_t base16384_encode_file(const char* input, const char* output, cha return base16384_err_write_file; } } - /* 由操作系统负责释放资源 - fclose(fpo); - fclose(fp); - 以缩短程序运行时间 */ + fclose(fpo); + fclose(fp); #ifndef _WIN32 } else { // small file, use mmap & fwrite int fd = open(input, O_RDONLY); @@ -95,10 +94,8 @@ base16384_err_t base16384_encode_file(const char* input, const char* output, cha return base16384_err_write_file; } munmap(input_file, (size_t)inputsize); - /* 由操作系统负责释放资源 - fclose(fpo); - close(fd); - 以缩短程序运行时间 */ + fclose(fpo); + close(fd); } #endif return base16384_err_ok; @@ -156,10 +153,8 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha return base16384_err_write_file; } } - /* 由操作系统负责释放资源 - fclose(fpo); - fclose(fp); - 以缩短程序运行时间 */ + fclose(fpo); + fclose(fp); #ifndef _WIN32 } else { // small file, use mmap & fwrite int fd = open(input, O_RDONLY); @@ -176,10 +171,8 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha return base16384_err_write_file; } munmap(input_file, (size_t)inputsize); - /* 由操作系统负责释放资源 - fclose(fpo); - close(fd); - 以缩短程序运行时间 */ + fclose(fpo); + close(fd); } #endif return base16384_err_ok;