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

Francisco Chiotta via cfe-dev cfe-dev at lists.llvm.org
Mon Nov 30 17:18:10 PST 2015


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

    }

  }

}

// 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){

      return true;

    }

  }

  return false;

}

void loopExample(IntegerList list){

  char* obj = (char*)malloc(sizeof(char));

  free(obj); <- First free statement.

  if (!hasEvenNumbers(list)){

    walkthrough(list, obj);

  }

  else {

    std::cout << "The list has at least one even number!" << std::endl;

  }

}


Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151130/a676bbc7/attachment.html>


More information about the cfe-dev mailing list