[cfe-dev] Fwd: [analyzer] false positive in loop?

Jonathan Roelofs via cfe-dev cfe-dev at lists.llvm.org
Mon Nov 30 19:42:33 PST 2015



On 11/30/15 6:18 PM, Francisco Chiotta via cfe-dev wrote:
> Hi guys,
>
>
> Does this chunk of code represent a false positive? it warns a double
> free. I'm being too ambitious?
>
>
> // Does a walk through the list looking for even numbers. If any,
>
> // it frees obj parameter.
>
> static void walkthrough(IntegerList list, char* obj) {
>
> for (int i = 0; i<list.getSize(); i++) {
>
> int number = list.at(i);
>
> if (number % 2 == 0){
>
> free(obj);  <- Attempt to release free memory

If execution ever reaches here ^,

>
>      }
>
>    }
>
> }
>
> // Tell if the list has at least one even number.
>
> bool hasEvenNumbers(IntegerList list) {
>
> for (int i = 0; i<list.getSize(); i++) {
>
> int number = list.at(i);
>
> if (number % 2 == 0){
>
> returntrue;
>
>      }
>
>    }
>
> returnfalse;
>
> }
>
> void loopExample(IntegerList list){
>
> char* obj = (char*)malloc(sizeof(char));
>
> free(obj); <- First free statement.

it must have passed through here ^... Meaning, if the second statement 
is ever executed, it is always a double free.


Jon

>
> if(!hasEvenNumbers(list)){
>
> walkthrough(list, obj);
>
>    }
>
> else {
>
> std::cout<< "The list has at least one even number!"<< std::endl;
>
>    }
>
> }
>
>
> Thanks!
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>

-- 
Jon Roelofs
jonathan at codesourcery.com
CodeSourcery / Mentor Embedded



More information about the cfe-dev mailing list