Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,20 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
#endif
#if INI_ALLOW_REALLOC && !INI_USE_STACK
char* new_line;
size_t offset;
#endif
char section[MAX_SECTION] = "";
#if INI_ALLOW_MULTILINE
char prev_name[MAX_NAME] = "";
#endif

size_t offset;
char* start;
char* end;
char* name;
char* value;
int lineno = 0;
int error = 0;
char abyss[16]; /* Used to consume input when a line is too long. */

#if !INI_USE_STACK
line = (char*)ini_malloc(INI_INITIAL_ALLOC);
Expand All @@ -136,8 +137,9 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,

/* Scan through stream line by line */
while (reader(line, (int)max_line, stream) != NULL) {
#if INI_ALLOW_REALLOC && !INI_USE_STACK
offset = strlen(line);

#if INI_ALLOW_REALLOC && !INI_USE_STACK
while (offset == max_line - 1 && line[offset - 1] != '\n') {
max_line *= 2;
if (max_line > INI_MAX_LINE)
Expand All @@ -150,14 +152,24 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
line = new_line;
if (reader(line + offset, (int)(max_line - offset), stream) == NULL)
break;
offset += strlen(line + offset);
if (max_line >= INI_MAX_LINE)
break;
offset += strlen(line + offset);
}
#endif

lineno++;

/* If line exceeded INI_MAX_LINE bytes, discard till end of line. */
if (offset == max_line - 1 && line[offset - 1] != '\n') {
while (reader(abyss, sizeof(abyss), stream) != NULL) {
if (!error)
error = lineno;
if (abyss[strlen(abyss) - 1] == '\n')
break;
}
}

start = line;
#if INI_ALLOW_BOM
if (lineno == 1 && (unsigned char)start[0] == 0xEF &&
Expand Down
10 changes: 3 additions & 7 deletions tests/baseline_heap_max_line.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ no_file.ini: e=-1 user=0
... [comment_test]
... test1=1;2;3;
... test2=2;3;4;this;
... test2=needs whitespace b;
... test;3=345;
... test4=4#5#6;
... test4=only starts a comm;
... test7=;
... test8=; not a comm;
... [colon_tests]
Expand All @@ -22,7 +20,7 @@ no_file.ini: e=-1 user=0
... funny2=with : col;
... funny3=two = equa;
... funny4=two : colo;
normal.ini: e=2 user=101
normal.ini: e=1 user=101
... [section1]
... name1=value1;
... name2=value2;
Expand All @@ -47,11 +45,9 @@ user_error.ini: e=3 user=104
... multi=the quick;
... multi=brown fox;
... name=bob smith;
... name=comment line 1;
... name=comment line 2;
... foo=bar;
... foo=Hi World;
multi_line.ini: e=5 user=105
multi_line.ini: e=4 user=105
bad_multi.ini: e=1 user=105
... [bom_section]
... bom_name=bom_value;
Expand All @@ -69,4 +65,4 @@ duplicate_sections.ini: e=0 user=108
... key1=val1;
no_value.ini: e=2 user=109
... name=value;
long_section.ini: e=2 user=110
long_section.ini: e=1 user=110
10 changes: 3 additions & 7 deletions tests/baseline_heap_realloc_max_line.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ no_file.ini: e=-1 user=0
... [comment_test]
... test1=1;2;3;
... test2=2;3;4;this;
... test2=needs whitespace b;
... test;3=345;
... test4=4#5#6;
... test4=only starts a comm;
... test7=;
... test8=; not a comm;
... [colon_tests]
Expand All @@ -22,7 +20,7 @@ no_file.ini: e=-1 user=0
... funny2=with : col;
... funny3=two = equa;
... funny4=two : colo;
normal.ini: e=2 user=101
normal.ini: e=1 user=101
... [section1]
... name1=value1;
... name2=value2;
Expand All @@ -47,11 +45,9 @@ user_error.ini: e=3 user=104
... multi=the quick;
... multi=brown fox;
... name=bob smith;
... name=comment line 1;
... name=comment line 2;
... foo=bar;
... foo=Hi World;
multi_line.ini: e=5 user=105
multi_line.ini: e=4 user=105
bad_multi.ini: e=1 user=105
... [bom_section]
... bom_name=bom_value;
Expand All @@ -69,4 +65,4 @@ duplicate_sections.ini: e=0 user=108
... key1=val1;
no_value.ini: e=2 user=109
... name=value;
long_section.ini: e=2 user=110
long_section.ini: e=1 user=110
5 changes: 2 additions & 3 deletions tests/baseline_heap_string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ crlf: e=0 user=102
... [sec]
... foo=0123456789012;
... bar=4321;
long line: e=3 user=103
long line: e=2 user=103
... [sec]
... foo=0123456789012;
... bix=1234;
long continued: e=0 user=104
long continued: e=2 user=104
... [s]
... a=1;
... c=3;
Expand Down
10 changes: 3 additions & 7 deletions tests/baseline_multi_max_line.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ no_file.ini: e=-1 user=0
... [comment_test]
... test1=1;2;3;
... test2=2;3;4;this;
... test2=needs whitespace b;
... test;3=345;
... test4=4#5#6;
... test4=only starts a comm;
... test7=;
... test8=; not a comm;
... [colon_tests]
Expand All @@ -22,7 +20,7 @@ no_file.ini: e=-1 user=0
... funny2=with : col;
... funny3=two = equa;
... funny4=two : colo;
normal.ini: e=2 user=101
normal.ini: e=1 user=101
... [section1]
... name1=value1;
... name2=value2;
Expand All @@ -47,11 +45,9 @@ user_error.ini: e=3 user=104
... multi=the quick;
... multi=brown fox;
... name=bob smith;
... name=comment line 1;
... name=comment line 2;
... foo=bar;
... foo=Hi World;
multi_line.ini: e=5 user=105
multi_line.ini: e=4 user=105
bad_multi.ini: e=1 user=105
... [bom_section]
... bom_name=bom_value;
Expand All @@ -69,4 +65,4 @@ duplicate_sections.ini: e=0 user=108
... key1=val1;
no_value.ini: e=2 user=109
... name=value;
long_section.ini: e=2 user=110
long_section.ini: e=1 user=110
5 changes: 2 additions & 3 deletions tests/baseline_string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ crlf: e=0 user=102
... [sec]
... foo=0123456789012;
... bar=4321;
long line: e=3 user=103
long line: e=2 user=103
... [sec]
... foo=0123456789012;
... bix=1234;
long continued: e=0 user=104
long continued: e=2 user=104
... [s]
... a=1;
... c=3;
Expand Down
Loading