Fix style files handling: stop leaking strings.

Everytime getstyle() was being called, we returned newly allocated
strings without ever freing them.
Now uri stylefiles only get allocated once at setup().

Signed-off-by: Christoph Lohmann <20h@r-36.net>
This commit is contained in:
Quentin Rameau 2015-10-31 12:34:31 +01:00 committed by Christoph Lohmann
parent d486169fa4
commit d84fe71094
1 changed files with 22 additions and 14 deletions

36
surf.c
View File

@ -154,7 +154,7 @@ static const char *getatom(Client *c, int a);
static void gettogglestat(Client *c); static void gettogglestat(Client *c);
static void getpagestat(Client *c); static void getpagestat(Client *c);
static char *geturi(Client *c); static char *geturi(Client *c);
static gchar *getstyle(const char *uri); static const gchar *getstyle(const char *uri);
static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri); static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
@ -645,20 +645,21 @@ geturi(Client *c)
return uri; return uri;
} }
gchar * const gchar *
getstyle(const char *uri) getstyle(const char *uri)
{ {
int i; int i;
if (stylefile != NULL) if (stylefile != NULL)
return g_strconcat("file://", stylefile, NULL); return stylefile;
for (i = 0; i < LENGTH(styles); i++) { for (i = 0; i < LENGTH(styles); i++) {
if (styles[i].regex && !regexec(&(styles[i].re), uri, 0, if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
NULL, 0)) NULL, 0))
return g_strconcat("file://", styles[i].style, NULL); return styles[i].style;
} }
return g_strdup("");
return "";
} }
void void
@ -1285,8 +1286,8 @@ void
setup(void) setup(void)
{ {
int i; int i;
char *proxy; char *proxy, *new_proxy;
char *new_proxy; char *styledirfile, *stylepath;
SoupURI *puri; SoupURI *puri;
SoupSession *s; SoupSession *s;
GError *error = NULL; GError *error = NULL;
@ -1306,8 +1307,8 @@ setup(void)
cookiefile = buildfile(cookiefile); cookiefile = buildfile(cookiefile);
scriptfile = buildfile(scriptfile); scriptfile = buildfile(scriptfile);
cachefolder = buildpath(cachefolder); cachefolder = buildpath(cachefolder);
styledir = buildpath(styledir);
if (stylefile == NULL) { if (stylefile == NULL) {
styledir = buildpath(styledir);
for (i = 0; i < LENGTH(styles); i++) { for (i = 0; i < LENGTH(styles); i++) {
if (regcomp(&(styles[i].re), styles[i].regex, if (regcomp(&(styles[i].re), styles[i].regex,
REG_EXTENDED)) { REG_EXTENDED)) {
@ -1316,11 +1317,19 @@ setup(void)
styles[i].regex); styles[i].regex);
styles[i].regex = NULL; styles[i].regex = NULL;
} }
styles[i].style = buildfile(g_strconcat(styledir, "/", styledirfile = g_strconcat(styledir, "/",
styles[i].style, NULL)); styles[i].style, NULL);
stylepath = buildfile(styledirfile);
styles[i].style = g_strconcat("file://", stylepath,
NULL);
g_free(styledirfile);
g_free(stylepath);
} }
g_free(styledir);
} else { } else {
stylefile = buildfile(stylefile); stylepath = buildfile(stylefile);
stylefile = g_strconcat("file://", stylepath, NULL);
g_free(stylepath);
} }
/* request handler */ /* request handler */
@ -1523,11 +1532,10 @@ void
togglestyle(Client *c, const Arg *arg) togglestyle(Client *c, const Arg *arg)
{ {
WebKitWebSettings *settings = webkit_web_view_get_settings(c->view); WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
char *uri;
enablestyles = !enablestyles; enablestyles = !enablestyles;
uri = enablestyles ? getstyle(geturi(c)) : g_strdup(""); g_object_set(G_OBJECT(settings), "user-stylesheet-uri",
g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL); enablestyles ? getstyle(geturi(c)) : "", NULL);
updatetitle(c); updatetitle(c);
} }