Reverse loop order with variable upper boundary not handled correctly by analyzer
| Bugzilla Link | 12531 |
| Version | trunk |
| OS | Linux |
| CC | @ekatz,@Florob,@magcius,@tkremenek |
Extended Description
Consider this code:
void f(int j) { void *buf[j]; int i; if (j < 0) return; for (i = 0; i < j; ++i) buf[i] = malloc(100); #if WORKING for (i = 0; i < j; ++i) #else for (i = j - 1; i >= 0; ++i) #endif free(buf[i]); }
The two for loops in the #if are equivalent, but the second triggers the "use of potentially uninitialised memory" check.
mentioned in issue llvm/llvm-bugzilla-archive#14797
mentioned in issue llvm/llvm-bugzilla-archive#12675
There are two duplicates with correctly-written loops that show this bug in action. Perhaps they should have not been marked duplicates.
I had the same thought and subsequently re-tested #14797. It seems to have been fixed elsewhere in the meantime.
There are two duplicates with correctly-written loops that show this bug in action. Perhaps they should have not been marked duplicates.
Consider this code:
void f(int j) { void *buf[j]; int i; if (j < 0) return; for (i = 0; i < j; ++i) buf[i] = malloc(100); #if WORKING for (i = 0; i < j; ++i) #else for (i = j - 1; i >= 0; ++i) #endif free(buf[i]); }
The two for loops in the #if are equivalent, but the second triggers the "use of potentially uninitialised memory" check.
Those loops are not equivalent. The analyzer is correct.
The second loop should have --i instead of ++i.
Still reproducable with r200573.
*** Bug llvm/llvm-bugzilla-archive#12675 has been marked as a duplicate of this bug. ***
*** Bug llvm/llvm-bugzilla-archive#14797 has been marked as a duplicate of this bug. ***
cloned to rdar://problem/11238277
I suspect the difference has to do with how loops are unrolled, and the power of the constraint solver. Currently the solver can reason about constraints between symbolic value and constants, but not relationships between symbolic values. In other words, it can better reason about:
i >= 0
but not as well:
i < j
assigned to @tkremenek