<div dir="ltr">Hi,<div><br></div><div>the attached patch adds a new warning that makes memcmp & co warn on misplaced parentheses such as</div><div><br></div><div>  if (memcmp(a, b, sizeof(a) != 0))</div><div><br></div>
<div>like so:</div><div><br></div><div><div>test4.cc:5:30: warning: size argument in 'memcmp' call is a comparison [-Wmemsize-comparison]</div><div>  if (memcmp(a, b, sizeof(a) != 0))</div><div>                   ~~~~~~~~~~^~~~</div>
<div>test4.cc:5:7: note: did you mean to compare the result of 'memcmp' instead?</div><div>  if (memcmp(a, b, sizeof(a) != 0))</div><div>      ^                          ~</div><div>                            )</div>
<div>test4.cc:5:20: note: explicitly cast the argument to size_t to silence this warning</div><div>  if (memcmp(a, b, sizeof(a) != 0))</div><div>                   ^</div><div>                   (size_t)(     )</div><div>
1 warning generated.</div></div><div><br></div><div><br></div><div>This would have found one bug in NSS that we recently fixed [1] and found one more bug in chromium we didn't know about before [2]. It had 0 false positives on all of chromium.</div>
<div><br></div><div>The idea of triggering this warning on a binop in the size argument is due to rnk.</div><div><br></div><div>This warning can possibly be extended later on, but I feel this is a good start.</div><div><br>
</div><div>Thoughts?</div><div><br></div><div>Nico</div><div><br></div><div>[1]: <a href="https://codereview.chromium.org/99423002/diff/1/net/third_party/nss/ssl/ssl3con.c">https://codereview.chromium.org/99423002/diff/1/net/third_party/nss/ssl/ssl3con.c</a></div>
<div>[2]: <a href="https://codereview.chromium.org/8431007/#msg12">https://codereview.chromium.org/8431007/#msg12</a></div></div>