3 ms·
Something I'd like to add here the actual fix is - + if (rmem > (size + (unsigned int)sk->sk_rcvbuf)) However in reality this would have worked too - + if (rm
by mav3rick 6y ago
Something I'd like to add here the actual fix is -
+ if (rmem > (size + (unsigned int)sk->sk_rcvbuf))
However in reality this would have worked too -
+ if (rmem > (unsigned int)(size + sk->sk_rcvbuf))
(The bit pattern of the result remains the same and it's still casted as unsigned int during the comparison)
However, signed integer overflow is undefined behavior in C and unsigned integer overflow isn't. Hence, the submitted patch is the correct solution
- floatingatoll 6y agoThose are not safe to treat as equivalent, even if it might work in theory. You should always cast as narrowly as possible, and when you see code doing otherwise, look very carefully for bugs. If A + (cast)B is a correct form, then (cast)(A + B) is generally an inappropriate form. As you note, it’s possible it will happen to work, but it’s not good form.
- mav3rick 6y agoYes I qualified it at the end why the former is the correct solution and not the latter.
- floatingatoll 6y agoI figured that "in reality this would have worked too" is a easy phrase to misinterpret as "this would have worked" (assuming they miss the context a paragraph later), and so the reply helps ensure that others do not misread it as I initially did.
- mav3rick 6y agoYes ! thank you.