config: Fix macro parsing and expansion bugs

This commit is contained in:
mintsuki 2022-07-02 13:29:18 +02:00
parent 8f8af188fb
commit 51b36a79c4
1 changed files with 12 additions and 4 deletions

View File

@ -178,8 +178,7 @@ int init_config(size_t config_size) {
} }
if (config_addr[i] == '\n' || config_addr[i] == 0 || config_addr[i+1] != '=') { if (config_addr[i] == '\n' || config_addr[i] == 0 || config_addr[i+1] != '=') {
bad_config = true; continue;
panic(true, "config: Malformed macro definition");
} }
i += 2; i += 2;
@ -208,13 +207,22 @@ int init_config(size_t config_size) {
for (i = 0, in = 0; i < config_size;) { for (i = 0, in = 0; i < config_size;) {
if ((config_size - i >= 3 && memcmp(config_addr + i, "\n${", 3) == 0) if ((config_size - i >= 3 && memcmp(config_addr + i, "\n${", 3) == 0)
|| (config_size - i >= 2 && i == 0 && memcmp(config_addr, "${", 2) == 0)) { || (config_size - i >= 2 && i == 0 && memcmp(config_addr, "${", 2) == 0)) {
size_t orig_i = i;
i += i ? 3 : 2; i += i ? 3 : 2;
while (config_addr[i] != '\n' && config_addr[i] != 0) { while (config_addr[i++] != '}') {
i++; if (i >= config_size) {
bad_config = true;
panic(true, "config: Malformed macro usage");
}
}
if (config_addr[i] != '=') {
i = orig_i;
goto next;
} }
continue; continue;
} }
next:
if (config_size - i >= 2 && memcmp(config_addr + i, "${", 2) == 0) { if (config_size - i >= 2 && memcmp(config_addr + i, "${", 2) == 0) {
char *macro_name = ext_mem_alloc(1024); char *macro_name = ext_mem_alloc(1024);
i += 2; i += 2;