1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-11 09:18:07 +03:00

install: fix memory leak if GREEDY_REALLOC() fails

This commit is contained in:
David Tardon 2023-04-14 09:43:43 +02:00
parent 13524b29a2
commit 52c788e6e0

View File

@ -78,14 +78,19 @@ static bool install_info_has_also(const InstallInfo *i) {
return !strv_isempty(i->also); return !strv_isempty(i->also);
} }
static void unit_file_preset_rule_done(UnitFilePresetRule *rule) {
assert(rule);
free(rule->pattern);
strv_free(rule->instances);
}
void unit_file_presets_freep(UnitFilePresets *p) { void unit_file_presets_freep(UnitFilePresets *p) {
if (!p) if (!p)
return; return;
for (size_t i = 0; i < p->n_rules; i++) { for (size_t i = 0; i < p->n_rules; i++)
free(p->rules[i].pattern); unit_file_preset_rule_done(&p->rules[i]);
strv_free(p->rules[i].instances);
}
free(p->rules); free(p->rules);
p->n_rules = 0; p->n_rules = 0;
@ -3241,7 +3246,7 @@ static int read_presets(RuntimeScope scope, const char *root_dir, UnitFilePreset
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
UnitFilePresetRule rule = {}; _cleanup_(unit_file_preset_rule_done) UnitFilePresetRule rule = {};
const char *parameter; const char *parameter;
char *l; char *l;
@ -3296,7 +3301,7 @@ static int read_presets(RuntimeScope scope, const char *root_dir, UnitFilePreset
if (!GREEDY_REALLOC(ps.rules, ps.n_rules + 1)) if (!GREEDY_REALLOC(ps.rules, ps.n_rules + 1))
return -ENOMEM; return -ENOMEM;
ps.rules[ps.n_rules++] = rule; ps.rules[ps.n_rules++] = TAKE_STRUCT(rule);
continue; continue;
} }