{"id":1097,"date":"2007-04-18T07:00:26","date_gmt":"2007-04-18T12:00:26","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=1097"},"modified":"2007-11-29T07:34:18","modified_gmt":"2007-11-29T12:34:18","slug":"does-not-compute","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=1097","title":{"rendered":"Does Not Compute"},"content":{"rendered":"<p>A bit of code-blogging.  <\/p>\n<p>I spent some of yesterday afternoon debugging a very mundane bit of code.  Nothing special. A bunch of memory space was getting trashed, as poorly managed memory is wont to do.  I picked through the code and found some appaling bugs.  (Which I had written.)  The full code was a couple of pages long, but the thing boiled down to this:<\/p>\n<p><tt><\/p>\n<pre>void DoSomething (void *thing_in)\r\n{\r\n\r\n    void       *thing1;\r\n    void       *thing2;\r\n\r\n    thing1 = malloc (sizeof (*thing_in));\r\n    \/\/Do some stuff with thing_in and thing1.\r\n    thing2 = malloc (thing1, some_new_size);\r\n    free (thing1);\r\n    \/\/Do some other, unrelated stuff. Then...\r\n    memcpy (thing2, thing1, sizeof (*thing1));\r\n    \/\/now that i just got done copying a free'd block of memory to thing2,\r\n    \/\/I'll do some more stuff with thing2.\r\n    free (thing1);\r\n    free (thing2);\r\n\r\n}<\/pre>\n<p><\/tt><\/p>\n<p>Now, the actual code had all sorts of crazy stuff going on between these steps that obfuscated the mistake, but at the heart of it this is what I was doing:  I was free()ing blocks of memory more than once, and copying stuff from recently unallocated blocks of memory.  Now, this in and of itself is not very remarkable.  If you lose your head you can easily make a blunder like this.  The thing is, it should result in a crash.  What I&#8217;m doing above is programatic sepuku.<\/p>\n<p>But in this case the program did <em>not<\/em> crash. At least, not every time and certainly not right away. Not only did it <em>not<\/em> instantly die when it executed this code, but it more or less did what it was supposed to, or failed in innocuous ways that weren&#8217;t readily apparent. It called this function several dozen times a day, and yet the program <em>ran on like this for a span of nine months without anyone noticing something was seriously amiss.<\/em>   <\/p>\n<p>There was another program that would restart this one in the event that it crashed.  I don&#8217;t know how often that happened.  I&#8217;m afraid to look.  I&#8217;m terrified that I&#8217;ll discover my program ran like this for all that time, like a man going on and living his life without noticing that a couple of his major internal organs have been removed.  This is not supposed to be possible.<\/p>\n<p>The whole thing gave me the willies, really. <\/p>\n<p>LATER: Edited the example to better show what the problem was.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A bit of code-blogging. I spent some of yesterday afternoon debugging a very mundane bit of code. Nothing special. A bunch of memory space was getting trashed, as poorly managed memory is wont to do. I picked through the code and found some appaling bugs. (Which I had written.) The full code was a couple [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[524],"class_list":["post-1097","post","type-post","status-publish","format-standard","hentry","category-general","tag-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/1097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1097"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/1097\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}