Warning: Implicit Backdoor
For those wondering how this works:<p>In C, like most languages, all binary operators on numbers – including arithmetic as well as comparisons – &#x27;natively&#x27; require two operands of the same type.  When you pass operands of different types, the language usually converts one of the operands to the other&#x27;s type.  (Sometimes it converts both operands to a different type instead.)<p>In this case, without the cast, the comparison will be performed by converting the `int` to `size_t`.  The dangerous case is when `num` is negative.  Since `size_t` is unsigned, the conversion will turn negative numbers into very high positive numbers, so `num &gt; limit` will be true, and the function will safely reject the allocation.  On the other hand, with `num &gt; (int)limit`, no conversion occurs; the comparison is performed between two signed integers, and `num &gt; limit` will be false if `num` is negative.<p>It would be better to be more explicit.  Perhaps use two comparisons (the compiler will optimize them into one if possible):<p><pre><code>    if (num &lt; 0 || num &gt; limit)
</code></pre>
Or, better yet, make `allocatebufs` take `size_t` instead of `int` in the first place.  Taking `int` is risky even without the comparison bug: if someone calls the function with a `size_t` on a platform where `size_t` is larger than `int` (i.e. 64-bit platforms), the upper bits will be truncated.  For instance, if the passed-in value is 0x100000001, it will be truncated to 0x1, causing allocatebufs to return a much smaller buffer than expected.<p>If you&#x27;re wondering about the exact rules for what types get converted to what, they&#x27;re very complicated, unnecessarily so.  It mostly boils down to &quot;larger type wins; in case of a tie, unsigned wins over signed&quot;.  But there are exceptions.  Here&#x27;s the spec language:<p>- <a href="http:&#x2F;&#x2F;c0x.coding-guidelines.com&#x2F;6.3.1.8.html" rel="nofollow">http:&#x2F;&#x2F;c0x.coding-guidelines.com&#x2F;6.3.1.8.html</a><p>- <a href="http:&#x2F;&#x2F;c0x.coding-guidelines.com&#x2F;6.3.1.1.html" rel="nofollow">http:&#x2F;&#x2F;c0x.coding-guidelines.com&#x2F;6.3.1.1.html</a>