<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">

<channel>
	<title>jjongsma &#187; work</title>
	<atom:link href="http://blogs.gnome.org/jjongsma/tag/work/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.gnome.org/jjongsma</link>
	<description>Hacking on GNOME, but with a healthy dose of C++</description>
	<lastBuildDate>Thu, 18 Aug 2011 14:47:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-nd/3.0/</creativeCommons:license>		<item>
		<title>How to waste an evening debugging the internals of glib&#8217;s qsort implementation</title>
		<link>http://blogs.gnome.org/jjongsma/2009/12/08/qsort-segfault/</link>
		<comments>http://blogs.gnome.org/jjongsma/2009/12/08/qsort-segfault/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 03:49:34 +0000</pubDate>
		<dc:creator>jonner</dc:creator>
				<category><![CDATA[GNOME]]></category>
		<category><![CDATA[gtkmm]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://blogs.gnome.org/jjongsma/?p=79</guid>
		<description><![CDATA[Working on a personal itch-scratching project, I found myself wanting a sortable treeview. So I stuffed my treemodel into a TreeModelSort and set up my sort functions, and everything was happy. Except, when running my application, I kept getting strange segfaults within glib&#8217;s qsort implementation. I had set the following function as my sort_func (yes, [...]]]></description>
			<content:encoded><![CDATA[<p>Working on a personal itch-scratching project, I found myself wanting a sortable treeview.  So I stuffed my treemodel into a TreeModelSort and set up my sort functions, and everything was happy.  Except, when running my application, I kept getting strange segfaults within glib&#8217;s qsort implementation.  I had set the following function as my sort_func (yes, it&#8217;s C++, but it should be fairly self-explanatory):</p>
<pre><code>static int
compare_foo (const Gtk::TreeModel::iterator&#038; a, const Gtk::TreeModel::iterator&#038; b)
{
    std::tr1::shared_ptr&lt;Foo&gt; l1, l2;
    l1 = a-&gt;get_value (COLUMN_FOO);
    l2 = b-&gt;get_value (COLUMN_FOO);

    if (!l1)
        return -1;

    if (!l2)
        return 1;

    return l1-&gt;get_id () - l2-&gt;get_id ();
}</code></pre>
<p>Can you spot the error?  Running it under valgrind indicated that I was reading data from *before* the start of the array that was being sorted.  In fact, the following lines of code in g_qsort_with_data() were causing tmp_ptr to be decreased back past the start of the array:</p>
<pre><code>        while ((*compare_func) ((void *) run_ptr, (void *) tmp_ptr, user_data) &lt; 0)
          tmp_ptr -= size;</code></pre>
<p>Hmm.  Why is there no guard to prevent tmp_ptr from being decreased past the start?  Then I noticed the following comment up a few lines:</p>
<pre><code>    /* Find smallest element in first threshold and place it at the
       array's beginning.  This is the smallest array element,
       and the operation speeds up insertion sort's inner loop. */</code></pre>
<p>So at this point, the code is assuming that the very first element of the array is the smallest, so compare_func should always return &gt;= 0 when it reaches the start of the array.  Aha!  So the problem is my compare_func.  It turns out I forgot to handle the single case of both values being NULL.</p>
<pre><code>    l1 = a-&gt;get_value (COLUMN_FOO);
    l2 = b-&gt;get_value (COLUMN_FOO);
+
+   if (!l1 &#038;&#038; !l2)
+       return 0;

    if (!l1)
        return -1;</code></pre>
<p>So there&#8217;s not really a big lesson to be learned here, but if you ever hit strange segfaults deep inside glib&#8217;s qsort while sorting a treemodel, do yourself a favor and double-check that your sort function is sane first.  Also, do yourself a favor and run it under valgrind as soon as you get a strange segfault.  Knowing the exact point of the invalid read is infinitely more helpful than waiting until that invalid read causes a segfault.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gnome.org/jjongsma/2009/12/08/qsort-segfault/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  blogs.gnome.org/jjongsma/tag/work/feed/ ) in 1.20513 seconds, on Feb 10th, 2012 at 5:13 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 10th, 2012 at 6:13 am UTC -->
