From 1b6ba415627fff57d26049d2e9cba09d4142ce61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sun, 16 Oct 2022 12:50:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20hardening?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 8 ++++++++ file.c | 25 +++++++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) 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;