diff --git a/src/config.c b/src/config.c index 2e2000e..00e1aed 100644 --- a/src/config.c +++ b/src/config.c @@ -47,14 +47,25 @@ bad: dp_log_fatal("Config: invalid color %s on line %d", value, line_idx); } -static void load_int(const char *value, int line_idx, int *out) { - // Only nonnegative numbers +static void load_int(const char *value, int line_idx, int min, int max, int *out) { + const char *p = value; + int mul = 1; + if (*p == '-') { + ++p; + mul = -1; + } + *out = 0; - for (size_t i = 0; value[i] != '\0'; i++) { - if ((value[i] < '0' && value[i] > 9) || *out > INT_MAX / 10) { + for (; *p != '\0'; p++) { + if ((*p < '0' && *p > 9) || *out >= INT_MAX / 10) { dp_log_fatal("Config: invalid number %s on line %d", value, line_idx); } - *out = *out * 10 - '0' + value[i]; + *out = *out * 10 - '0' + *p; + } + *out *= mul; + + if (*out < min || *out > max) { + dp_log_fatal("Config: number %s on line %d is out of range", value, line_idx); } } @@ -174,13 +185,9 @@ void dp_config_load(struct dp_config *config, const char *user_path) { } else if (strcmp(key, "border-color") == 0) { load_color(value, line_idx, config->border_color); } else if (strcmp(key, "border-size") == 0) { - load_int(value, line_idx, &config->border_size); + load_int(value, line_idx, 0, INT_MAX, &config->border_size); } else if (strcmp(key, "png-compression") == 0) { - load_int(value, line_idx, &config->png_compression); - if (config->png_compression > 9) { - dp_log_fatal( - "Config: invalid value %d for png-compression", config->png_compression); - } + load_int(value, line_idx, 0, 9, &config->png_compression); } else if (strcmp(key, "quick-select") == 0) { load_bool(value, line_idx, &config->quick_select); } else if (strcmp(key, "quit-key") == 0) {