From 956f91f6a4680d83038ab6ad7422e5576618eab6 Mon Sep 17 00:00:00 2001 From: Nikos Tsipinakis Date: Sat, 11 Feb 2017 17:51:01 +0200 Subject: [PATCH] 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 --- src/option_parser.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/option_parser.c b/src/option_parser.c index d7c9183..1486037 100644 --- a/src/option_parser.c +++ b/src/option_parser.c @@ -206,14 +206,15 @@ char *clean_value(char *value) int load_ini_file(FILE * fp) { - char line[BUFSIZ]; - if (!fp) return 1; + char *line = NULL; + size_t line_len = 0; + int line_num = 0; char *current_section = NULL; - while (fgets(line, sizeof(line), fp) != NULL) { + while (getline(&line, &line_len, fp) != -1) { line_num++; char *start = g_strstrip(line); @@ -280,6 +281,7 @@ int load_ini_file(FILE * fp) add_entry(current_section, key, value); } + free(line); if (current_section) free(current_section); return 0;