From 7969febfa9f0ef3fe679808d509d8fbb8bdbb5a4 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 9 Jun 2022 17:12:42 +0900 Subject: [PATCH] ini-parser: check closing bracket when parsing section name Change-Id: Ic939bfd6f57c9ee20e41fe7adf34f0debe3539e6 Signed-off-by: Youngjae Cho (cherry picked from commit 64006e638e2f1151d400490ca99ea6c96ddd0de1) --- src/libcommon/ini-parser.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libcommon/ini-parser.c b/src/libcommon/ini-parser.c index f08b28a..0f145c6 100644 --- a/src/libcommon/ini-parser.c +++ b/src/libcommon/ini-parser.c @@ -166,6 +166,7 @@ int libsys_config_parse_by_section(const char *fname, int cb(const struct parse_ char tmp_sectname[128]; char tmp_key[128]; char tmp_value[128]; + char closing_bracket; if (!fname || !cb) return -EINVAL; @@ -178,16 +179,16 @@ int libsys_config_parse_by_section(const char *fname, int cb(const struct parse_ while (getline(&line, &len, fp) != EOF) { // find section - retval = sscanf_trim_whitespace_comment(line, "[%127[^]]]", tmp_sectname); - if (retval != 1) + retval = sscanf_trim_whitespace_comment(line, "[%127[^]]%c", tmp_sectname, &closing_bracket); + if (retval != 2 || closing_bracket != ']') continue; result.section = strndup(tmp_sectname, sizeof(tmp_sectname)); // parse properties within the section while((n_read = getline(&line, &len, fp)) != EOF) { - retval = sscanf_trim_whitespace_comment(line, "[%127[^]]]", tmp_sectname); - if (retval == 1) { // found next section. stop parsing properties + retval = sscanf_trim_whitespace_comment(line, "[%127[^]]%c", tmp_sectname, &closing_bracket); + if (retval == 2 && closing_bracket == ']') { // found next section. stop parsing properties fseek(fp, -n_read, SEEK_CUR); break; } -- 2.34.1