{"id":1210,"date":"2013-04-07T04:01:13","date_gmt":"2013-04-06T18:01:13","guid":{"rendered":"http:\/\/brnz.org\/hbr\/?p=1210"},"modified":"2013-04-07T04:01:13","modified_gmt":"2013-04-06T18:01:13","slug":"bug-of-the-day-tuesday-2013-04-02","status":"publish","type":"post","link":"https:\/\/brnz.org\/hbr\/?p=1210","title":{"rendered":"Bug of the day, Tuesday 2013-04-02"},"content":{"rendered":"<h3>Implicitly finding bugs in other people&#8217;s code<\/h3>\n<p><span style=\"line-height: 1.714285714; font-size: 1rem;\">A crash was reported with a call stack leading to some code I&#8217;d been making changes to a week ago (code that previously featured in <\/span><a style=\"line-height: 1.714285714; font-size: 1rem;\" title=\"Bugs of the day, Friday 2013-03-22\" href=\"https:\/\/brnz.org\/hbr\/?p=1139\">Unexpected use of class<\/a><span style=\"line-height: 1.714285714; font-size: 1rem;\">\u00a0and\u00a0<\/span><a style=\"line-height: 1.714285714; font-size: 1rem;\" title=\"Bugs of the day, Monday 2013-03-25\" href=\"https:\/\/brnz.org\/hbr\/?p=1159\">Initialized, but not in the right order<\/a><span style=\"line-height: 1.714285714; font-size: 1rem;\">).<\/span><\/p>\n<p>The code I&#8217;d changed now looks something like this:<\/p>\n<pre escaped=\"true\" lang=\"cpp\">struct Foo : public Baz {\r\n\u00a0 Bar* m_Bar;\r\n  Foo() : m_Bar(NULL) {}\r\n  void SetBar(Bar* bar) { m_Bar = bar; }\r\n\r\n\u00a0 virtual void Destroy() override;\r\n};<\/pre>\n<p>The cause of the crash was something trying to make use of a null <strong>m_Bar<\/strong> member of a <strong>Foo<\/strong> object.<\/p>\n<p>With regard to the previous crashes I&#8217;d encountered, my first suspicion that this was an object that was somehow being created in a way that had not correctly initialized the <strong>m_Bar<\/strong>. Checking the circumstances of the crash (the reporter was able to find a specific cause, which was a great help), I was unable to see a way that this object could have been created with <strong>m_Bar<\/strong> remaining null.<\/p>\n<h3>Sidebar: A way in which my debugging skills have improved in the last year<\/h3>\n<p>I have identified the following tendency in my thinking: see a bug, take a guess at the cause, and then look for that cause in the following way:<\/p>\n<p>1. Examine some part of the codebase.<br \/>\n2. If expected cause not found, goto 1.<\/p>\n<p>So if I guessed the wrong cause, I get stuck in a loop: &#8220;It must be X, but I haven&#8217;t yet found how X could happen &#8212; I must look harder!&#8221;<\/p>\n<p>Part of this came, I think, from a degree of insecurity about my own ability to read and understand code: &#8220;the bug is in here, but I must have missed something&#8221;<\/p>\n<p>What I&#8217;ve realized in the past year is that my code reading skills aren&#8217;t that bad &#8212; if I haven&#8217;t found the cause I&#8217;m looking for, it&#8217;s more likely that I&#8217;m looking for the wrong cause. Particularly, I am now able to approach a problem with a hypothesis about the cause, and can more clearly identify when I&#8217;ve found some information that invalidates that hypothesis and I should be looking for a different cause.<\/p>\n<p>In the context of this bug, validating how the object was being created and seeing that the codepath would always validly initialize <strong>m_Bar<\/strong> meant that the bug was not in the initialization &#8212; it must be somewhere else.<\/p>\n<h3>The other nuller<\/h3>\n<p>So something else was setting <strong>m_Bar<\/strong> to null after the initialization. There is one other place where that happens&#8230;<\/p>\n<pre escaped=\"true\" lang=\"cpp\">  virtual void Destroy() {\r\n\u00a0 \u00a0 if( m_Bar != NULL) {\r\n    \u00a0 m_Bar-&gt;Destroy();\r\n      m_Bar = NULL;\r\n    } else\r\n\u00a0 \u00a0 \u00a0 HARD_TO_MISS_NOTIFICATION( USEFUL_WARNING_37 );\r\n\u00a0 }<\/pre>\n<p>So, it looks like there&#8217;s a good chance that this object has had its <strong>Destroy()<\/strong> called, and looking through the code, it becomes clear that this is indeed what has happened.<\/p>\n<ul>\n<li><span style=\"line-height: 1.714285714; font-size: 1rem;\">There was a <strong>Validate()<\/strong> function called on our object that checked certain things about that object&#8217;s state, calling <strong>Destroy()<\/strong> if it did not meet certain conditions.<\/span><\/li>\n<li><strong>Validate()<\/strong> was called before attempting to make use of <strong>m_Bar<\/strong>.<\/li>\n<li>The bool returned by <strong>Validate()<\/strong> is ignored by the calling code.<\/li>\n<li><span style=\"line-height: 1.714285714; font-size: 1rem;\">The circumstances that lead to the crash are such that <strong>Validate()<\/strong> will always call <strong>Destroy()<\/strong>.<\/span><\/li>\n<\/ul>\n<p><span style=\"line-height: 1.714285714; font-size: 1rem;\">One <strong>if(!Validate()) return;<\/strong> later, the crash no longer occurs.<\/span><\/p>\n<p>This bug came from a combination of circumstances:<\/p>\n<ul>\n<li><span style=\"line-height: 1.714285714; font-size: 1rem;\">existing code that ignored the logical (? I guess that&#8217;s the right word&#8230;) validity of the object &#8212; it had been destroyed, but still contained valid data, and<\/span><\/li>\n<li><span style=\"line-height: 1.714285714; font-size: 1rem;\">my change with caused the logical state to be reflected in actual state.<\/span><\/li>\n<\/ul>\n<p>I did examine all the other callers of the <strong>Validate()<\/strong> function &#8212; none of these exhibited the same type of problem.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Implicitly finding bugs in other people&#8217;s code A crash was reported with a call stack leading to some code I&#8217;d been making changes to a week ago (code that previously featured in Unexpected use of class\u00a0and\u00a0Initialized, but not in the right order). The code I&#8217;d changed now looks something like this: struct Foo : public &hellip; <a href=\"https:\/\/brnz.org\/hbr\/?p=1210\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bug of the day, Tuesday 2013-04-02&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1210"}],"collection":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1210"}],"version-history":[{"count":4,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1210\/revisions"}],"predecessor-version":[{"id":1246,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1210\/revisions\/1246"}],"wp:attachment":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}