refactor: strncpy -> strlcpy

This commit is contained in:
2025-11-10 14:13:27 +01:00
parent f5bf71f535
commit 352f97c2db
7 changed files with 22 additions and 17 deletions
+8 -7
View File
@@ -1,3 +1,4 @@
#include <bsd/string.h>
#include <limits.h> #include <limits.h>
#include <log.h> #include <log.h>
#include <stdbool.h> #include <stdbool.h>
@@ -105,8 +106,8 @@ void args_parse(Parameters *params, int argc, char **argv) {
char *arg; char *arg;
char *value; char *value;
strncpy(params->project_path, DATADIR "/default", STR_LEN); strlcpy(params->project_path, DATADIR "/default", STR_LEN);
strncpy(params->config_file, "forge_project.cfg", STR_LEN); strlcpy(params->config_file, "forge_project.cfg", STR_LEN);
params->hot_reload = false; params->hot_reload = false;
params->output = true; params->output = true;
params->output_screen = 0; params->output_screen = 0;
@@ -119,7 +120,7 @@ void args_parse(Parameters *params, int argc, char **argv) {
params->video_in.length = 0; params->video_in.length = 0;
params->video_size = 0; params->video_size = 0;
params->internal_size = 720; params->internal_size = 720;
strncpy(params->state_file, "forge_saved_state.txt", STR_LEN); strlcpy(params->state_file, "forge_saved_state.txt", STR_LEN);
params->load_state = true; params->load_state = true;
params->save_state = true; params->save_state = true;
params->trace_midi = false; params->trace_midi = false;
@@ -134,9 +135,9 @@ void args_parse(Parameters *params, int argc, char **argv) {
puts(PACKAGE " " VERSION); puts(PACKAGE " " VERSION);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else if (is_arg(arg, "-p") || is_arg(arg, "--project")) { } else if (is_arg(arg, "-p") || is_arg(arg, "--project")) {
strncpy(params->project_path, value, STR_LEN); strlcpy(params->project_path, value, STR_LEN);
} else if (is_arg(arg, "-c") || is_arg(arg, "--config")) { } else if (is_arg(arg, "-c") || is_arg(arg, "--config")) {
strncpy(params->config_file, value, STR_LEN); strlcpy(params->config_file, value, STR_LEN);
} else if (is_arg(arg, "-hr") || is_arg(arg, "--hot-reload")) { } else if (is_arg(arg, "-hr") || is_arg(arg, "--hot-reload")) {
params->hot_reload = true; params->hot_reload = true;
} else if (is_arg(arg, "-s") || is_arg(arg, "--screen")) { } else if (is_arg(arg, "-s") || is_arg(arg, "--screen")) {
@@ -165,7 +166,7 @@ void args_parse(Parameters *params, int argc, char **argv) {
log_error("maximum video input reached"); log_error("maximum video input reached");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
strncpy(params->video_in.values[params->video_in.length++], value, strlcpy(params->video_in.values[params->video_in.length++], value,
STR_LEN); STR_LEN);
} else if (is_arg(arg, "-vs") || is_arg(arg, "--video-size")) { } else if (is_arg(arg, "-vs") || is_arg(arg, "--video-size")) {
params->video_size = parse_uint(arg, value); params->video_size = parse_uint(arg, value);
@@ -178,7 +179,7 @@ void args_parse(Parameters *params, int argc, char **argv) {
invalid_value(arg, value); invalid_value(arg, value);
} }
} else if (is_arg(arg, "-sf") || is_arg(arg, "--state-file")) { } else if (is_arg(arg, "-sf") || is_arg(arg, "--state-file")) {
strncpy(params->state_file, value, STR_LEN); strlcpy(params->state_file, value, STR_LEN);
} else if (is_arg(arg, "-ls") || is_arg(arg, "--load-state")) { } else if (is_arg(arg, "-ls") || is_arg(arg, "--load-state")) {
params->load_state = true; params->load_state = true;
} else if (is_arg(arg, "-nls") || is_arg(arg, "--no-load-state")) { } else if (is_arg(arg, "-nls") || is_arg(arg, "--no-load-state")) {
+5 -4
View File
@@ -1,3 +1,4 @@
#include <bsd/string.h>
#include <hashmap.h> #include <hashmap.h>
#include <log.h> #include <log.h>
#include <stdlib.h> #include <stdlib.h>
@@ -52,11 +53,11 @@ static void parse_config_file_line(ConfigFile *config, char *line) {
key_size = equal_pos - line; key_size = equal_pos - line;
value_size = size - key_size - 1; value_size = size - key_size - 1;
strncpy(item.key, line, key_size); strlcpy(item.key, line, key_size + 1);
item.key[key_size] = '\0'; item.key[key_size] = '\0';
if (value_size > 0) { if (value_size > 0) {
strncpy(item.value, line + key_size + 1, value_size); strlcpy(item.value, line + key_size + 1, value_size + 1);
item.value[value_size] = '\0'; item.value[value_size] = '\0';
} }
@@ -90,7 +91,7 @@ char *config_file_get_str(ConfigFile *config, char *key, char *default_value) {
ConfigFileItem c_key; ConfigFileItem c_key;
ConfigFileItem *item; ConfigFileItem *item;
strncpy(c_key.key, key, STR_LEN); strlcpy(c_key.key, key, STR_LEN);
item = (ConfigFileItem *)hashmap_get(config->map, &c_key); item = (ConfigFileItem *)hashmap_get(config->map, &c_key);
@@ -106,7 +107,7 @@ unsigned int config_file_get_int(ConfigFile *config, char *key,
ConfigFileItem c_key; ConfigFileItem c_key;
ConfigFileItem *item; ConfigFileItem *item;
strncpy(c_key.key, key, STR_LEN); strlcpy(c_key.key, key, STR_LEN);
item = (ConfigFileItem *)hashmap_get(config->map, &c_key); item = (ConfigFileItem *)hashmap_get(config->map, &c_key);
+2 -1
View File
@@ -1,3 +1,4 @@
#include <bsd/string.h>
#include <log.h> #include <log.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@@ -68,7 +69,7 @@ bool file_update(File *file) {
} }
void file_read(File *file, char *path) { void file_read(File *file, char *path) {
strncpy(file->path, path, STR_LEN); strlcpy(file->path, path, STR_LEN);
file->content = NULL; file->content = NULL;
file->error = false; file->error = false;
file->last_write = 0; file->last_write = 0;
+2 -1
View File
@@ -1,5 +1,6 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
#include <bsd/string.h>
#include "types.h" #include "types.h"
@@ -7,7 +8,7 @@
#include "log.h" #include "log.h"
void midi_open(MidiDevice *device, char *name) { void midi_open(MidiDevice *device, char *name) {
strncpy(device->name, name, STR_LEN); strlcpy(device->name, name, STR_LEN);
device->input = NULL; device->input = NULL;
device->output = NULL; device->output = NULL;
+1 -1
View File
@@ -80,7 +80,7 @@ void project_init(Project *project, char *project_path, char *config_file) {
char *frag_prefix; char *frag_prefix;
unsigned int i; unsigned int i;
strncpy(project->path, project_path, STR_LEN); strlcpy(project->path, project_path, STR_LEN);
snprintf(config_path, STR_LEN, "%s/%s", project_path, config_file); snprintf(config_path, STR_LEN, "%s/%s", project_path, config_file);
+2 -2
View File
@@ -63,8 +63,8 @@ char *string_replace_at(char *src, unsigned int from, unsigned int to,
dst = malloc(src_len - (to - from) + rpl_len + 1); dst = malloc(src_len - (to - from) + rpl_len + 1);
strncpy(dst, src, from); strlcpy(dst, src, from + 1);
strncpy(dst + from, rpl, rpl_len); strlcpy(dst + from, rpl, rpl_len + 1);
strlcpy(dst + from + rpl_len, src + to, src_len - to + 1); strlcpy(dst + from + rpl_len, src + to, src_len - to + 1);
return dst; return dst;
+2 -1
View File
@@ -1,3 +1,4 @@
#include <bsd/string.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <linux/videodev2.h> #include <linux/videodev2.h>
@@ -66,7 +67,7 @@ static void ioctl_error(VideoCapture *video_capture, const char *operation,
} }
static void open_device(VideoCapture *video_capture, char *name) { static void open_device(VideoCapture *video_capture, char *name) {
strncpy(video_capture->name, name, STR_LEN); strlcpy(video_capture->name, name, STR_LEN);
video_capture->error = false; video_capture->error = false;
video_capture->fd = -1; video_capture->fd = -1;
video_capture->exp_fd = -1; video_capture->exp_fd = -1;