Fix issue with loading config files with lines bigger than BUFSIZ

To load each line of the configuration file we were previously using a
simple buffer of length BUFSIZ. BUFSIZ is a macro provided by glibc as
the 'recommended length for a buffer'. But since one of our users
encountered a situation where really long config lines were necessary
it's time to change that behaviour.

This commit changes the line reading from using fgets with a character
limit of BUFSIZ to simply using getline.

We initialize the buffer pointer to NULL since getline will
automatically allocate a big enough buffer for us if it's passed a NULL
pointer. After that we pass the same buffer pointer again since,
according to the getline manpage, getline will also call realloc on the
buffer if necessary. Which means the only thing we have to do is call
free() at the end of the parsing process.

Fixes #294
This commit is contained in:
Nikos Tsipinakis 2017-02-11 17:51:01 +02:00
parent 42a641c728
commit 956f91f6a4

View File

@ -206,14 +206,15 @@ char *clean_value(char *value)
int load_ini_file(FILE * fp) int load_ini_file(FILE * fp)
{ {
char line[BUFSIZ];
if (!fp) if (!fp)
return 1; return 1;
char *line = NULL;
size_t line_len = 0;
int line_num = 0; int line_num = 0;
char *current_section = NULL; char *current_section = NULL;
while (fgets(line, sizeof(line), fp) != NULL) { while (getline(&line, &line_len, fp) != -1) {
line_num++; line_num++;
char *start = g_strstrip(line); char *start = g_strstrip(line);
@ -280,6 +281,7 @@ int load_ini_file(FILE * fp)
add_entry(current_section, key, value); add_entry(current_section, key, value);
} }
free(line);
if (current_section) if (current_section)
free(current_section); free(current_section);
return 0; return 0;