<br><br><div class="gmail_quote">On Thu, Jul 21, 2011 at 1:33 PM, Eli Friedman <span dir="ltr"><<a href="mailto:eli.friedman@gmail.com">eli.friedman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Wed, Jul 20, 2011 at 11:39 AM, Kaelyn Uhrain <<a href="mailto:rikka@google.com">rikka@google.com</a>> wrote:<br>
> I've attached an updated version of my patch that better handles cases where<br>
> pointer arithmetic is done after casting a constant-size array to a pointer<br>
> for a smaller base type (e.g. casting an int array to char*). Of the pointer<br>
> arithmetic warnings, about 24% could be considered false positives; however,<br>
> the actual number of false positives is quite small and 2/3 of them stem<br>
> from the use of a single macro--if you count those as a single warning &<br>
> false positive, the rate drops to 17%. Of the false positives most are from<br>
> semi-questionable pointer arithmetic where a constant greater than the<br>
> length of the array/pointer is being added to the pointer and some int > 1<br>
> being subtracted from it, e.g.:<br>
><br>
> void foo(int n) {<br>
>   char x[5];<br>
>   if (n > 0) bar(x + 6 - n);<br>
> }<br>
<br>
</div>Strictly speaking, that isn't a false positive... I can't think of how<br>
we would actually produce anything other than the expected result, but<br>
it has undefined behavior, and the IR we generate for it has undefined<br>
behavior.<br></blockquote><div><br>I oversimplified my example, as the main case of the above form that is a false positive is:<br><br>char buffer[5]; buffer + sizeof("Hello")-1<br><br>where you have pointer arith done with multiple constants that can be folded together. I'm trying to figure out a way to deal with it given that adding parens--buffer + (sizeof("Hello")-1)--silences the warning as it changes the AST so that the "-" binop is the right expr of the "+" binop instead of the "+" binop being the left expr of the "-" binop.<br>
<br>In the case I originally gave I agree that it isn't strictly a false positive even if n <= 6 is guaranteed, and it is a warning that can be silenced by judicious use of parens: x + (6 - n) won't trigger the warning since (6 - n) isn't a constant integer expr.<br>
<br>Cheers,<br>Kaelyn<br></div></div>