From be8f8a9f4caa883e3de98b276cdad55d4fc65c0d Mon Sep 17 00:00:00 2001 From: Thorsten Töpper Date: Mon, 16 Jun 2025 20:20:20 +0200 Subject: CMakeLists: hardening flags --- CMakeLists.txt | 6 ++++-- include/list_management.h | 12 ++++++++---- include/options.h | 10 ++++------ include/output.h | 3 +-- src/dir_monitor.c | 4 +++- src/list_management.c | 6 +++--- src/options.c | 6 +----- src/output.c | 6 ++++-- 8 files changed, 28 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 163f941..f9e3ad8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,9 @@ project(dir_monitor # prevent errors from unbeknown features, used to and assuming to be there longer... set (CMAKE_C_STANDARD 17) -add_compile_options(-Wall) +add_compile_options(-Wall -Wextra -Wformat -Wformat=2 -Wconversion -Wimplicit-fallthrough -Werror=format-security + -Wbidi-chars=any -Werror=implicit -Werror=incompatible-pointer-types -Werror=int-conversion + -fno-delete-null-pointer-checks -fstack-clash-protection -fstack-protector-strong -fstrict-flex-arrays=3) set(SOURCE_DM src/dir_monitor.c src/list_management.c src/options.c src/output.c) @@ -28,5 +30,5 @@ target_link_libraries(dir_monitor_asan asan) add_executable(dir_monitor ${SOURCE_DM}) target_include_directories(dir_monitor PRIVATE include) -target_compile_options(dir_monitor_debug PUBLIC -O2 -Werror) +target_compile_options(dir_monitor PUBLIC -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -FPIE -pie -Wl,-z,noexecstack -Wl,--as-needed -Wl,--no-copy-dt-needed-entries) diff --git a/include/list_management.h b/include/list_management.h index 0a887c7..a62fe45 100644 --- a/include/list_management.h +++ b/include/list_management.h @@ -1,8 +1,6 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* Copyright 2025 Thorsten Töpper - * - * list_management - data handling related code * * vim:ts=4:sw=4:expandtab */ @@ -13,9 +11,15 @@ #include /* === DEFINITIONS === */ + +/* off_t is defined in different headers, on Linux with glibc, + * stdio.h, unistd.h and types.h + * output.h requires this struct definition therefore off_t + * becomes implicitly defined through stdio.h as every source + * file includes output.h */ struct list_node { struct list_node *next; - size_t fsize; + off_t fsize; char fname[256]; time_t ftime; /* can be creation, access or modification */ }; @@ -24,7 +28,7 @@ struct list_head { struct list_node *first; }; -struct list_node *create_node(char *fname, size_t fsize, time_t ftime); +struct list_node *create_node(char *fname, long int fsize, time_t ftime); struct list_head *create_list_sort_reversed(struct list_head *list); void destroy_list(struct list_head *list); int insert_sorted_by_size(struct list_head *list, struct list_node *node); diff --git a/include/options.h b/include/options.h index 70f22e4..7f26f4d 100644 --- a/include/options.h +++ b/include/options.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* Copyright 2025 Thorsten Töpper - * - * dir_monitor - print specified stat() information from entries of a given - * directory to stdout. Call it via watch for repeated output to a terminal. * * vim:ts=4:sw=4:expandtab */ @@ -19,12 +16,16 @@ /* === DEFINITIONS === */ +/* TODO: if ported to other platforms, those precompiler checks need to be extended */ +#ifndef PATH_SEP #define PATH_SEP '/' +#endif enum esort_type { SORT_BY_SIZE, SORT_BY_TIME }; /* === GLOBAL VARIABLES === */ + extern bool option_sort_reverse_order; extern enum esort_type option_sort_type; extern bool option_show_hidden_entries; @@ -37,8 +38,5 @@ void set_option(const char *option_name, char *option_argument); * containing the options in options.c */ void usage(char *executable); - - - #endif diff --git a/include/output.h b/include/output.h index 22f3885..beb9559 100644 --- a/include/output.h +++ b/include/output.h @@ -1,8 +1,6 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* Copyright 2025 Thorsten Töpper - * - * output related functions and definitions * * vim:ts=4:sw=4:expandtab */ @@ -25,5 +23,6 @@ int fputc_all_cols(char c, FILE *fdout); int fputc_width_x(char c, size_t x, FILE *fdout); void print_list(struct list_head *list); + #endif diff --git a/src/dir_monitor.c b/src/dir_monitor.c index b2257ce..df85f18 100644 --- a/src/dir_monitor.c +++ b/src/dir_monitor.c @@ -110,7 +110,7 @@ struct list_head *get_data_from_directory(char *path) { int main(int argc, char **argv) { struct list_head *list = NULL; int path_index = 1; - /* TODO: Handle options */ + if (argc < 2) { usage(argv[0]); return EXIT_FAILURE; @@ -120,9 +120,11 @@ int main(int argc, char **argv) { usage(argv[0]); return EXIT_FAILURE; } + list = get_data_from_directory(argv[path_index]); print_list(list); destroy_list(list); + return EXIT_SUCCESS; } diff --git a/src/list_management.c b/src/list_management.c index 634b884..3b02d7f 100644 --- a/src/list_management.c +++ b/src/list_management.c @@ -15,9 +15,10 @@ /* === IMPLEMENTATION === */ -inline struct list_node *create_node(char *fname, size_t fsize, time_t ftime) { + +inline struct list_node *create_node(char *fname, off_t fsize, time_t ftime) { struct list_node *node = NULL; - + if (fname == NULL || fname[0] == '\0') { LOGERR("ERROR: No valid filename given\n"); return NULL; @@ -97,7 +98,6 @@ inline int insert_sorted_by_time(struct list_head *list, struct list_node *node) INSERT_BY_NUMERIC_FIELD(list, node, ftime); return -3; } -#undef INSERT_BY_NUMERIC_FIELD struct list_head *create_list_sort_reversed(struct list_head *list) { diff --git a/src/options.c b/src/options.c index 848eaf5..2136c83 100644 --- a/src/options.c +++ b/src/options.c @@ -1,8 +1,6 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* Copyright 2025 Thorsten Töpper - * - * options - * * vim:ts=4:sw=4:expandtab */ @@ -11,14 +9,11 @@ #include #include - #include "output.h" #include "options.h" - - /* === GLOBAL VARIABLES === */ struct option long_options[] = { { "reverse-sort", no_argument, 0, 0 }, @@ -97,6 +92,7 @@ void set_option(const char *option_name, char *option_argument) { LOGERR("ERROR: Option '%s' not recognized\n.", option_name); } + int parse_arguments(int argc, char **argv) { int c = 0, index; diff --git a/src/output.c b/src/output.c index 9dd274b..be0afb1 100644 --- a/src/output.c +++ b/src/output.c @@ -63,7 +63,7 @@ void print_list(struct list_head *list) { ptr = lh->first; while (ptr != NULL) { - printf(" %8lu %2s ", ((ptr->fsize>=1024) ? (ptr->fsize/1024) : ptr->fsize), + printf(" %8ld %2s ", ((ptr->fsize>=1024) ? (ptr->fsize/1024) : ptr->fsize), ((ptr->fsize >= 1024) ? "kB" : "")); tm = localtime(&ptr->ftime); @@ -75,7 +75,9 @@ void print_list(struct list_head *list) { printf(" %8s ", timestamp); } printf(" %s\n", ptr->fname); - total_size += ptr->fsize; + /* Linux: Neither man stat(2) nor stat(3type) declare when struct stat field st_size + * get's a negative value. */ + total_size += (ptr->fsize>0) ? (unsigned long int)ptr->fsize : 0; ptr = ptr->next; } fputc_all_cols('=', stdout); -- cgit v1.2.3-70-g09d2