From e4d5ac9c2ec86dd5e5465d6fe138b6162246c6f2 Mon Sep 17 00:00:00 2001 From: Mikhail Zakharov Date: Sun, 29 Sep 2024 12:53:13 +0200 Subject: [PATCH] `inifile.c`: multiple proxy-chain links , potential buffer issue in sections def fix --- CHANGELOG.md | 4 +++- inifile.c | 27 ++++++++++++++------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1aa454..b33f438 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # CHANGELOG -* **2024.09.27 Current** +* **2024.09.28 Current** + * `inifile.c`: Correct usage of multiple links of a proxy-chain + * `inifile.c`: Potential buffer-overflow fixed in parsing `[SECTIONS]` * `ts-warp.c`: Proxy server selection optimization * `pidlist.c`: `Uninitialised value was created by a heap allocation` fixed diff --git a/inifile.c b/inifile.c index 881ad35..37fa06b 100644 --- a/inifile.c +++ b/inifile.c @@ -50,7 +50,7 @@ ini_section *read_ini(char *ifile_name) { /* Read and parse INI-file */ FILE *fini; - char buffer[BUF_SIZE], section[STR_SIZE]; + char buffer[BUF_SIZE], section[BUF_SIZE]; char *s = NULL, *d = NULL, *x = NULL; /* String manipulation pointers */ ini_entry entry; /* tmp place for the parsed line */ ini_section *ini_root = NULL, *c_sect = NULL, *l_sect = NULL; @@ -74,6 +74,17 @@ ini_section *read_ini(char *ifile_name) { s = buffer; strsep(&s, "#;\n\r"); + s = d = buffer; + do { + while(isspace(*s)) s++; /* Remove whitespaces */ + if ((!isascii(*s) || iscntrl(*s)) && *s != '\0') { /* Ignore lines with non-ASCII or Control chars */ + printl(LOG_WARN, "LN: %d IGNORED: Contains non-ASCII or Control character: [%#x]!", + ln, (unsigned char)*s); + *buffer = '\0'; + break; + } + } while((*d++ = *s++)); + /* Get section */ if (sscanf(buffer, "[%[a-zA-Z0-9_\t +()-]]", section) == 1) { printl(LOG_VERB, "LN: [%d] S: [%s]", ln, section); @@ -106,17 +117,6 @@ ini_section *read_ini(char *ifile_name) { free(proxy_port); proxy_port = NULL; fproxy_port = 0; } } else { /* Entries within sections */ - s = d = buffer; - do { - while(isspace(*s)) s++; /* Remove whitespaces */ - if ((!isascii(*s) || iscntrl(*s)) && *s != '\0') { /* Ignore lines with non-ASCII or Control chars */ - printl(LOG_WARN, "LN: %d IGNORED: Contains non-ASCII or Control character: [%#x]!", - ln, (unsigned char)*s); - *buffer = '\0'; - break; - } - } while((*d++ = *s++)); - if (!*buffer) continue; /* Skip an empty line */ if (strchr(buffer, '=') == NULL) { /* Skip variables without vals */ printl(LOG_WARN, "LN: %d IGNORED: The variable must be assigned a value", ln); @@ -380,7 +380,8 @@ int create_chains(struct ini_section *ini, struct chain_list *chain) { st = (struct proxy_chain *)malloc(sizeof(struct proxy_chain)); st->chain_member = sts; st->next = NULL; - if (sc) sc->next = st; else s->p_chain = st; + if (!sc) s->p_chain = st; else sc->next = st; + sc = st; printl(LOG_VERB, "Linking chain section: [%s]", st->chain_member->section_name); } else printl(LOG_VERB, "Chain section: [%s] linked from: [%s] does not really exist",