amdfwtool: Clear struct match before regular expression matching

If it is not cleared and the number of strings is fewer than last
iteration, the match[3] will keep the last value, which actually
should be empty.

Add assert to make sure the level is a legal value.

BUG=b:222038278

Change-Id: If14e0923fbb1648d83784eb5dc1411c93227db5a
Signed-off-by: Zheng Bao <fishbaozi@gmail.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/62482
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Felix Held <felix-coreboot@felixheld.de>
This commit is contained in:
Zheng Bao 2022-03-01 17:18:00 +08:00 committed by Felix Held
parent 0e545b252d
commit 4df5af87be
1 changed files with 19 additions and 5 deletions

View File

@ -428,10 +428,15 @@ int get_input_file_line(FILE *f, char line[], int line_buf_size)
return OK; return OK;
} }
static int is_valid_entry(char *oneline, regmatch_t *match) #define N_MATCHES 4
static int is_valid_entry(char *oneline, regmatch_t match[N_MATCHES])
{ {
int retval; int retval, index;
for (index = 0; index < N_MATCHES; index++) {
match[index].rm_so = -1;
match[index].rm_eo = -1;
}
if (regexec(&entries_line_expr, oneline, 3, match, 0) == 0) { if (regexec(&entries_line_expr, oneline, 3, match, 0) == 0) {
oneline[match[1].rm_eo] = '\0'; oneline[match[1].rm_eo] = '\0';
oneline[match[2].rm_eo] = '\0'; oneline[match[2].rm_eo] = '\0';
@ -467,7 +472,6 @@ static int skip_comment_blank_line(char *oneline)
return retval; return retval;
} }
#define N_MATCHES 4
/* /*
return value: return value:
0: The config file can not be parsed correctly. 0: The config file can not be parsed correctly.
@ -476,9 +480,15 @@ static int skip_comment_blank_line(char *oneline)
uint8_t process_config(FILE *config, amd_cb_config *cb_config, uint8_t print_deps) uint8_t process_config(FILE *config, amd_cb_config *cb_config, uint8_t print_deps)
{ {
char oneline[MAX_LINE_SIZE], *path_filename; char oneline[MAX_LINE_SIZE], *path_filename;
regmatch_t match[N_MATCHES] = {0}; regmatch_t match[N_MATCHES];
char dir[MAX_LINE_SIZE] = {'\0'}; char dir[MAX_LINE_SIZE] = {'\0'};
uint32_t dir_len; uint32_t dir_len;
int index;
for (index = 0; index < N_MATCHES; index++) {
match[index].rm_so = -1;
match[index].rm_eo = -1;
}
compile_reg_expr(REG_EXTENDED | REG_NEWLINE, compile_reg_expr(REG_EXTENDED | REG_NEWLINE,
blank_or_comment_regex, &blank_or_comment_expr); blank_or_comment_regex, &blank_or_comment_expr);
@ -527,13 +537,17 @@ uint8_t process_config(FILE *config, amd_cb_config *cb_config, uint8_t print_dep
/* If the optional level field is present, /* If the optional level field is present,
extract the level char. */ extract the level char. */
if (match[3].rm_so != 0) { if (match[3].rm_so != -1) {
if (cb_config->recovery_ab == 0) if (cb_config->recovery_ab == 0)
ch_lvl = oneline[match[3].rm_so + 1]; ch_lvl = oneline[match[3].rm_so + 1];
else else
ch_lvl = oneline[match[3].rm_so + 2]; ch_lvl = oneline[match[3].rm_so + 2];
} }
assert(ch_lvl == 'x' || ch_lvl == 'X' ||
ch_lvl == 'b' || ch_lvl == 'B' ||
ch_lvl == '1' || ch_lvl == '2');
if (find_register_fw_filename_psp_dir( if (find_register_fw_filename_psp_dir(
&(oneline[match[1].rm_so]), &(oneline[match[1].rm_so]),
path_filename, ch_lvl, cb_config) == 0) { path_filename, ch_lvl, cb_config) == 0) {