{"id":203,"date":"2016-09-17T17:18:09","date_gmt":"2016-09-18T00:18:09","guid":{"rendered":"http:\/\/www.asrivas.me\/blog\/?p=203"},"modified":"2016-09-18T12:20:38","modified_gmt":"2016-09-18T19:20:38","slug":"more-fun-with-signed-unsigned-comparisons","status":"publish","type":"post","link":"https:\/\/www.asrivas.me\/blog\/more-fun-with-signed-unsigned-comparisons\/","title":{"rendered":"More fun with Signed \/ Unsigned Comparisons"},"content":{"rendered":"<p>Here is why one should be very careful while mixing signed and unsigned integer comparisons. Take a look at this code snippet below:<\/p>\n<pre class=\"tab-size:3 lang:c++ decode:true \" title=\"string::pos comparisons\" >#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n        std::string presidentsOrders = \"NO lets do more sanctions\";\r\n        unsigned int pos;\r\n\r\n        pos = presidentsOrders.find(\"YES\");\r\n        if (pos != std::string::npos) {\r\n                std::cout &lt;&lt; \"Lets nuke North Korea\" &lt;&lt; std::endl;\r\n        } else {\r\n                std::cout &lt;&lt; \"More sanctions (yawn)\" &lt;&lt; std::endl;\r\n        }\r\n        return 0;\r\n}<\/pre>\n<p>The President does not want to nuke North Korea, but lets see how the program actually interprets this. If you try compiling and executing this program in a 32-bit environment, it will work as it should:<\/p>\n<pre class=\"lang:default decode:true \" >abhi@devserver:~\/cpp_string_pos$ .\/a.out \r\nMore sanctions (yawn)\r\nabhi@devserver:~\/cpp_string_pos$\r\n<\/pre>\n<p>Now compile the same program in a 64-bit environment (you can cross-compile using the -m32 or -m64 flag in g++). Here is the result:<\/p>\n<pre class=\"lang:default decode:true \" >abhi@devserver:~\/cpp_string_pos$ g++ cpp_string_pos32.cpp -m64\r\nabhi@devserver:~\/cpp_string_pos$ .\/a.out \r\nLets nuke North Korea\r\nabhi@devserver:~\/cpp_string_pos$<\/pre>\n<p>So we effectively misinterpreted the order to fire the nuclear missile. The problem is that std::string::npos is a size_t type and usually defined as:<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nstatic const size_t npos = -1;\r\n<\/pre>\n<p>This is <code> 0xFFFFFFFF <\/code> on 32-bit, but becomes <code>0xFFFFFFFFFFFFFFFF<\/code> on 64-bit. The comparison on 64-bit is then between the unsigned int pos which is upconverted to <code>0x00000000FFFFFFFF<\/code> and <code>0xFFFFFFFFFFFFFFFF<\/code> causing the check to fail and the nuke getting fired. So the right thing to do here is to use std::string::size_type for <code>pos<\/code> instead which would store the right value in the <code>pos<\/code> variable.  This is of course an exaggerated example, and I do hope that missile systems have more levels of strict checks that need to be cleared before they get fired, but it showcases how disastrous this kind of mistake can be. Poor choices of data types for variables can really run your program into the ground.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is why one should be very careful while mixing signed and unsigned integer comparisons. Take a look at this code snippet below: #include &lt;iostream&gt; #include &lt;string&gt; int main(int argc, char *argv[]) { std::string presidentsOrders = &#8220;NO lets do more sanctions&#8221;; unsigned int pos; pos = presidentsOrders.find(&#8220;YES&#8221;); if (pos != std::string::npos) { std::cout &lt;&lt; &#8220;Lets &#8230; <span class=\"more\"><a class=\"more-link\" href=\"https:\/\/www.asrivas.me\/blog\/more-fun-with-signed-unsigned-comparisons\/\">[Read more&#8230;]<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/posts\/203"}],"collection":[{"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":7,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":215,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions\/215"}],"wp:attachment":[{"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}