diff options
author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2024-01-07 11:57:13 +0100 |
---|---|---|
committer | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2024-01-07 11:57:13 +0100 |
commit | ae4294ff5b85b4004c40743bf5330cc1ab2d7ae3 (patch) | |
tree | e9eba8fb63e36fd2178130c70a740140d7c3e84c /lib | |
parent | 7dfddfbc88b1d78b9824a68feca19df87a3eb504 (diff) | |
download | monitoring-plugins-ae4294ff5b85b4004c40743bf5330cc1ab2d7ae3.tar.gz |
Ini Parser: Avoid freeing symbols from text section by remembering where they are from
Diffstat (limited to 'lib')
-rw-r--r-- | lib/parse_ini.c | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 0cc864ae..09c0dc4f 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c | |||
@@ -36,6 +36,7 @@ | |||
36 | */ | 36 | */ |
37 | typedef struct { | 37 | typedef struct { |
38 | char *file; | 38 | char *file; |
39 | bool file_string_on_heap; | ||
39 | char *stanza; | 40 | char *stanza; |
40 | } np_ini_info; | 41 | } np_ini_info; |
41 | 42 | ||
@@ -95,16 +96,22 @@ parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) | |||
95 | i->stanza = malloc(sizeof(char) * (stanza_len + 1)); | 96 | i->stanza = malloc(sizeof(char) * (stanza_len + 1)); |
96 | strncpy(i->stanza, locator, stanza_len); | 97 | strncpy(i->stanza, locator, stanza_len); |
97 | i->stanza[stanza_len] = '\0'; | 98 | i->stanza[stanza_len] = '\0'; |
98 | } else /* otherwise we use the default stanza */ | 99 | } else {/* otherwise we use the default stanza */ |
99 | i->stanza = strdup(def_stanza); | 100 | i->stanza = strdup(def_stanza); |
101 | } | ||
100 | 102 | ||
101 | if (i->stanza == NULL) | 103 | if (i->stanza == NULL) |
102 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | 104 | die(STATE_UNKNOWN, _("malloc() failed!\n")); |
103 | 105 | ||
104 | /* check whether there's an @file part */ | 106 | /* check whether there's an @file part */ |
105 | i->file = stanza_len == locator_len | 107 | if (stanza_len == locator_len) { |
106 | ? default_file() | 108 | i->file = default_file(); |
107 | : strdup(&(locator[stanza_len + 1])); | 109 | i->file_string_on_heap = false; |
110 | } else { | ||
111 | i->file = strdup(&(locator[stanza_len + 1])); | ||
112 | i->file_string_on_heap = true; | ||
113 | } | ||
114 | |||
108 | if (i->file == NULL || i->file[0] == '\0') | 115 | if (i->file == NULL || i->file[0] == '\0') |
109 | die(STATE_UNKNOWN, | 116 | die(STATE_UNKNOWN, |
110 | _("Cannot find config file in any standard location.\n")); | 117 | _("Cannot find config file in any standard location.\n")); |
@@ -136,7 +143,10 @@ np_get_defaults(const char *locator, const char *default_section) | |||
136 | _("Invalid section '%s' in config file '%s'\n"), i.stanza, | 143 | _("Invalid section '%s' in config file '%s'\n"), i.stanza, |
137 | i.file); | 144 | i.file); |
138 | 145 | ||
139 | free(i.file); | 146 | if (i.file_string_on_heap) { |
147 | free(i.file); | ||
148 | } | ||
149 | |||
140 | if (inifile != stdin) | 150 | if (inifile != stdin) |
141 | fclose(inifile); | 151 | fclose(inifile); |
142 | free(i.stanza); | 152 | free(i.stanza); |
@@ -358,14 +368,18 @@ add_option(FILE *f, np_arg_list **optlst) | |||
358 | static char * | 368 | static char * |
359 | default_file(void) | 369 | default_file(void) |
360 | { | 370 | { |
361 | char **p, *ini_file; | 371 | char *ini_file; |
362 | 372 | ||
363 | if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || | 373 | if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || |
364 | (ini_file = default_file_in_path()) != NULL) | 374 | (ini_file = default_file_in_path()) != NULL) { |
365 | return ini_file; | 375 | return ini_file; |
366 | for (p = default_ini_path_names; *p != NULL; p++) | 376 | } |
367 | if (access(*p, F_OK) == 0) | 377 | |
378 | for (char **p = default_ini_path_names; *p != NULL; p++) { | ||
379 | if (access(*p, F_OK) == 0) { | ||
368 | return *p; | 380 | return *p; |
381 | } | ||
382 | } | ||
369 | return NULL; | 383 | return NULL; |
370 | } | 384 | } |
371 | 385 | ||