llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Reverse loop order with variable upper boundary not handled correctly by analyzer

Open jsonn opened this issue 14 years ago • 11 comments

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.

jsonn avatar Apr 12 '12 12:04 jsonn

mentioned in issue llvm/llvm-bugzilla-archive#14797

belkadan avatar Nov 26 '21 17:11 belkadan

mentioned in issue llvm/llvm-bugzilla-archive#12675

AnnaZaks avatar Nov 26 '21 16:11 AnnaZaks

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.

Florob avatar Oct 29 '19 16:10 Florob

There are two duplicates with correctly-written loops that show this bug in action. Perhaps they should have not been marked duplicates.

magcius avatar Oct 29 '19 14:10 magcius

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.

ekatz avatar Oct 29 '19 08:10 ekatz

Still reproducable with r200573.

jsonn avatar Feb 04 '14 19:02 jsonn

*** Bug llvm/llvm-bugzilla-archive#12675 has been marked as a duplicate of this bug. ***

AnnaZaks avatar Mar 07 '13 22:03 AnnaZaks

*** Bug llvm/llvm-bugzilla-archive#14797 has been marked as a duplicate of this bug. ***

belkadan avatar Feb 23 '13 02:02 belkadan

cloned to rdar://problem/11238277

tkremenek avatar Apr 12 '12 21:04 tkremenek

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

tkremenek avatar Apr 12 '12 21:04 tkremenek

assigned to @tkremenek

jsonn avatar Apr 12 '12 12:04 jsonn