@@ -167,6 +167,7 @@ class TestBufferOverrun : public TestFixture {
167167 TEST_CASE (array_index_77);
168168 TEST_CASE (array_index_78);
169169 TEST_CASE (array_index_79); // #15225
170+ TEST_CASE (array_index_80);
170171 TEST_CASE (array_index_multidim);
171172 TEST_CASE (array_index_switch_in_for);
172173 TEST_CASE (array_index_for_in_for); // FP: #2634
@@ -2074,6 +2075,59 @@ class TestBufferOverrun : public TestFixture {
20742075 ASSERT_EQUALS (" " , errout_str ());
20752076 }
20762077
2078+ void array_index_80 ()
2079+ {
2080+ check (" const int a[5] = {1,2,3,4,5};\n "
2081+ " const int b[5] = {6,7,8,9,0};\n "
2082+ " int f(bool c) {\n "
2083+ " int s = 0;\n "
2084+ " const int N = c ? (sizeof(a) / sizeof(a[0])) : (sizeof(b) / sizeof(b[0]));\n "
2085+ " for (int i = 0; i < N; i++) {\n "
2086+ " s += (c ? a[i] : b[i]);\n "
2087+ " if (i == 4) {}\n "
2088+ " }\n "
2089+ " return s;\n "
2090+ " }\n " );
2091+ ASSERT_EQUALS (" " , errout_str ());
2092+
2093+ check (" const int a[5] = {1,2,3,4,5};\n "
2094+ " int f(bool c) {\n "
2095+ " int s = 0;\n "
2096+ " int N = 4;\n "
2097+ " if (c)\n "
2098+ " N = 5;\n "
2099+ " for (int i = 0; i < N; i++) {\n "
2100+ " s += a[i];\n "
2101+ " if (i == 4) {}\n "
2102+ " }\n "
2103+ " return s;\n "
2104+ " }\n " );
2105+ ASSERT_EQUALS (" " , errout_str ());
2106+
2107+ check (" const int a[5] = {1,2,3,4,5};\n "
2108+ " int f(int N) {\n "
2109+ " int s = 0;\n "
2110+ " for (int i = 0; i < N; i++) {\n "
2111+ " s += a[i];\n "
2112+ " if (i == 4) {}\n "
2113+ " }\n "
2114+ " return s;\n "
2115+ " }\n " );
2116+ ASSERT_EQUALS (" " , errout_str ());
2117+
2118+ check (" const int a[5] = {1,2,3,4,5};\n "
2119+ " int f(int N, int i) {\n "
2120+ " if (i < N) {\n "
2121+ " if (i == 4) {}\n "
2122+ " i++;\n "
2123+ " if (i < N)\n "
2124+ " return a[i];\n "
2125+ " }\n "
2126+ " return 0;\n "
2127+ " }\n " );
2128+ ASSERT_EQUALS (" " , errout_str ());
2129+ }
2130+
20772131 void array_index_multidim () {
20782132 check (" void f()\n "
20792133 " {\n "
0 commit comments