{"id":6922,"date":"2006-07-20T00:00:00","date_gmt":"2006-07-19T14:00:00","guid":{"rendered":"http:\/\/michael.ellerman.id.au\/blog\/2006\/07\/20\/not-compiling-your-code-snippets-considered-harmful\/"},"modified":"2007-10-25T13:26:36","modified_gmt":"2007-10-25T03:26:36","slug":"not-compiling-your-code-snippets-considered-harmful","status":"publish","type":"post","link":"https:\/\/michael.ellerman.id.au\/blog\/2006\/07\/20\/not-compiling-your-code-snippets-considered-harmful\/","title":{"rendered":"Not compiling your code snippets considered harmful"},"content":{"rendered":"<p>Paul Wayper <a href=\"http:\/\/www.mabula.net\/tbfw\/blosxom.cgi\/2006\/07\/20#2006-07-20-kernel-programming-considered-harmful\">takes issue<\/a> with the habbit kernel programmers have of #defining things as <code>do { stuff; } while (0)<\/code>.<\/p>\n<p>He points out that this can lead to:<\/p>\n<pre><code>if (x &gt; y)\r\n    exch(x,y);\r\n    call_foobar(x,y);\r\nelse\r\n    x = 0;\r\n<\/code><\/pre>\n<p>And says:<\/p>\n<blockquote><p>.. the exch() call completes the if statement and the call_foobar() call is executed unconditionally. Indenting in this case is worse than a sham, it actively deceives the programmer into thinking that the logic will work when it won&#8217;t.<\/p><\/blockquote>\n<p>Although I agree with his point, that mixing #defines that contain { } and if\/else blocks that don&#8217;t can be dangerous, the example doesn&#8217;t actually compile. To trigger the bug you need:<\/p>\n<pre><code>if (x &gt; y)\r\n    exch(x, y);\r\n    if (x)\r\n        printf(\"hello!\");\r\nelse\r\n    x = 0;\r\n<\/code><\/pre>\n<p>Which after expansion of the macro looks logically like:<\/p>\n<pre><code>if (x &gt; y) {\r\n    .. guts of exch ..\r\n}\r\n\r\nif (x)\r\n    printf(\"hello!\");\r\nelse\r\n    x = 0;\r\n<\/code><\/pre>\n<p>So it&#8217;s still nasty, but in the trivial if\/else case you are safe.<\/p>\n<p>Don&#8217;t miss some more of <a href=\"http:\/\/antonblanchardfacts.com\/\">Paul&#8217;s excellent work<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Paul Wayper takes issue with the habbit kernel programmers have of #defining things as do { stuff; } while (0). He points out that this can lead to: if (x &gt; y) exch(x,y); call_foobar(x,y); else x = 0; And says: .. the exch() call completes the if statement and the call_foobar() call is executed unconditionally. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[17,28],"_links":{"self":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/posts\/6922"}],"collection":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/comments?post=6922"}],"version-history":[{"count":0,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/posts\/6922\/revisions"}],"wp:attachment":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/media?parent=6922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/categories?post=6922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/tags?post=6922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}