...
For values of n
where 0 < n <= INT_MAX
, the loop executes n
times, as expected.
For values of n >
where INT_MAX < n <= (size_t)INT_MIN
, the loop executes INT_MAX + 1
times. For each iteration, i
is Once i
becomes negative the loop stops, and i
remains in the range 0
through INT_MAX
. Once i
becomes negative, the loop will stop, because a negative value for i
converts into SIZE_MAX + 1 + i
. Because sizeof(size_t) > sizeof(int)
, SIZE_MAX
is well above INT_MAX * 2
, as well as SIZE_MAX + 1 + INT_MIN
, therefore the size_t
values i
converts to are always greater than values of n
in the range INT_MAX + 1
to INT_MAX * 2
Wiki Markup |
---|
For values of {{n}} where {{(size_t)INT_MIN < n <= SIZE_MAX}}, {{i}} wraps and takes the values {{INT_MIN}} to {{INT_MIN + (n - (size_t)INT_MIN - 1)}}. Execution of the loop overwrites memory from {{p\[INT_MIN\]}} through {{p\[INT_MIN + (n - (size_t)INT_MIN - 1)\]}}. |
Compliant Solution (TR 24731-1)
...