{"id":48,"date":"2013-10-31T16:16:05","date_gmt":"2013-10-31T16:16:05","guid":{"rendered":"http:\/\/wphost.spider-e.com\/?p=48"},"modified":"2013-10-31T16:37:29","modified_gmt":"2013-10-31T16:37:29","slug":"bug-hunting-malloc-failed","status":"publish","type":"post","link":"https:\/\/wphost.spider-e.com\/?p=48","title":{"rendered":"Bug hunting : malloc failed :-("},"content":{"rendered":"<p>I&#8217;ve been tracking down an elusive bug which was causing one of my devices to occasionally start mis-behaving.\u00a0The device would respond slowly (if at all) to some commands, and respond normally to others.<\/p>\n<p>It was pretty obvious what the issue was when I attached the debugger and started single-stepping through the code while it was misbehaving.<\/p>\n<p>I landed on the &#8216;return&#8217; in this code:<\/p>\n<pre>prevLen = sizeof(LIST_ITEM)+32;\r\n pWorkItem = malloc(prevLen);<\/pre>\n<pre>if(pWorkItem == 0)\r\n return 0;<\/pre>\n<p>so it was obvious I was getting a malloc fail, which meant I had ran out of memory somewhere along the line. The &#8216;return 0&#8217; meant that the command silently failed, without any feedback to the user.<\/p>\n<p><!--more--><\/p>\n<p>I quickly looked through the remainder of this function and found that the memory which was malloc&#8217;d was never freed, so I made two changes.<br \/>\nFirst I ensured that the memory was freed as soon as we were finished with it, and additionally I added some feedback to the external interface like this:<\/p>\n<pre>prevLen = sizeof(LIST_ITEM)+32;\r\n pWorkItem = malloc(prevLen);<\/pre>\n<pre>if(pWorkItem == 0)\r\n {\r\n sprintf(txBuf,\"$ERR:MIM-MALLOC FAIL at %d in %s\\r\",__LINE__, __FILE__);\r\n UART_Merc_PutString(txBuf);\r\n return 0;\r\n }<\/pre>\n<p>The UART_Merc_PutString sends data to a debug terminal.<\/p>\n<p>With these fixes in place it&#8217;s all working sweetly now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been tracking down an elusive bug which was causing one of my devices to occasionally start mis-behaving.\u00a0The device would respond slowly (if at all) to some commands, and respond normally to others. It was pretty obvious what the issue was when I attached the debugger and started single-stepping through the code while it was [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3],"tags":[22,23,21,20],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-software","tag-bug","tag-debug","tag-fail","tag-malloc"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p42WEU-M","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/wphost.spider-e.com\/index.php?rest_route=\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wphost.spider-e.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wphost.spider-e.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wphost.spider-e.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wphost.spider-e.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=48"}],"version-history":[{"count":3,"href":"https:\/\/wphost.spider-e.com\/index.php?rest_route=\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":52,"href":"https:\/\/wphost.spider-e.com\/index.php?rest_route=\/wp\/v2\/posts\/48\/revisions\/52"}],"wp:attachment":[{"href":"https:\/\/wphost.spider-e.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wphost.spider-e.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wphost.spider-e.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}