refactor: sprintf -> snprint, strncpy -> strlcpy
This commit is contained in:
+1
-1
@@ -99,7 +99,7 @@ make -f Makefile.dev release-arch
|
||||
- [x] `forge_project.cfg`
|
||||
- [x] Define frag prefix in config
|
||||
- [x] Use custom `#include xxx.glsl` preprocessor
|
||||
- [ ] Use snprintf isntead of sprintf (and strlcpy instand of strncpy)
|
||||
- [x] Use snprintf isntead of sprintf (and strlcpy instand of strncpy)
|
||||
- [ ] Pass "heavy" struct as pointer to avoid stack overload
|
||||
- [x] Clean and sort args
|
||||
- [x] `--auto-random` / `--no-auto-random`
|
||||
|
||||
+7
-7
@@ -106,8 +106,8 @@ Parameters args_parse(int argc, char **argv) {
|
||||
char *arg;
|
||||
char *value;
|
||||
|
||||
strncpy(params.project_path, DATADIR "/default", STR_LEN);
|
||||
strncpy(params.config_file, "forge_project.cfg", STR_LEN);
|
||||
strlcpy(params.project_path, DATADIR "/default", STR_LEN);
|
||||
strlcpy(params.config_file, "forge_project.cfg", STR_LEN);
|
||||
params.hot_reload = false;
|
||||
params.output = true;
|
||||
params.output_screen = 0;
|
||||
@@ -120,7 +120,7 @@ Parameters args_parse(int argc, char **argv) {
|
||||
params.video_in.length = 0;
|
||||
params.video_size = 0;
|
||||
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.save_state = true;
|
||||
params.trace_midi = false;
|
||||
@@ -135,9 +135,9 @@ Parameters args_parse(int argc, char **argv) {
|
||||
puts(PACKAGE " " VERSION);
|
||||
exit(EXIT_SUCCESS);
|
||||
} 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")) {
|
||||
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")) {
|
||||
params.hot_reload = true;
|
||||
} else if (is_arg(arg, "-s") || is_arg(arg, "--screen")) {
|
||||
@@ -166,7 +166,7 @@ Parameters args_parse(int argc, char **argv) {
|
||||
log_error("maximum video input reached");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
strncpy(params.video_in.values[params.video_in.length++], value, STR_LEN);
|
||||
strlcpy(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) {
|
||||
@@ -178,7 +178,7 @@ Parameters args_parse(int argc, char **argv) {
|
||||
invalid_value(arg, value);
|
||||
}
|
||||
} 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")) {
|
||||
params.load_state = true;
|
||||
} else if (is_arg(arg, "-nls") || is_arg(arg, "--no-load-state")) {
|
||||
|
||||
+4
-4
@@ -52,11 +52,11 @@ static void parse_config_file_line(ConfigFile config, char *line) {
|
||||
key_size = equal_pos - line;
|
||||
value_size = size - key_size - 1;
|
||||
|
||||
strncpy(item.key, line, key_size);
|
||||
strlcpy(item.key, line, key_size);
|
||||
item.key[key_size] = '\0';
|
||||
|
||||
if (value_size > 0) {
|
||||
strncpy(item.value, line + key_size + 1, value_size);
|
||||
strlcpy(item.value, line + key_size + 1, value_size);
|
||||
item.value[value_size] = '\0';
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ char *config_file_get_str(ConfigFile config, char *key, char *default_value) {
|
||||
ConfigFileItem c_key;
|
||||
ConfigFileItem *item;
|
||||
|
||||
strncpy(c_key.key, key, STR_LEN);
|
||||
strlcpy(c_key.key, key, STR_LEN);
|
||||
|
||||
item = (ConfigFileItem *)hashmap_get(config.map, &c_key);
|
||||
|
||||
@@ -109,7 +109,7 @@ unsigned int config_file_get_int(ConfigFile config, char *key,
|
||||
ConfigFileItem c_key;
|
||||
ConfigFileItem *item;
|
||||
|
||||
strncpy(c_key.key, key, STR_LEN);
|
||||
strlcpy(c_key.key, key, STR_LEN);
|
||||
|
||||
item = (ConfigFileItem *)hashmap_get(config.map, &c_key);
|
||||
|
||||
|
||||
+1
-1
@@ -70,7 +70,7 @@ bool file_update(File *file) {
|
||||
File file_read(char *path) {
|
||||
File file;
|
||||
|
||||
strncpy(file.path, path, STR_LEN);
|
||||
strlcpy(file.path, path, STR_LEN);
|
||||
file.content = NULL;
|
||||
file.error = false;
|
||||
file.last_write = 0;
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@
|
||||
MidiDevice midi_open(char *name) {
|
||||
MidiDevice device;
|
||||
|
||||
strncpy(device.name, name, STR_LEN);
|
||||
strlcpy(device.name, name, STR_LEN);
|
||||
device.input = NULL;
|
||||
device.output = NULL;
|
||||
|
||||
|
||||
+12
-12
@@ -120,7 +120,7 @@ static void init_input(ShaderProgram *program, ConfigFile config,
|
||||
|
||||
for (i = 0; i < program->in_count; i++) {
|
||||
if (i < inputs.length && !inputs.values[i].error) {
|
||||
sprintf(name, "IN_%d_OUT", i + 1);
|
||||
snprintf(name, STR_LEN, "IN_%d_OUT", i + 1);
|
||||
tex_i = config_file_get_int(config, name, 0);
|
||||
link_input_to_texture(program, &inputs.values[i], tex_i);
|
||||
} else {
|
||||
@@ -143,7 +143,7 @@ static void init_framebuffers(ShaderProgram *program, ConfigFile config) {
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, program->frame_buffers[i]);
|
||||
|
||||
sprintf(name, "FRAG_%d_OUT", i + 1);
|
||||
snprintf(name, STR_LEN, "FRAG_%d_OUT", i + 1);
|
||||
tex_i = config_file_get_int(config, name, 0);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
||||
program->textures[tex_i], 0);
|
||||
@@ -279,7 +279,7 @@ static void init_single_program(ShaderProgram *program, unsigned int i,
|
||||
prefix = config_file_get_str(config, "UNIFORM_IN_RESOLUTION_PREFIX",
|
||||
"iInputResolution");
|
||||
for (j = 0; j < program->in_count; j++) {
|
||||
sprintf(name, "%s%d", prefix, j + 1);
|
||||
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
||||
program->iinres_locations[i * program->in_count + j] =
|
||||
glGetUniformLocation(program->programs[i], name);
|
||||
}
|
||||
@@ -287,37 +287,37 @@ static void init_single_program(ShaderProgram *program, unsigned int i,
|
||||
prefix =
|
||||
config_file_get_str(config, "UNIFORM_IN_FORMAT_PREFIX", "iInputFormat");
|
||||
for (j = 0; j < program->in_count; j++) {
|
||||
sprintf(name, "%s%d", prefix, j + 1);
|
||||
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
||||
program->iinfmt_locations[i * program->in_count + j] =
|
||||
glGetUniformLocation(program->programs[i], name);
|
||||
}
|
||||
|
||||
prefix = config_file_get_str(config, "UNIFORM_IN_FPS_PREFIX", "iInputFPS");
|
||||
for (j = 0; j < program->in_count; j++) {
|
||||
sprintf(name, "%s%d", prefix, j + 1);
|
||||
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
||||
program->iinfps_locations[i * program->in_count + j] =
|
||||
glGetUniformLocation(program->programs[i], name);
|
||||
}
|
||||
|
||||
prefix = config_file_get_str(config, "UNIFORM_SEED_PREFIX", "iSeed");
|
||||
for (j = 0; j < program->frag_count; j++) {
|
||||
sprintf(name, "%s%d", prefix, j + 1);
|
||||
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
||||
program->iseed_locations[i * program->frag_count + j] =
|
||||
glGetUniformLocation(program->programs[i], name);
|
||||
}
|
||||
|
||||
prefix = config_file_get_str(config, "UNIFORM_STATE_PREFIX", "iState");
|
||||
for (j = 0; j < program->frag_count; j++) {
|
||||
sprintf(name, "%s%d", prefix, j + 1);
|
||||
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
||||
program->istate_locations[i * program->frag_count + j] =
|
||||
glGetUniformLocation(program->programs[i], name);
|
||||
}
|
||||
|
||||
for (j = 0; j < program->sub_type_count; j++) {
|
||||
sprintf(name, "SUB_%d_PREFIX", j + 1);
|
||||
snprintf(name, STR_LEN, "SUB_%d_PREFIX", j + 1);
|
||||
prefix = config_file_get_str(config, name, 0);
|
||||
for (k = 0; k < program->sub_variant_count; k++) {
|
||||
sprintf(name, "%s%d", prefix, k + 1);
|
||||
snprintf(name, STR_LEN, "%s%d", prefix, k + 1);
|
||||
program->sub_locations[i * program->sub_variant_count *
|
||||
program->sub_type_count +
|
||||
j * program->sub_variant_count + k] =
|
||||
@@ -327,7 +327,7 @@ static void init_single_program(ShaderProgram *program, unsigned int i,
|
||||
|
||||
prefix = config_file_get_str(config, "UNIFORM_ACTIVE_PREFIX", "iActive");
|
||||
for (j = 0; j < program->active_count; j++) {
|
||||
sprintf(name, "%s%d", prefix, j + 1);
|
||||
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
||||
program->iactive_locations[i * program->active_count + j] =
|
||||
glGetUniformLocation(program->programs[i], name);
|
||||
}
|
||||
@@ -347,7 +347,7 @@ static void init_single_program(ShaderProgram *program, unsigned int i,
|
||||
index2 = 0;
|
||||
for (j = 0; j < state_config.midi_active_counts.length; j++) {
|
||||
for (k = 0; k < state_config.midi_active_counts.values[j]; k++) {
|
||||
sprintf(name, "%s%d_%d", prefix, j + 1, k + 1);
|
||||
snprintf(name, STR_LEN, "%s%d_%d", prefix, j + 1, k + 1);
|
||||
program->imidi_locations[i * program->midi_lengths.length + index2++] =
|
||||
glGetUniformLocation(program->programs[i], name);
|
||||
}
|
||||
@@ -356,7 +356,7 @@ static void init_single_program(ShaderProgram *program, unsigned int i,
|
||||
// create texX uniforms pointer
|
||||
prefix = config_file_get_str(config, "UNIFORM_TEX_PREFIX", "iTex");
|
||||
for (j = 0; j < program->tex_count; j++) {
|
||||
sprintf(name, "%s%d", prefix, j);
|
||||
snprintf(name, STR_LEN, "%s%d", prefix, j);
|
||||
program->textures_locations[i * program->tex_count + j] =
|
||||
glGetUniformLocation(program->programs[i], name);
|
||||
}
|
||||
|
||||
+33
-31
@@ -21,7 +21,7 @@ StateConfig state_parse_config(ConfigFile config) {
|
||||
config_file_get_int(config, "SELECT_PAGE_COUNT", 0);
|
||||
|
||||
for (i = 0; i < state_config.select_page_codes.length; i++) {
|
||||
sprintf(name, "SELECT_PAGE_%d", i + 1);
|
||||
snprintf(name, STR_LEN, "SELECT_PAGE_%d", i + 1);
|
||||
state_config.select_page_codes.values[i] =
|
||||
config_file_get_int(config, name, UNSET_MIDI_CODE);
|
||||
}
|
||||
@@ -30,7 +30,7 @@ StateConfig state_parse_config(ConfigFile config) {
|
||||
config_file_get_int(config, "SELECT_ITEM_COUNT", 0);
|
||||
|
||||
for (i = 0; i < state_config.select_item_codes.length; i++) {
|
||||
sprintf(name, "SELECT_ITEM_%d", i + 1);
|
||||
snprintf(name, STR_LEN, "SELECT_ITEM_%d", i + 1);
|
||||
state_config.select_item_codes.values[i] =
|
||||
config_file_get_int(config, name, UNSET_MIDI_CODE);
|
||||
}
|
||||
@@ -42,7 +42,7 @@ StateConfig state_parse_config(ConfigFile config) {
|
||||
config_file_get_int(config, "FRAG_COUNT", 1);
|
||||
|
||||
for (i = 0; i < state_config.select_frag_codes.length; i++) {
|
||||
sprintf(name, "SELECT_FRAG_%d", i + 1);
|
||||
snprintf(name, STR_LEN, "SELECT_FRAG_%d", i + 1);
|
||||
state_config.select_frag_codes.values[i] =
|
||||
config_file_get_int(config, name, UNSET_MIDI_CODE);
|
||||
}
|
||||
@@ -55,7 +55,7 @@ StateConfig state_parse_config(ConfigFile config) {
|
||||
|
||||
count = 0;
|
||||
for (i = 0; i < state_config.midi_active_counts.length; i++) {
|
||||
sprintf(name, "MIDI_%d_ACTIVE_COUNT", i + 1);
|
||||
snprintf(name, STR_LEN, "MIDI_%d_ACTIVE_COUNT", i + 1);
|
||||
state_config.midi_active_counts.values[i] =
|
||||
config_file_get_int(config, name, 1);
|
||||
state_config.midi_active_offsets.values[i] = count;
|
||||
@@ -66,7 +66,7 @@ StateConfig state_parse_config(ConfigFile config) {
|
||||
|
||||
for (i = 0; i < state_config.midi_active_counts.length; i++) {
|
||||
for (j = 0; j < state_config.midi_active_counts.values[i]; j++) {
|
||||
sprintf(name, "MIDI_%d_ACTIVE_%d", i + 1, j + 1);
|
||||
snprintf(name, STR_LEN, "MIDI_%d_ACTIVE_%d", i + 1, j + 1);
|
||||
state_config.midi_active_codes
|
||||
.values[state_config.midi_active_offsets.values[i] + j] =
|
||||
config_file_get_int(config, name, UNSET_MIDI_CODE);
|
||||
@@ -76,7 +76,7 @@ StateConfig state_parse_config(ConfigFile config) {
|
||||
count = 0;
|
||||
offset = 0;
|
||||
for (i = 0; i < state_config.midi_counts.length; i++) {
|
||||
sprintf(name, "MIDI_%d_COUNT", i + 1);
|
||||
snprintf(name, STR_LEN, "MIDI_%d_COUNT", i + 1);
|
||||
state_config.midi_counts.values[i] = config_file_get_int(config, name, 0);
|
||||
state_config.midi_offsets.values[i] = count;
|
||||
state_config.values_offsets.values[i] = offset;
|
||||
@@ -90,15 +90,15 @@ StateConfig state_parse_config(ConfigFile config) {
|
||||
for (i = 0; i < state_config.midi_counts.length; i++) {
|
||||
offset = state_config.midi_offsets.values[i];
|
||||
for (j = 0; j < state_config.midi_counts.values[i]; j++) {
|
||||
sprintf(name, "MIDI_%d_%d_X", i + 1, j + 1);
|
||||
snprintf(name, STR_LEN, "MIDI_%d_%d_X", i + 1, j + 1);
|
||||
state_config.midi_codes.values[(offset + j) * 3] =
|
||||
config_file_get_int(config, name, UNSET_MIDI_CODE);
|
||||
|
||||
sprintf(name, "MIDI_%d_%d_Y", i + 1, j + 1);
|
||||
snprintf(name, STR_LEN, "MIDI_%d_%d_Y", i + 1, j + 1);
|
||||
state_config.midi_codes.values[(offset + j) * 3 + 1] =
|
||||
config_file_get_int(config, name, UNSET_MIDI_CODE);
|
||||
|
||||
sprintf(name, "MIDI_%d_%d_Z", i + 1, j + 1);
|
||||
snprintf(name, STR_LEN, "MIDI_%d_%d_Z", i + 1, j + 1);
|
||||
state_config.midi_codes.values[(offset + j) * 3 + 2] =
|
||||
config_file_get_int(config, name, UNSET_MIDI_CODE);
|
||||
}
|
||||
@@ -108,7 +108,7 @@ StateConfig state_parse_config(ConfigFile config) {
|
||||
config_file_get_int(config, "FADER_COUNT", 0);
|
||||
|
||||
for (i = 0; i < state_config.fader_codes.length; i++) {
|
||||
sprintf(name, "FADER_%d", i + 1);
|
||||
snprintf(name, STR_LEN, "FADER_%d", i + 1);
|
||||
state_config.fader_codes.values[i] =
|
||||
config_file_get_int(config, name, UNSET_MIDI_CODE);
|
||||
}
|
||||
@@ -338,26 +338,26 @@ static void state_load(SharedContext *context, StateConfig state_config,
|
||||
context->selected = config_file_get_int(saved_state, "selected", 0);
|
||||
|
||||
for (i = 0; i < context->state.length; i++) {
|
||||
sprintf(key, "seed_%d", i);
|
||||
snprintf(key, STR_LEN, "seed_%d", i);
|
||||
context->seeds[i] =
|
||||
config_file_get_int(saved_state, key, context->seeds[i]);
|
||||
sprintf(key, "state_%d", i);
|
||||
snprintf(key, STR_LEN, "state_%d", i);
|
||||
context->state.values[i] = config_file_get_int(saved_state, key, 0);
|
||||
}
|
||||
|
||||
for (i = 0; i < state_config.midi_active_counts.length; i++) {
|
||||
sprintf(key, "active_%d", i);
|
||||
snprintf(key, STR_LEN, "active_%d", i);
|
||||
context->active[i] = config_file_get_int(saved_state, key, 0);
|
||||
}
|
||||
|
||||
for (i = 0; i < state_config.midi_codes.length; i++) {
|
||||
sprintf(key, "value_%d_x", i);
|
||||
snprintf(key, STR_LEN, "value_%d_x", i);
|
||||
context->values[i][0] =
|
||||
(float)config_file_get_int(saved_state, key, 0) / MIDI_MAX;
|
||||
sprintf(key, "value_%d_y", i);
|
||||
snprintf(key, STR_LEN, "value_%d_y", i);
|
||||
context->values[i][1] =
|
||||
(float)config_file_get_int(saved_state, key, 0) / MIDI_MAX;
|
||||
sprintf(key, "value_%d_z", i);
|
||||
snprintf(key, STR_LEN, "value_%d_z", i);
|
||||
context->values[i][2] =
|
||||
(float)config_file_get_int(saved_state, key, 0) / MIDI_MAX;
|
||||
}
|
||||
@@ -416,29 +416,31 @@ void state_save(SharedContext *context, StateConfig state_config,
|
||||
|
||||
lines.length = 0;
|
||||
|
||||
sprintf(lines.values[lines.length++], "tempo=%d",
|
||||
(unsigned int)context->tempo.tempo);
|
||||
sprintf(lines.values[lines.length++], "page=%d", context->page);
|
||||
sprintf(lines.values[lines.length++], "selected=%d", context->selected);
|
||||
snprintf(lines.values[lines.length++], STR_LEN, "tempo=%d",
|
||||
(unsigned int)context->tempo.tempo);
|
||||
snprintf(lines.values[lines.length++], STR_LEN, "page=%d", context->page);
|
||||
snprintf(lines.values[lines.length++], STR_LEN, "selected=%d",
|
||||
context->selected);
|
||||
|
||||
for (i = 0; i < context->state.length; i++) {
|
||||
sprintf(lines.values[lines.length++], "seed_%d=%d", i, context->seeds[i]);
|
||||
sprintf(lines.values[lines.length++], "state_%d=%d", i,
|
||||
context->state.values[i]);
|
||||
snprintf(lines.values[lines.length++], STR_LEN, "seed_%d=%d", i,
|
||||
context->seeds[i]);
|
||||
snprintf(lines.values[lines.length++], STR_LEN, "state_%d=%d", i,
|
||||
context->state.values[i]);
|
||||
}
|
||||
|
||||
for (i = 0; i < state_config.midi_active_counts.length; i++) {
|
||||
sprintf(lines.values[lines.length++], "active_%d=%d", i,
|
||||
context->active[i]);
|
||||
snprintf(lines.values[lines.length++], STR_LEN, "active_%d=%d", i,
|
||||
context->active[i]);
|
||||
}
|
||||
|
||||
for (i = 0; i < state_config.midi_codes.length; i++) {
|
||||
sprintf(lines.values[lines.length++], "value_%d_x=%d", i,
|
||||
(unsigned int)(context->values[i][0] * MIDI_MAX));
|
||||
sprintf(lines.values[lines.length++], "value_%d_y=%d", i,
|
||||
(unsigned int)(context->values[i][1] * MIDI_MAX));
|
||||
sprintf(lines.values[lines.length++], "value_%d_z=%d", i,
|
||||
(unsigned int)(context->values[i][2] * MIDI_MAX));
|
||||
snprintf(lines.values[lines.length++], STR_LEN, "value_%d_x=%d", i,
|
||||
(unsigned int)(context->values[i][0] * MIDI_MAX));
|
||||
snprintf(lines.values[lines.length++], STR_LEN, "value_%d_y=%d", i,
|
||||
(unsigned int)(context->values[i][1] * MIDI_MAX));
|
||||
snprintf(lines.values[lines.length++], STR_LEN, "value_%d_z=%d", i,
|
||||
(unsigned int)(context->values[i][2] * MIDI_MAX));
|
||||
}
|
||||
|
||||
file_write(state_file, lines);
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ static void ioctl_error(VideoCapture *video_capture, const char *operation,
|
||||
static VideoCapture open_device(char *name) {
|
||||
VideoCapture video_capture;
|
||||
|
||||
strncpy(video_capture.name, name, STR_LEN);
|
||||
strlcpy(video_capture.name, name, STR_LEN);
|
||||
video_capture.error = false;
|
||||
video_capture.fd = -1;
|
||||
video_capture.exp_fd = -1;
|
||||
|
||||
Reference in New Issue
Block a user