From 8f19c243f06502c0e28734071602dd4665af0405 Mon Sep 17 00:00:00 2001 From: klemek Date: Mon, 10 Nov 2025 14:31:37 +0100 Subject: [PATCH] refactor: strlen -> strnlen --- src/config_file.c | 6 +++--- src/forge.c | 4 ++-- src/string.c | 11 ++++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/config_file.c b/src/config_file.c index ca4cfe5..77a5852 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -27,7 +27,7 @@ static uint64_t item_hash(const void *item, uint64_t seed0, uint64_t seed1) { c_item = item; - return hashmap_sip(c_item->key, strlen(c_item->key), seed0, seed1); + return hashmap_sip(c_item->key, strnlen(c_item->key, STR_LEN), seed0, seed1); } static void parse_config_file_line(ConfigFile *config, char *line) { @@ -95,7 +95,7 @@ char *config_file_get_str(ConfigFile *config, char *key, char *default_value) { item = (ConfigFileItem *)hashmap_get(config->map, &c_key); - if (item == NULL || strlen(item->value) == 0) { + if (item == NULL || strnlen(item->value, STR_LEN) == 0) { return default_value; } @@ -111,7 +111,7 @@ unsigned int config_file_get_int(ConfigFile *config, char *key, item = (ConfigFileItem *)hashmap_get(config->map, &c_key); - if (item == NULL || strlen(item->value) == 0) { + if (item == NULL || strnlen(item->value, STR_LEN) == 0) { return default_value; } diff --git a/src/forge.c b/src/forge.c index 5068112..bbe6ff8 100644 --- a/src/forge.c +++ b/src/forge.c @@ -44,12 +44,12 @@ static void compute_fps(bool trace_fps) { } if (window_output != NULL) { - sprintf(title, PACKAGE " " VERSION " - %.0ffps", fps); + snprintf(title, STR_LEN, PACKAGE " " VERSION " - %.0ffps", fps); window_update_title(window_output, title); } if (window_monitor != NULL) { - sprintf(title, PACKAGE " " VERSION " (monitor) - %.0ffps", fps); + snprintf(title, STR_LEN, PACKAGE " " VERSION " (monitor) - %.0ffps", fps); window_update_title(window_monitor, title); } diff --git a/src/string.c b/src/string.c index 631ec46..9a5eeda 100644 --- a/src/string.c +++ b/src/string.c @@ -3,6 +3,7 @@ #include #include +#include "config.h" #include "string.h" unsigned int string_trim(char *str) { @@ -11,7 +12,7 @@ unsigned int string_trim(char *str) { unsigned int end; start = 0; - end = strlen(str) - 1; + end = strnlen(str, STR_LEN) - 1; if (end == 0) { return 0; @@ -28,7 +29,7 @@ unsigned int string_trim(char *str) { } // If the string was trimmed, adjust the null terminator - if (start > 0 || end < (strlen(str) - 1)) { + if (start > 0 || end < (strnlen(str, STR_LEN) - 1)) { memmove(str, str + start, end - start + 1); str[end - start + 1] = '\0'; } @@ -44,7 +45,7 @@ bool string_is_number(char *value) { if (value == NULL) { return false; } - value_len = strlen(value); + value_len = strnlen(value, STR_LEN); for (i = 0; i < value_len; i++) { if (!is_digit(value[i])) { return false; @@ -58,8 +59,8 @@ char *string_replace_at(char *src, unsigned int from, unsigned int to, unsigned long src_len, rpl_len; char *dst; - src_len = strlen(src); - rpl_len = strlen(rpl); + src_len = strnlen(src, STR_LEN * STR_LEN); + rpl_len = strnlen(rpl, STR_LEN * STR_LEN); dst = malloc(src_len - (to - from) + rpl_len + 1);