Re: [dev] [surf] leak and segfault patches

From: Kris Maglione <maglione.k_AT_gmail.com>
Date: Sun, 25 Jul 2010 11:56:57 -0400

On Sun, Jul 25, 2010 at 03:50:59PM +0100, Rob wrote:
>Hi, I found a few problems when I was messing around with surf this afternoon.
>
>The first patch should fix a memory leak that happens when surf loads a new uri.
>I tried to check with debug flags and valgrind, but, after sifting
>through the usual thousand or so GTK leaks, invalid writes and other
>assorted problems, I found nothing, although the patch makes sense,
>given that 'u' is g_free()'d in one branch of the if-statement, but
>not the other.
>
>The second patch fixes a null pointer dereference that I got when
>refreshing a page while it was still loading. I'm pretty certain 'w'
>was null because valgrind reported line 256 as the error, and the only
>pointer dereference on that line is w->window. Either that or one of
>the function arguments was null and surf segfaulted inside a gtk
>function. Typically, by the time I realised this, the valgrind log was
>lost to the oblivion, so it's tough luck there.

And while you're at it:

diff --git a/surf.c b/surf.c
--- a/surf.c
+++ b/surf.c
@@ -78,7 +78,7 @@ static void drawindicator(Client *c);
  static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
  static void find(Client *c, const Arg *arg);
  static const char *getatom(Client *c, int a);
-static const char *getcookies(SoupURI *uri);
+static char *getcookies(SoupURI *uri);
  static char *geturi(Client *c);
  void gotheaders(SoupMessage *msg, gpointer user_data);
  static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
@@ -153,6 +153,7 @@ runscript(WebKitWebFrame *frame, JSConte
          
          if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
                  jsscript = JSStringCreateWithUTF8CString(script);
+ g_free(script);
                  JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception);
          }
  }
@@ -276,9 +277,9 @@ find(Client *c, const Arg *arg) {
          webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
  }
  
-const char *
+char *
  getcookies(SoupURI *uri) {
- const char *c;
+ char *c;
          SoupCookieJar *j = soup_cookie_jar_text_new(cookiefile, TRUE);
          c = soup_cookie_jar_get_cookies(j, uri, TRUE);
          g_object_unref(j);
@@ -524,12 +525,14 @@ void
  newrequest(SoupSession *s, SoupMessage *msg, gpointer v) {
          SoupMessageHeaders *h = msg->request_headers;
          SoupURI *uri;
- const char *c;
+ char *c;
  
          soup_message_headers_remove(h, "Cookie");
          uri = soup_message_get_uri(msg);
- if((c = getcookies(uri)))
+ if((c = getcookies(uri))) {
                  soup_message_headers_append(h, "Cookie", c);
+ g_free(c);
+ }
          g_signal_connect_after(G_OBJECT(msg), "got-headers", G_CALLBACK(gotheaders), NULL);
  }
  

Not that it matters much when WebKit leaks like a sieve.

-- 
Kris Maglione
Increasingly, people seem to misinterpret complexity as
sophistication, which is baffling---the incomprehensible should cause
suspicion rather than admiration.  Possibly this trend results from a
mistaken belief that using a somewhat mysterious device confers an
aura of power on the user.
	--Niklaus Wirth
Received on Sun Jul 25 2010 - 17:56:57 CEST

This archive was generated by hypermail 2.2.0 : Sun Jul 25 2010 - 18:00:03 CEST