commit:     784f3743879ec97e08197d3b21ccec54ffce914d
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Fri Nov 29 13:55:34 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Fri Nov 29 13:55:34 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=784f3743

main: fix memleak for main_overlay in the correct way

the previous fix introduced a double free, the real problem is that the
original main_overlay pointer was taken before its default value was
assigned

Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 main.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/main.c b/main.c
index 1f20b38..ba15c8c 100644
--- a/main.c
+++ b/main.c
@@ -617,7 +617,7 @@ read_one_repos_conf(const char *repos_conf)
        nsec = iniparser_getnsec(dict);
        while (nsec-- > 0) {
                repo = iniparser_getsecname(dict, nsec);
-               if (!strcmp(repo, "DEFAULT"))
+               if (strcmp(repo, "DEFAULT") == 0)  /* already handled above */
                        continue;
 
                xasprintf(&conf, "%s:location", repo);
@@ -646,7 +646,6 @@ read_one_repos_conf(const char *repos_conf)
                                xarraypush_str(overlay_src, repos_conf);
                        }
                        if (main_repo && strcmp(repo, main_repo) == 0) {
-                               free(main_overlay);
                                main_overlay = ele;
                                free(vars_to_read[11 /* PORTDIR */].src);
                                vars_to_read[11 /* PORTDIR */].src = 
xstrdup(repos_conf);
@@ -720,7 +719,7 @@ initialize_portage_env(void)
        env_vars *var;
        char pathbuf[_Q_PATH_MAX];
        const char *configroot = getenv("PORTAGE_CONFIGROOT");
-       char *orig_main_overlay = main_overlay;
+       char *orig_main_overlay;
 
        /* initialize all the strings with their default value */
        for (i = 0; vars_to_read[i].name; ++i) {
@@ -741,6 +740,7 @@ initialize_portage_env(void)
 
        /* read overlays first so we can resolve repo references in profile
         * parent files */
+       orig_main_overlay = main_overlay;
        snprintf(pathbuf, sizeof(pathbuf), "%.*s", (int)i, configroot);
        read_repos_conf(pathbuf, "/usr/share/portage/config/repos.conf");
        read_repos_conf(pathbuf, "/etc/portage/repos.conf");
@@ -752,6 +752,7 @@ initialize_portage_env(void)
                xarraypush_str(overlay_src, STR_DEFAULT);
        } else if (orig_main_overlay == main_overlay) {
                /* if no explicit overlay was flagged as main, take the first 
one */
+               free(orig_main_overlay);
                main_overlay = array_get_elem(overlays, 0);
                set_portage_env_var(&vars_to_read[11] /* PORTDIR */, 
main_overlay,
                                (char *)array_get_elem(overlay_src, 0));

Reply via email to