]> gitweb.ps.run Git - ps-cgit/blobdiff - ui-shared.c
ui-shared: fix resource leak: free allocation from cgit_hosturl
[ps-cgit] / ui-shared.c
index 89c48979586edaa7dc6d7df1aa133f288638b70e..c04f380be52defbcfb2afb9187b61529c24e68ed 100644 (file)
@@ -54,21 +54,21 @@ const char *cgit_httpscheme(void)
                return "http://";
 }
 
-const char *cgit_hosturl(void)
+char *cgit_hosturl(void)
 {
        if (ctx.env.http_host)
-               return ctx.env.http_host;
+               return xstrdup(ctx.env.http_host);
        if (!ctx.env.server_name)
                return NULL;
        if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
-               return ctx.env.server_name;
+               return xstrdup(ctx.env.server_name);
        return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
 }
 
-const char *cgit_currenturl(void)
+char *cgit_currenturl(void)
 {
        if (!ctx.qry.url)
-               return cgit_rooturl();
+               return xstrdup(cgit_rooturl());
        const char *root = cgit_rooturl();
        size_t len = strlen(root);
        if (len && root[len - 1] == '/')
@@ -157,8 +157,11 @@ static void site_url(const char *page, const char *search, const char *sort, int
 
        if (always_root || page)
                html_attr(cgit_rooturl());
-       else
-               html_attr(cgit_currenturl());
+       else {
+               char *currenturl = cgit_currenturl();
+               html_attr(currenturl);
+               free(currenturl);
+       }
 
        if (page) {
                htmlf("?p=%s", page);
@@ -727,7 +730,7 @@ void cgit_print_docstart(void)
                return;
        }
 
-       const char *host = cgit_hosturl();
+       char *host = cgit_hosturl();
        html(cgit_doctype);
        html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
        html("<head>\n");
@@ -746,16 +749,19 @@ void cgit_print_docstart(void)
                html("'/>\n");
        }
        if (host && ctx.repo && ctx.qry.head) {
+               char *fileurl;
                struct strbuf sb = STRBUF_INIT;
                strbuf_addf(&sb, "h=%s", ctx.qry.head);
 
                html("<link rel='alternate' title='Atom feed' href='");
                html(cgit_httpscheme());
-               html_attr(cgit_hosturl());
-               html_attr(cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
-                                      sb.buf));
+               html_attr(host);
+               fileurl = cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
+                                      sb.buf);
+               html_attr(fileurl);
                html("' type='application/atom+xml'/>\n");
                strbuf_release(&sb);
+               free(fileurl);
        }
        if (ctx.repo)
                cgit_add_clone_urls(print_rel_vcs_link);
@@ -765,6 +771,7 @@ void cgit_print_docstart(void)
        html("<body>\n");
        if (ctx.cfg.header)
                html_include(ctx.cfg.header);
+       free(host);
 }
 
 void cgit_print_docend(void)
@@ -997,9 +1004,12 @@ void cgit_print_pageheader(void)
                                        ctx.qry.head, ctx.qry.vpath);
                html("</td><td class='form'>");
                html("<form class='right' method='get' action='");
-               if (ctx.cfg.virtual_root)
-                       html_url_path(cgit_fileurl(ctx.qry.repo, "log",
-                                                  ctx.qry.vpath, NULL));
+               if (ctx.cfg.virtual_root) {
+                       char *fileurl = cgit_fileurl(ctx.qry.repo, "log",
+                                                  ctx.qry.vpath, NULL);
+                       html_url_path(fileurl);
+                       free(fileurl);
+               }
                html("'>\n");
                cgit_add_hidden_formfields(1, 0, "log");
                html("<select name='qt'>\n");
@@ -1014,19 +1024,21 @@ void cgit_print_pageheader(void)
                html("<input type='submit' value='search'/>\n");
                html("</form>\n");
        } else if (ctx.env.authenticated) {
+               char *currenturl = cgit_currenturl();
                site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0, 1);
                if (ctx.cfg.root_readme)
                        site_link("about", "about", NULL, hc("about"),
                                  NULL, NULL, 0, 1);
                html("</td><td class='form'>");
                html("<form method='get' action='");
-               html_attr(cgit_currenturl());
+               html_attr(currenturl);
                html("'>\n");
                html("<input type='text' name='q' size='10' value='");
                html_attr(ctx.qry.search);
                html("'/>\n");
                html("<input type='submit' value='search'/>\n");
                html("</form>");
+               free(currenturl);
        }
        html("</td></tr></table>\n");
        if (ctx.env.authenticated && ctx.qry.vpath) {