diff options
| author | elbachir-one <bachiralfa@gmail.com> | 2025-05-29 19:36:31 +0100 |
|---|---|---|
| committer | elbachir-one <bachiralfa@gmail.com> | 2025-05-30 16:37:38 +0100 |
| commit | fa46156c39c427112658887748966e1a84e5549c (patch) | |
| tree | 56a435c530633f4b9a0132a1b512f9a9a9d50346 /src/parser.c | |
| parent | 51fcaf2050eaae19253baef0cdc7e40b6b6c29fd (diff) | |
Improved function logic for clarity and maintainability
Fixed
- Corrected double increment bug in `should_floatn`
- Ensured `should_float` entries are properly parsed and quoted strings handled
- Prevented out-of-bounds access in float parsing
- Added fallback error if no config file is found
- Improved error messages for better diagnostics
- Replaced unnecessary heap allocation with stack-safe operations
Diffstat (limited to 'src/parser.c')
| -rw-r--r-- | src/parser.c | 90 |
1 files changed, 43 insertions, 47 deletions
diff --git a/src/parser.c b/src/parser.c index 6bf1294..bef8852 100644 --- a/src/parser.c +++ b/src/parser.c @@ -132,9 +132,8 @@ int parser(Config *cfg) return -1; } - // check $XDG_CONFIG_HOME/sxwmrc, then $XDG_CONFIG_HOME/sxwm/sxwmrc, then $HOME/.config/sxwmrc + // Determine config file path const char *xdg_config_home = getenv("XDG_CONFIG_HOME"); - if (xdg_config_home) { snprintf(path, sizeof path, "%s/sxwmrc", xdg_config_home); if (access(path, R_OK) == 0) { @@ -157,7 +156,11 @@ int parser(Config *cfg) goto found; } -found:; // label followed by declaration is a C23 extension + // Nothing found + fprintf(stderr, "sxwmrc: no configuration file found\n"); + return -1; + +found:; FILE *f = fopen(path, "r"); if (!f) { fprintf(stderr, "sxwmrc: cannot open %s\n", path); @@ -168,8 +171,14 @@ found:; // label followed by declaration is a C23 extension int lineno = 0; int should_floatn = 0; + // Initialize should_float matrix for (int j = 0; j < 256; j++) { - cfg->should_float[j] = calloc(256, sizeof(char *)); // allocate array of 256 strings + cfg->should_float[j] = calloc(256, sizeof(char *)); + if (!cfg->should_float[j]) { + fprintf(stderr, "calloc failed\n"); + fclose(f); + return -1; + } } while (fgets(line, sizeof line, f)) { @@ -184,6 +193,7 @@ found:; // label followed by declaration is a C23 extension fprintf(stderr, "sxwmrc:%d: missing ':'\n", lineno); continue; } + *sep = '\0'; char *key = strip(s); char *rest = strip(sep + 1); @@ -213,7 +223,7 @@ found:; // label followed by declaration is a C23 extension cfg->border_swap_col = parse_col(rest); } else if (!strcmp(key, "master_width")) { - float mf = atoi(rest) / 100.0f; + float mf = (float)atoi(rest) / 100.0f; for (int i = 0; i < MAX_MONITORS; i++) { cfg->master_width[i] = mf; } @@ -228,62 +238,46 @@ found:; // label followed by declaration is a C23 extension cfg->snap_distance = atoi(rest); } else if (!strcmp(key, "should_float")) { - // should_float: binary --arg,binary2 parameter --arg,binary3 - if (should_floatn >= 256) { fprintf(stderr, "sxwmrc:%d: too many should_float entries\n", lineno); continue; } - char *win = strip(rest); - - // remove comments - char *nocom = malloc(strlen(win) + 1); - char *comment = strchr(win, '#'); - if (comment) { - strncpy(nocom, win, comment - win); - nocom[comment - win] = '\0'; - } else { - strcpy(nocom, win); - } - - char *final = strip(nocom); - - char *comma_ptr, *space_ptr; + char *comment = strchr(rest, '#'); + size_t len = comment ? (size_t)(comment - rest) : strlen(rest); + char win[len + 1]; + strncpy(win, rest, len); + win[len] = '\0'; + + char *final = strip(win); + char *comma_ptr; char *comma = strtok_r(final, ",", &comma_ptr); while (comma) { - if (should_floatn < 256) { - // if comma starts and ends with quotes, remove them - if (*comma == '"') { - comma++; - } - char *end = comma + strlen(comma) - 1; - if (*end == '"') { - *end = '\0'; - } + if (should_floatn >= 256) { + fprintf(stderr, "sxwmrc:%d: too many should_float entries\n", lineno); + break; + } - printf("comma: %s\n", comma); - char *argv = strtok_r(comma, " ", &space_ptr); - int i = 0; + comma = strip(comma); + if (*comma == '"') + comma++; + char *end = comma + strlen(comma) - 1; + if (*end == '"') + *end = '\0'; - while (argv) { - printf("argv: %s\n", argv); - cfg->should_float[should_floatn][i] = strdup(argv); - argv = strtok_r(NULL, " ", &space_ptr); - i++; - } + char *space_ptr; + char *argv = strtok_r(comma, " ", &space_ptr); + int i = 0; - should_floatn++; - } - else { - fprintf(stderr, "sxwmrc:%d: too many should_float entries\n", lineno); - break; + while (argv && i < 256) { + cfg->should_float[should_floatn][i++] = strdup(argv); + argv = strtok_r(NULL, " ", &space_ptr); } + + should_floatn++; comma = strtok_r(NULL, ",", &comma_ptr); } - - should_floatn++; } else if (!strcmp(key, "call") || !strcmp(key, "bind")) { char *mid = strchr(rest, ':'); @@ -301,6 +295,7 @@ found:; // label followed by declaration is a C23 extension fprintf(stderr, "sxwmrc:%d: bad key in '%s'\n", lineno, combo); continue; } + Binding *b = alloc_bind(cfg, mods, ks); if (!b) { fputs("sxwm: too many binds\n", stderr); @@ -342,6 +337,7 @@ found:; // label followed by declaration is a C23 extension fprintf(stderr, "sxwmrc:%d: bad key in '%s'\n", lineno, combo); continue; } + Binding *b = alloc_bind(cfg, mods, ks); if (!b) { fputs("sxwm: too many binds\n", stderr); |
