Skip to content

Commit

Permalink
config: range check parsed integers
Browse files Browse the repository at this point in the history
negative values don't make sense for these config vars, so
reject them.

also reject values that are too large and would get truncated.
  • Loading branch information
N-R-K authored and cdown committed Jun 7, 2024
1 parent d41bc73 commit 4a1e026
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ int convert_bool(const char *str, void *output) {
return -EINVAL;
}

int convert_int(const char *str, void *output) {
int convert_positive_int(const char *str, void *output) {
char *end;
long val = strtol(str, &end, 10);
if (*end != '\0' || end == str) {
if (*end != '\0' || end == str || val < 0 || val > INT_MAX) {
return -EINVAL;
}
*(int *)output = (int)val;
Expand Down Expand Up @@ -264,10 +264,11 @@ static int config_apply_default_values(struct config_entry entries[],
*/
int config_setup_internal(FILE *file, struct config *cfg) {
struct config_entry entries[] = {
{"max_clips", "CM_MAX_CLIPS", &cfg->max_clips, convert_int, "1000", 0},
{"max_clips", "CM_MAX_CLIPS", &cfg->max_clips, convert_positive_int,
"1000", 0},
{"max_clips_batch", "CM_MAX_CLIPS_BATCH", &cfg->max_clips_batch,
convert_int, "100", 0},
{"oneshot", "CM_ONESHOT", &cfg->oneshot, convert_int, "0", 0},
convert_positive_int, "100", 0},
{"oneshot", "CM_ONESHOT", &cfg->oneshot, convert_positive_int, "0", 0},
{"own_clipboard", "CM_OWN_CLIPBOARD", &cfg->own_clipboard, convert_bool,
"0", 0},
{"selections", "CM_SELECTIONS", &cfg->selections, convert_selections,
Expand Down
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ enum selection_type _nonnull_
storage_atom_to_selection_type(Atom atom, struct cm_selections *sels);

int convert_bool(const char *str, void *output);
int convert_int(const char *str, void *output);
int convert_positive_int(const char *str, void *output);
int convert_ignore_window(const char *str, void *output);
int config_setup_internal(FILE *file, struct config *cfg);
void config_free(struct config *cfg);
Expand Down

0 comments on commit 4a1e026

Please sign in to comment.