From 54ce876f6ae8c0754f52bf4b8889679d45cebb1c Mon Sep 17 00:00:00 2001 From: klemek Date: Sun, 2 Nov 2025 17:35:37 +0100 Subject: [PATCH] refactor(optimize): use fixed size strings everywhere except for files --- src/args.c | 19 ++++++++++--------- src/config.h | 6 ++++++ src/config_file.c | 9 +++++---- src/config_file.h | 2 +- src/file.c | 11 +++-------- src/file.h | 4 ++-- src/forge.c | 12 +++++------- src/midi.c | 3 ++- src/state.c | 8 ++++---- src/types.h | 24 ++++++++++++------------ src/video.c | 3 ++- 11 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/args.c b/src/args.c index be35697..dab732e 100644 --- a/src/args.c +++ b/src/args.c @@ -72,7 +72,9 @@ static void invalid_value(char *arg, char *value) { print_help(EXIT_FAILURE); } -static bool is_arg(char *arg, char *ref) { return strcoll(arg, ref) == 0; } +static bool is_arg(char *arg, const char *ref) { + return strcoll(arg, ref) == 0; +} static char *split_arg_value(char *arg) { strtok(arg, "="); @@ -101,9 +103,9 @@ Parameters args_parse(int argc, char **argv) { params.output_screen = 0; params.monitor = false; params.monitor_screen = 0; - params.frag_path = DATADIR "/shaders"; - params.config_path = DATADIR "/default.cfg"; - params.state_file = "forge_saved_state.txt"; + strncpy(params.frag_path, DATADIR "/shaders", STR_LEN); + strncpy(params.config_path, DATADIR "/default.cfg", STR_LEN); + strncpy(params.state_file, "forge_saved_state.txt", STR_LEN); params.load_state = true; params.save_state = false; params.internal_size = 720; @@ -126,11 +128,11 @@ Parameters args_parse(int argc, char **argv) { } else if (is_arg(arg, "-s") || is_arg(arg, "--screen")) { params.output_screen = parse_uint(arg, value); } else if (is_arg(arg, "-f") || is_arg(arg, "--frag")) { - params.frag_path = value; + strncpy(params.frag_path, value, STR_LEN); } else if (is_arg(arg, "-c") || is_arg(arg, "--config")) { - params.config_path = value; + strncpy(params.config_path, value, STR_LEN); } else if (is_arg(arg, "-sf") || is_arg(arg, "--state-file")) { - params.state_file = value; + strncpy(params.state_file, value, STR_LEN); } else if (is_arg(arg, "-ls") || is_arg(arg, "--load-state")) { params.load_state = true; } else if (is_arg(arg, "-nls") || is_arg(arg, "--no-load-state")) { @@ -149,8 +151,7 @@ Parameters args_parse(int argc, char **argv) { log_error("maximum video input reached"); exit(EXIT_FAILURE); } - - params.video_in.values[params.video_in.length++] = value; + strncpy(params.video_in.values[params.video_in.length++], value, STR_LEN); } else if (is_arg(arg, "-vs") || is_arg(arg, "--video-size")) { params.video_size = parse_uint(arg, value); if (params.video_size == 0) { diff --git a/src/config.h b/src/config.h index 7654bd6..8213789 100644 --- a/src/config.h +++ b/src/config.h @@ -15,6 +15,12 @@ #define DATADIR "." #endif +/* STRINGS */ + +#ifndef STR_LEN +#define STR_LEN 1024 +#endif + /* TYPES */ #ifndef MAX_VIDEO diff --git a/src/config_file.c b/src/config_file.c index 98f10aa..7f9d73e 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -3,6 +3,7 @@ #include #include +#include "config.h" #include "config_file.h" #include "file.h" #include "string.h" @@ -61,7 +62,7 @@ static void parse_config_file_line(ConfigFile config, char *line) { hashmap_set(config.map, &item); } -ConfigFile config_file_read(char *path, bool free_path) { +ConfigFile config_file_read(char *path) { File file; ConfigFile config; char *line; @@ -82,7 +83,7 @@ ConfigFile config_file_read(char *path, bool free_path) { line = strtok(NULL, "\n"); } - file_free(&file, free_path); + file_free(&file); return config; } @@ -91,7 +92,7 @@ char *config_file_get_str(ConfigFile config, char *key, char *default_value) { ConfigFileItem c_key; ConfigFileItem *item; - strcpy(c_key.key, key); + strncpy(c_key.key, key, STR_LEN); item = (ConfigFileItem *)hashmap_get(config.map, &c_key); @@ -107,7 +108,7 @@ unsigned int config_file_get_int(ConfigFile config, char *key, ConfigFileItem c_key; ConfigFileItem *item; - strcpy(c_key.key, key); + strncpy(c_key.key, key, STR_LEN); item = (ConfigFileItem *)hashmap_get(config.map, &c_key); diff --git a/src/config_file.h b/src/config_file.h index 6f69d13..501929b 100644 --- a/src/config_file.h +++ b/src/config_file.h @@ -3,7 +3,7 @@ #ifndef CONFIG_FILE_H #define CONFIG_FILE_H -ConfigFile config_file_read(char *path, bool free_path); +ConfigFile config_file_read(char *path); char *config_file_get_str(ConfigFile config, char *key, char *default_value); diff --git a/src/file.c b/src/file.c index ddc11a9..d4089b4 100644 --- a/src/file.c +++ b/src/file.c @@ -66,7 +66,7 @@ void file_update(File *file) { File file_read(char *path) { File file; - file.path = path; + sprintf(file.path, path, STR_LEN); file.content = NULL; file.error = false; file.last_write = 0; @@ -75,7 +75,7 @@ File file_read(char *path) { return file; } -void file_write(char *path, ConstStringArray lines) { +void file_write(char *path, StringArray lines) { unsigned int i; FILE *file_pointer; @@ -105,9 +105,4 @@ void file_prepend(File *src, File extra) { free(old_src_content); } -void file_free(File *file, bool free_path) { - free(file->content); - if (free_path) { - free(file->path); - } -} \ No newline at end of file +void file_free(File *file) { free(file->content); } \ No newline at end of file diff --git a/src/file.h b/src/file.h index 9f68abe..daca753 100644 --- a/src/file.h +++ b/src/file.h @@ -11,8 +11,8 @@ void file_update(File *file); void file_prepend(File *src, File extra); -void file_free(File *file, bool free_path); +void file_write(char *path, StringArray lines); -void file_write(char *path, ConstStringArray lines); +void file_free(File *file); #endif /* FILE_H */ \ No newline at end of file diff --git a/src/forge.c b/src/forge.c index 6c84ead..585812c 100644 --- a/src/forge.c +++ b/src/forge.c @@ -101,11 +101,9 @@ static void hot_reload() { File read_fragment_shader_file(char *frag_path, unsigned int i) { File fragment_shader; - char *file_path; + char file_path[STR_LEN]; - file_path = malloc(sizeof(char) * 1024); - - sprintf(file_path, "%s/frag%d.glsl", frag_path, i); + snprintf(file_path, STR_LEN, "%s/frag%d.glsl", frag_path, i); fragment_shader = file_read(file_path); if (fragment_shader.error) { exit(EXIT_FAILURE); @@ -134,10 +132,10 @@ static void free_files(unsigned int frag_count) { unsigned int i; for (i = 0; i < frag_count; i++) { - file_free(&fragment_shaders.values[i], true); + file_free(&fragment_shaders.values[i]); } - file_free(&common_shader_code, true); + file_free(&common_shader_code); } static void init_inputs(StringArray video_in, unsigned int video_size) { @@ -237,7 +235,7 @@ void forge_run(Parameters params) { context->stop = false; - config = config_file_read(params.config_path, false); + config = config_file_read(params.config_path); frag_count = config_file_get_int(config, "FRAG_COUNT", 1); in_count = config_file_get_int(config, "IN_COUNT", 0); diff --git a/src/midi.c b/src/midi.c index 1b37e46..94b5a14 100644 --- a/src/midi.c +++ b/src/midi.c @@ -1,13 +1,14 @@ #include #include +#include "config.h" #include "log.h" #include "types.h" MidiDevice midi_open(char *name) { MidiDevice device; - device.name = name; + strncpy(device.name, name, STR_LEN); device.input = NULL; device.output = NULL; diff --git a/src/state.c b/src/state.c index 35abf99..ec7c969 100644 --- a/src/state.c +++ b/src/state.c @@ -319,13 +319,13 @@ bool state_background_midi_write(SharedContext *context, return false; } -void state_load(SharedContext *context, StateConfig state_config, - char *state_file) { +static void state_load(SharedContext *context, StateConfig state_config, + char *state_file) { ConfigFile saved_state; char key[100]; unsigned int i; - saved_state = config_file_read(state_file, false); + saved_state = config_file_read(state_file); tempo_set(&context->tempo, config_file_get_int(saved_state, "tempo", context->tempo.tempo)); @@ -402,7 +402,7 @@ void state_randomize(SharedContext *context, StateConfig state_config) { void state_save(SharedContext *context, StateConfig state_config, char *state_file) { - ConstStringArray lines; + StringArray lines; unsigned int i; log_info("Saving state to '%s'...", state_file); diff --git a/src/types.h b/src/types.h index 242c05d..63e01d0 100644 --- a/src/types.h +++ b/src/types.h @@ -21,14 +21,13 @@ } X typedef ARRAY(UintArray, unsigned int); -typedef ARRAY(StringArray, char *); typedef ARRAY(Vec3Array, vec3); typedef ARRAY(GLuintArray, GLuint); -typedef struct ConstStringArray { - char values[ARRAY_SIZE][1000]; +typedef struct StringArray { + char values[ARRAY_SIZE][STR_LEN]; unsigned int length; -} ConstStringArray; +} StringArray; typedef struct Parameters { bool hot_reload; @@ -36,9 +35,9 @@ typedef struct Parameters { unsigned int output_screen; bool monitor; unsigned int monitor_screen; - char *frag_path; - char *config_path; - char *state_file; + char frag_path[STR_LEN]; + char config_path[STR_LEN]; + char state_file[STR_LEN]; bool load_state; bool save_state; unsigned int internal_size; @@ -54,8 +53,9 @@ typedef struct Vertex { } Vertex; typedef struct File { - char *path; + char path[STR_LEN]; char *content; + unsigned int length; bool error; time_t last_write; } File; @@ -117,7 +117,7 @@ typedef struct ShaderProgram { } ShaderProgram; typedef struct VideoCapture { - char *name; + char name[STR_LEN]; bool error; int fd; int exp_fd; @@ -198,13 +198,13 @@ typedef struct ConfigFile { } ConfigFile; typedef struct ConfigFileItem { - char key[256]; - char value[2048]; + char key[STR_LEN]; + char value[STR_LEN]; } ConfigFileItem; typedef struct MidiDevice { bool error; - char *name; + char name[STR_LEN]; snd_rawmidi_t *input; snd_rawmidi_t *output; } MidiDevice; diff --git a/src/video.c b/src/video.c index 5e6e318..0758b85 100644 --- a/src/video.c +++ b/src/video.c @@ -8,6 +8,7 @@ #include #include +#include "config.h" #include "timer.h" #include "types.h" #include "video.h" @@ -66,7 +67,7 @@ static void ioctl_error(VideoCapture *video_capture, const char *operation, static VideoCapture open_device(char *name) { VideoCapture video_capture; - video_capture.name = name; + strncpy(video_capture.name, name, STR_LEN); video_capture.error = false; video_capture.fd = -1;