{"id":450,"date":"2005-08-08T23:31:03","date_gmt":"2005-08-08T13:31:03","guid":{"rendered":"http:\/\/www.flamingspork.com\/blog\/?p=450"},"modified":"2005-08-08T23:31:03","modified_gmt":"2005-08-08T13:31:03","slug":"why-returns-1-on-error-is-bad","status":"publish","type":"post","link":"https:\/\/www.flamingspork.com\/blog\/2005\/08\/08\/why-returns-1-on-error-is-bad\/","title":{"rendered":"Why &#8220;returns -1 on error&#8221; is bad"},"content":{"rendered":"<p>(a general note on what&#8217;s good practice)<\/p>\n<p>In C, 0 is false and !0 is true.<\/p>\n<p>In the dim past there was an elsewhere where 0 was true and !0 was false. Why? Because there can be more than one error state and this is usually more interesting than how many ways success could have been acheived.<\/p>\n<p>Well, that sucks too &#8211; there&#8217;s information on success that could be useful (e.g. we succeeded, but only n bytes worth instead of the m you asked for).<\/p>\n<p>So, the way of &lt;0 on failure and else success came about for packing the maximum amount of information into the int that we commonly return from functions (and usually fits nicely in a register and it all leads to hugs, puppies and a warm feeling inside).<\/p>\n<p>So what do most people do on error? Return -1.<\/p>\n<p>Hrrmm&#8230; this casually (if not totally) defeats the point. In any function that does any real work, there&#8217;s going to be more than one place where failure could occur (even if it&#8217;s an error path that <b>should<\/b> never really happen&#8230; it will, but never to you&#8230; always to a guy somewhere in a country that you didn&#8217;t know existed and knows less $native_language than you have digits).<\/p>\n<p>So if you get a bug report in with a log message (because you <b>do<\/b> print log messages when errors occur! &#8211; especially non-totally-fatal ones!) about a failure, and you go to look at that function and go &#8220;aha! this function must have returned -1!&#8221; Well, it just so happens that there are five places that could return -1. Where did your program fail? Without a core dump or something, you will never know.<\/p>\n<p>So, what if these five places returned <b>different<\/b> error codes (which, of course, you wrote to the log)? Then you&#8217;d be able to narrow down the search for buggy code!<\/p>\n<p>It doesn&#8217;t have to be a unique number, or even user understandable (especially when these are places that shouldn&#8217;t fail &#8211; or so you think) but it makes your job a hell of a lot easier if you can quickly jump to the bit of code you should look at.<\/p>\n<p>In cluster, we have this great system where when really bad stuff happens, we get these nice trace logs of what signals have been cruising around the cluster recently. This greatly helps with debugging. It sort of makes you go &#8220;wow&#8221; when you first see a crash reported, trace file follows, and then a patch a few hrs later that fixes the problem. This is because it&#8217;s an aid in tracking down exactly <b>where<\/b> to look for the problem.<\/p>\n<p>&#8220;It crashed&#8221; is never a useful bug report. But only having the facilities in your software for only being able to say &#8220;it crashed&#8221; unless you&#8217;re a developer guru dude isn&#8217;t very useful either.<\/p>\n<p>The various backtrace reporting tools do a bit to help. As always, the more information the better. This is certainly the case when you look at the backtrace and go &#8220;how on earth did we ever get there?&#8221; or the stack is just completely hosed and you have no hope of finding your arse from your elbow (although these days valgrind will help you here).<\/p>\n<p>Here endith the lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(a general note on what&#8217;s good practice) In C, 0 is false and !0 is true. In the dim past there was an elsewhere where 0 was true and !0 was false. Why? Because there can be more than one &hellip; <a href=\"https:\/\/www.flamingspork.com\/blog\/2005\/08\/08\/why-returns-1-on-error-is-bad\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1,14,7],"tags":[],"class_list":["post-450","post","type-post","status-publish","format-standard","hentry","category-general","category-mysql","category-work-et-al"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5a6n8-7g","jetpack-related-posts":[{"id":1338,"url":"https:\/\/www.flamingspork.com\/blog\/2009\/01\/08\/magic-number-super-fun-happy-time\/","url_meta":{"origin":450,"position":0},"title":"magic number super fun happy time","author":"Stewart Smith","date":"2009-01-08","format":false,"excerpt":"umm..... int Field_timestamp::store(double nr) { int error= 0; if (nr < 0 || nr > 99991231235959.0) { set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, nr, DRIZZLE_TIMESTAMP_DATETIME); nr= 0; \/\/ Avoid overflow on buff error= 1; } error|= Field_timestamp::store((int64_t) rint(nr), false); return error; } (likely the same in mysql as well... haven't checked though). these date\u2026","rel":"","context":"In &quot;drizzle&quot;","block_context":{"text":"drizzle","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/drizzle-work-et-al\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1262,"url":"https:\/\/www.flamingspork.com\/blog\/2008\/11\/22\/drop-table-fail-on-the-road-to-removing-the-frm\/","url_meta":{"origin":450,"position":1},"title":"drop table fail (on the road to removing the FRM)","author":"Stewart Smith","date":"2008-11-22","format":false,"excerpt":"So... in removing the FRM file in Drizzle, I found a bit of a nugget on how drop table works (currently in the MySQL server and now \"did\" in Drizzle). If you DROP TABLE t1; this is what happens open the .frm file read first 10bytes (oh, and if you\u2026","rel":"","context":"In &quot;drizzle&quot;","block_context":{"text":"drizzle","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/drizzle-work-et-al\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4850,"url":"https:\/\/www.flamingspork.com\/blog\/2023\/06\/25\/getting-your-photos-out-of-shotwell\/","url_meta":{"origin":450,"position":2},"title":"Getting your photos out of Shotwell","author":"Stewart Smith","date":"2023-06-25","format":false,"excerpt":"Somewhat a while ago now, I wrote about how every time I return to write some software for the Mac, the preferred language has changed. The purpose of this adventure was to get my photos out of the aging Shotwell and onto my (then new) Mac and the Apple Photos\u2026","rel":"","context":"In &quot;General&quot;","block_context":{"text":"General","link":"https:\/\/www.flamingspork.com\/blog\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.flamingspork.com\/blog\/wp-content\/uploads\/2023\/06\/image.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.flamingspork.com\/blog\/wp-content\/uploads\/2023\/06\/image.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.flamingspork.com\/blog\/wp-content\/uploads\/2023\/06\/image.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.flamingspork.com\/blog\/wp-content\/uploads\/2023\/06\/image.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":280,"url":"https:\/\/www.flamingspork.com\/blog\/2004\/09\/30\/pear-bug-2417-patch-incorrect-processing-of-permission-denied-style-error-messages\/","url_meta":{"origin":450,"position":3},"title":"PEAR :: Bug #2417 :: [PATCH] Incorrect processing of &#8216;permission denied&#8217; style error messages","author":"Stewart Smith","date":"2004-09-30","format":false,"excerpt":"PEAR :: Bug #2417 :: [PATCH] Incorrect processing of 'permission denied' style error messages Got the bugger - and supplied a patch that fixes it! This should spell an end to those annoying (and really unhelpful) \"DB Error: unknown error\" messages from PEAR::DB when the user doesn't have enough permissions.\u2026","rel":"","context":"In &quot;linux-aus&quot;","block_context":{"text":"linux-aus","link":"https:\/\/www.flamingspork.com\/blog\/category\/linux-aus\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2253,"url":"https:\/\/www.flamingspork.com\/blog\/2011\/01\/05\/is-your-storage-engine-buggy-or-the-database-server\/","url_meta":{"origin":450,"position":4},"title":"Is your Storage Engine buggy or the database server?","author":"Stewart Smith","date":"2011-01-05","format":false,"excerpt":"If your storage engine returns an error from rnd_init (or doStartTableScan as it's named in Drizzle) and does not save this error and return it in any subsequent calls to rnd_next, your engine is buggy. Namely it is buggy in that a) an error may not be reported back to\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2382,"url":"https:\/\/www.flamingspork.com\/blog\/2011\/05\/21\/howto-fix-bzr-join-error-of-trees-have-the-same-root\/","url_meta":{"origin":450,"position":5},"title":"HOWTO fix: bzr join error of &#8220;Trees have the same root&#8221;","author":"Stewart Smith","date":"2011-05-21","format":false,"excerpt":"From\u00c2\u00a0https:\/\/answers.launchpad.net\/bzr\/+question\/71563 \u00ef\u00bb\u00bf\u00ef\u00bb\u00bfyou can do it from within Python like this: >>> import bzrlib.workingtree >>> bzrlib.workingtree.WorkingTree.open(\"subdir2\").set_root_id(\"tree_root_subdir2\") Hopefully I can find this easily in the future (have had to use it before)","rel":"","context":"In &quot;General&quot;","block_context":{"text":"General","link":"https:\/\/www.flamingspork.com\/blog\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":0,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}