<div dir="ltr">Hi,<div><br></div><div>Since the indexing is only known at runtime, out-of-bounds may or may not happen, and it's not possible to detect this statically. Therefore you need to resort to runtime checks, like UB sanitizer, to detect if indeed that's the case.</div><div><br></div><div>What is possible to detect statically is doing indexing operations using variables (not compile-time constants) as indices. You can use clang-tidy's cppcoreguidelines-pro-bounds-constant-array-index to get warnings about code that performs indexing without compile-time constants:</div><div><br></div><div><a href="https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-bounds-constant-array-index.html">https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-bounds-constant-array-index.html</a><br></div><div><br></div><div>Example:</div><div><a href="https://godbolt.org/z/4McYx8vKs">https://godbolt.org/z/4McYx8vKs</a><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jan 25, 2022 at 9:48 AM phy coder via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I'm tryin to using clang static analyzer tool for array bound checking but it seems fail , Here is an example : <br></div><div><br></div><div>#include <iostream> <br></div><div><br></div><div>int main() {</div><div>int size;</div><div>std::cin >> size;</div><div><br></div><div>int array[size];</div><div><br></div><div>for(int i = 0 ; i < n ; i++){</div><div>std::cin>>array[i] ;<br></div><div>}</div><div><br></div><div>int test[20] = {9,6,8,9,0,4,5,,3,2,7,8,9,8,12,34,87,43,65,32,23} ; // Just for testing <br></div><div>for(int i = 0 ; i < size; i++){</div><div>std::cout << test[array[i]] << " " ;<br></div><div>}</div><div><br></div><div>return 0;<br></div><div>}</div><div><br></div><div>There is a bug in this program that we are using the value of "array" as an index for the "test array"  , the value of "array" depends on the user and it's very obvious that the user might enter the negative value . When I used the clang static analyzer (--analyze) , it's not able to catch this and I didn't find any CSA checker for array bounds bug on the site . I also tried using the undefined behavior array bound checker for it , it's working when the user enters any negative value otherwise it was quite af . To be honest I didn't expect that this bug would be caught by UBsanitizer . But I was expecting this with clang static analyzer which seems to fail . <br></div><div><br></div><div>Sorry , If any tool exists for this which I wasn't aware of .  Le'me know if there is any tool that exists for this . <br></div><div><br></div><div>Thank you .<br></div></div>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>