Warning C6201: index out of range is not always reported in code analysis


  • Hi

    I have made a very simple Visual C++ project to illustrate the problem. I started with a general empty project of type static library, and then added the following code:

    void f()
    	int buff[25];
    	for (int i = 0; i <= 25; i++) // i exceeds array bound
    		buff[i] = 0; // initialize i
    		if (i == 1) { /* Do something */ }

    This is the same code as used in the documented example for C6201 except that I added the line "if (i == 1) {}".

    When running code analysis without the if statement, the out of range warning is reported as expected. However when running with the if statement, the code analysis reports no issues.
    This happens as long as the if statement contains the index used in the for loop.

    I use the rule set "Microsoft Native Recommended Rules", but have also tried "Mixed" and "Microsoft All Rules".
    I have tried both with VS 2013, update 5 and with VS 2015, update 1.

    Any ideas why the warning is not reported when including the if statement?
    Are there any settings that we have missed?


    Tuesday, January 12, 2016 1:22 PM


All replies