From d3e2732da85a5781cb145e692d1ba8ca31b69152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=B6llendorf?= Date: Sat, 13 Apr 2024 13:47:38 +0200 Subject: [PATCH 1/3] Add test to proof an inconsistency in iniparser concerning brackets The section "12345]" is readable from dictionary directly after iniparser_set(). The section is also written to file by iniparser_dump_ini() as "[12345]]" But if the file is parsed by iniparser_load() it is read as "12345". The reason is that iniparser_load() considers a section name to be enclosed by "[" and "]". If it find a line enclosed by square brackets it takes the characters enclosed by and excluding those brackets. refs #147 --- test/test_iniparser.c | 194 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) diff --git a/test/test_iniparser.c b/test/test_iniparser.c index dfcad8c..23d2205 100644 --- a/test/test_iniparser.c +++ b/test/test_iniparser.c @@ -20,6 +20,13 @@ #define TEST_TXT_PATH "ressources/test.txt" #define GRUEZI_INI_PATH "ressources/gruezi.ini" #define UTF8_INI_PATH "ressources/utf8.ini" +#define MISFORMED_INI_PATH "ressources/misformed.ini" +#define MISFORMED_INI_SEC0 "[12345" +#define MISFORMED_INI_SEC1 "12345]" +#define MISFORMED_INI_SEC1_ "12345" +#define MISFORMED_INI_SEC2 "123]45" +#define MISFORMED_INI_SEC2_ "123" +#define MISFORMED_INI_ATTR "1111" #define stringify_2(x) #x #define stringify(x) stringify_2(x) @@ -1024,3 +1031,190 @@ void Test_iniparser_utf8(CuTest *tc) iniparser_getstring(dic, "拉麺:メンマ", NULL)); dictionary_del(dic); } + +static void create_empty_ini_file(const char *filename) +{ + FILE *ini; + + if ((ini = fopen(filename, "w")) == NULL) { + fprintf(stderr, "iniparser: cannot create %s\n", filename); + return; + } + + fclose(ini); +} + +void Test_iniparser_misformed(CuTest *tc) +{ + dictionary *dic; + FILE *ini; + int ret; + + create_empty_ini_file(MISFORMED_INI_PATH); + dic = iniparser_load(MISFORMED_INI_PATH); + + if (!dic) { + fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + return; + } + + ret = iniparser_set(dic, MISFORMED_INI_SEC0, NULL); + + if (ret < 0) { + fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC0, + MISFORMED_INI_PATH); + goto del_dic; + } + + iniparser_set(dic, MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, "2222"); + /* test dictionary */ + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, + MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, -1)); + ini = fopen(MISFORMED_INI_PATH, "w+"); + + if (!ini) { + fprintf(stderr, "iniparser: cannot open %s\n", MISFORMED_INI_PATH); + goto del_dic; + } + + iniparser_dump_ini(dic, ini); + fclose(ini); + dictionary_del(dic); + /* check if section has been written as expected */ + dic = iniparser_load(MISFORMED_INI_PATH); + + if (!dic) { + fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + goto rm_ini; + } + + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, + MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, -1)); + dictionary_del(dic); + ret = remove(MISFORMED_INI_PATH); + + if (ret) { + fprintf(stderr, "cannot remove file: %s\n", MISFORMED_INI_PATH); + return; + } + + create_empty_ini_file(MISFORMED_INI_PATH); + dic = iniparser_load(MISFORMED_INI_PATH); + + if (!dic) { + fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + return; + } + + ret = iniparser_set(dic, MISFORMED_INI_SEC1, NULL); + + if (ret < 0) { + fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC1, + MISFORMED_INI_PATH); + goto del_dic; + } + + iniparser_set(dic, MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, "2222"); + /* test dictionary */ + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, + MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, -1)); + ini = fopen(MISFORMED_INI_PATH, "w+"); + + if (!ini) { + fprintf(stderr, "iniparser: cannot open %s\n", MISFORMED_INI_PATH); + goto del_dic; + } + + iniparser_dump_ini(dic, ini); + fclose(ini); + dictionary_del(dic); + /* check if section has been written as expected */ + dic = iniparser_load(MISFORMED_INI_PATH); + + if (!dic) { + fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + return; + } + + /* + * This test proofs an inconsistency in iniparser_set(): + * + * The section "12345]" (MISFORMED_INI_SEC1) is readable from dictionary + * directly after iniparser_set(). + * + * The section is also written to file by iniparser_dump_ini() as + * "[12345]]" ("[" MISFOREMD_INI_SEC1 "]"). + * + * But if the file is parsed by iniparser_load() it is read as "12345" + * (MISFORMED_INI_SEC). + * + * The reason is that iniparser_load() considers a section name to be + * enclosed by "[" and "]". If it find a line enclosed by square brackets + * it takes the characters enclosed by and excluding those brackets + */ + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, + MISFORMED_INI_SEC1_ ":" MISFORMED_INI_ATTR, -1)); + dictionary_del(dic); + ret = remove(MISFORMED_INI_PATH); + + if (ret) { + fprintf(stderr, "cannot remove file: %s\n", MISFORMED_INI_PATH); + return; + } + + create_empty_ini_file(MISFORMED_INI_PATH); + dic = iniparser_load(MISFORMED_INI_PATH); + + if (!dic) { + fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + return; + } + + ret = iniparser_set(dic, MISFORMED_INI_SEC2, NULL); + + if (ret < 0) { + fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC2, + MISFORMED_INI_PATH); + goto del_dic; + } + + iniparser_set(dic, MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, "2222"); + /* test dictionary */ + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, + MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, -1)); + ini = fopen(MISFORMED_INI_PATH, "w+"); + + if (!ini) { + fprintf(stderr, "iniparser: cannot open %s\n", MISFORMED_INI_PATH); + goto del_dic; + } + + iniparser_dump_ini(dic, ini); + fclose(ini); + dictionary_del(dic); + /* check if section has been written as expected */ + dic = iniparser_load(MISFORMED_INI_PATH); + + if (!dic) { + fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + return; + } + + /* + * This test again proofs the inconsistency in iniparser_set() explained + * above. + * + * This time the section name is "123]45" (MISFORMED_INI_SEC2). + */ + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, + MISFORMED_INI_SEC2_ ":" MISFORMED_INI_ATTR, -1)); +del_dic: + dictionary_del(dic); +rm_ini: + ret = remove(MISFORMED_INI_PATH); + + if (ret) { + fprintf(stderr, "cannot remove file: %s\n", MISFORMED_INI_PATH); + return; + } +} From 59c465cb85bb20ebe36f828e3489b64722c68bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=B6llendorf?= Date: Sat, 13 Apr 2024 13:52:15 +0200 Subject: [PATCH 2/3] Allow closing square brackets in section names refs #147 --- src/iniparser.c | 7 +++++-- test/test_iniparser.c | 28 ++-------------------------- 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/src/iniparser.c b/src/iniparser.c index 0924d06..2c639f5 100644 --- a/src/iniparser.c +++ b/src/iniparser.c @@ -673,8 +673,11 @@ static line_status iniparser_line( /* Comment line */ sta = LINE_COMMENT ; } else if (line[0]=='[' && line[len-1]==']') { - /* Section name */ - sscanf(line, "[%[^]]", section); + /* Section name without opening square bracket */ + sscanf(line, "[%s", section); + /* Section name without closing square bracket */ + section[len] = '\0'; + len--; strstrip(section); strlwc(section, section, len); sta = LINE_SECTION ; diff --git a/test/test_iniparser.c b/test/test_iniparser.c index 23d2205..19458f1 100644 --- a/test/test_iniparser.c +++ b/test/test_iniparser.c @@ -23,9 +23,7 @@ #define MISFORMED_INI_PATH "ressources/misformed.ini" #define MISFORMED_INI_SEC0 "[12345" #define MISFORMED_INI_SEC1 "12345]" -#define MISFORMED_INI_SEC1_ "12345" #define MISFORMED_INI_SEC2 "123]45" -#define MISFORMED_INI_SEC2_ "123" #define MISFORMED_INI_ATTR "1111" #define stringify_2(x) #x @@ -1136,24 +1134,8 @@ void Test_iniparser_misformed(CuTest *tc) return; } - /* - * This test proofs an inconsistency in iniparser_set(): - * - * The section "12345]" (MISFORMED_INI_SEC1) is readable from dictionary - * directly after iniparser_set(). - * - * The section is also written to file by iniparser_dump_ini() as - * "[12345]]" ("[" MISFOREMD_INI_SEC1 "]"). - * - * But if the file is parsed by iniparser_load() it is read as "12345" - * (MISFORMED_INI_SEC). - * - * The reason is that iniparser_load() considers a section name to be - * enclosed by "[" and "]". If it find a line enclosed by square brackets - * it takes the characters enclosed by and excluding those brackets - */ CuAssertIntEquals(tc, 2222, iniparser_getint(dic, - MISFORMED_INI_SEC1_ ":" MISFORMED_INI_ATTR, -1)); + MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, -1)); dictionary_del(dic); ret = remove(MISFORMED_INI_PATH); @@ -1200,14 +1182,8 @@ void Test_iniparser_misformed(CuTest *tc) return; } - /* - * This test again proofs the inconsistency in iniparser_set() explained - * above. - * - * This time the section name is "123]45" (MISFORMED_INI_SEC2). - */ CuAssertIntEquals(tc, 2222, iniparser_getint(dic, - MISFORMED_INI_SEC2_ ":" MISFORMED_INI_ATTR, -1)); + MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, -1)); del_dic: dictionary_del(dic); rm_ini: From f8bcd901b889bd9a9cf90802db73842ffbe55e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=B6llendorf?= Date: Sun, 21 Apr 2024 21:30:13 +0200 Subject: [PATCH 3/3] Re-use temporary INI file path in tests --- test/test_iniparser.c | 63 +++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/test/test_iniparser.c b/test/test_iniparser.c index 262e770..d34d554 100644 --- a/test/test_iniparser.c +++ b/test/test_iniparser.c @@ -20,12 +20,11 @@ #define TEST_TXT_PATH "ressources/test.txt" #define GRUEZI_INI_PATH "ressources/gruezi.ini" #define UTF8_INI_PATH "ressources/utf8.ini" -#define MISFORMED_INI_PATH "ressources/misformed.ini" +#define TMP_INI_PATH "ressources/tmp.ini" #define MISFORMED_INI_SEC0 "[12345" #define MISFORMED_INI_SEC1 "12345]" #define MISFORMED_INI_SEC2 "123]45" #define MISFORMED_INI_ATTR "1111" -#define TMP_INI_PATH "ressources/tmp.ini" #define QUOTES_INI_PATH "ressources/quotes.ini" #define QUOTES_INI_SEC "quotes" #define QUOTES_INI_ATTR0 "string0" @@ -1060,11 +1059,11 @@ void Test_iniparser_misformed(CuTest *tc) FILE *ini; int ret; - create_empty_ini_file(MISFORMED_INI_PATH); - dic = iniparser_load(MISFORMED_INI_PATH); + create_empty_ini_file(TMP_INI_PATH); + dic = iniparser_load(TMP_INI_PATH); if (!dic) { - fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); return; } @@ -1072,7 +1071,7 @@ void Test_iniparser_misformed(CuTest *tc) if (ret < 0) { fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC0, - MISFORMED_INI_PATH); + TMP_INI_PATH); goto del_dic; } @@ -1080,10 +1079,10 @@ void Test_iniparser_misformed(CuTest *tc) /* test dictionary */ CuAssertIntEquals(tc, 2222, iniparser_getint(dic, MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, -1)); - ini = fopen(MISFORMED_INI_PATH, "w+"); + ini = fopen(TMP_INI_PATH, "w+"); if (!ini) { - fprintf(stderr, "iniparser: cannot open %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH); goto del_dic; } @@ -1091,28 +1090,28 @@ void Test_iniparser_misformed(CuTest *tc) fclose(ini); dictionary_del(dic); /* check if section has been written as expected */ - dic = iniparser_load(MISFORMED_INI_PATH); + dic = iniparser_load(TMP_INI_PATH); if (!dic) { - fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); goto rm_ini; } CuAssertIntEquals(tc, 2222, iniparser_getint(dic, MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, -1)); dictionary_del(dic); - ret = remove(MISFORMED_INI_PATH); + ret = remove(TMP_INI_PATH); if (ret) { - fprintf(stderr, "cannot remove file: %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH); return; } - create_empty_ini_file(MISFORMED_INI_PATH); - dic = iniparser_load(MISFORMED_INI_PATH); + create_empty_ini_file(TMP_INI_PATH); + dic = iniparser_load(TMP_INI_PATH); if (!dic) { - fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); return; } @@ -1120,7 +1119,7 @@ void Test_iniparser_misformed(CuTest *tc) if (ret < 0) { fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC1, - MISFORMED_INI_PATH); + TMP_INI_PATH); goto del_dic; } @@ -1128,10 +1127,10 @@ void Test_iniparser_misformed(CuTest *tc) /* test dictionary */ CuAssertIntEquals(tc, 2222, iniparser_getint(dic, MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, -1)); - ini = fopen(MISFORMED_INI_PATH, "w+"); + ini = fopen(TMP_INI_PATH, "w+"); if (!ini) { - fprintf(stderr, "iniparser: cannot open %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH); goto del_dic; } @@ -1139,28 +1138,28 @@ void Test_iniparser_misformed(CuTest *tc) fclose(ini); dictionary_del(dic); /* check if section has been written as expected */ - dic = iniparser_load(MISFORMED_INI_PATH); + dic = iniparser_load(TMP_INI_PATH); if (!dic) { - fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); return; } CuAssertIntEquals(tc, 2222, iniparser_getint(dic, MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, -1)); dictionary_del(dic); - ret = remove(MISFORMED_INI_PATH); + ret = remove(TMP_INI_PATH); if (ret) { - fprintf(stderr, "cannot remove file: %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH); return; } - create_empty_ini_file(MISFORMED_INI_PATH); - dic = iniparser_load(MISFORMED_INI_PATH); + create_empty_ini_file(TMP_INI_PATH); + dic = iniparser_load(TMP_INI_PATH); if (!dic) { - fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); return; } @@ -1168,7 +1167,7 @@ void Test_iniparser_misformed(CuTest *tc) if (ret < 0) { fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC2, - MISFORMED_INI_PATH); + TMP_INI_PATH); goto del_dic; } @@ -1176,10 +1175,10 @@ void Test_iniparser_misformed(CuTest *tc) /* test dictionary */ CuAssertIntEquals(tc, 2222, iniparser_getint(dic, MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, -1)); - ini = fopen(MISFORMED_INI_PATH, "w+"); + ini = fopen(TMP_INI_PATH, "w+"); if (!ini) { - fprintf(stderr, "iniparser: cannot open %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH); goto del_dic; } @@ -1187,10 +1186,10 @@ void Test_iniparser_misformed(CuTest *tc) fclose(ini); dictionary_del(dic); /* check if section has been written as expected */ - dic = iniparser_load(MISFORMED_INI_PATH); + dic = iniparser_load(TMP_INI_PATH); if (!dic) { - fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); return; } @@ -1199,10 +1198,10 @@ void Test_iniparser_misformed(CuTest *tc) del_dic: dictionary_del(dic); rm_ini: - ret = remove(MISFORMED_INI_PATH); + ret = remove(TMP_INI_PATH); if (ret) { - fprintf(stderr, "cannot remove file: %s\n", MISFORMED_INI_PATH); + fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH); return; } }