From 3560c2e01563f39bf3775f6afe1e12997fe99969 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 7 Aug 2024 11:06:47 -0400 Subject: ini_getval: Return copies, not the original. * This forces one to use ini_setval to replace/append values to the data array(s). It's safer this way. --- src/ini.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/ini.c b/src/ini.c index 37d80fe..bddfdcd 100644 --- a/src/ini.c +++ b/src/ini.c @@ -168,17 +168,26 @@ int ini_getval(struct INIFILE *ini, char *section_name, char *key, int type, uni result->as_float = (float) strtod(data->value, NULL); break; case INIVAL_TYPE_STR: - result->as_char_p = lstrip(data->value); + result->as_char_p = strdup(data->value); + if (!result->as_char_p) { + return -1; + } + lstrip(result->as_char_p); break; case INIVAL_TYPE_STR_ARRAY: strcpy(tbufp, data->value); - *data->value = '\0'; + char *value = NULL; + size_t lines = num_chars(tbufp, '\n'); + value = calloc(strlen(tbufp) + lines + 1, sizeof(*value)); + if (!value) { + return -1; + } while ((token = strsep(&tbufp, "\n")) != NULL) { lstrip(token); - strcat(data->value, token); - strcat(data->value, "\n"); + strcat(value, token); + strcat(value, "\n"); } - result->as_char_p = data->value; + result->as_char_p = value; break; case INIVAL_TYPE_BOOL: result->as_bool = false; -- cgit