intmain(int argc, char* argv[]){ int *buff = malloc(SIZE * sizeof(int)); int status = 0; ... //code that does not free buff return status; //buff is never closed }
第二种是有的路径释放了,而有的路径上没有释放,如下
1 2 3 4 5 6 7 8 9
int* f(){ try { int *buff = malloc(SIZE*sizeof(int)); do_stuff(buff); return buff; } catch (int do_stuff_exception) { returnNULL; //returns NULL on error, but does not free memory } }
from DataFlow::Node source,DataFlow::Node sink,FunctionCall fc where exists( | DataFlow::localFlow(source, sink) and source.asExpr() instanceof FunctionCall and check_name_pre(source.asExpr().(FunctionCall).getTarget()) and check_name_post(fc.getTarget()) and fc.getAnArgument()=sink.asExpr() ) select source.asExpr(),source.asExpr().getLocation()
from FunctionCall func,int total,int freed,DataFlow::Node sc,DataFlow::Node sk where total = count(FunctionCall fc_pre | check_name_pre(fc_pre.getTarget())) and freed = count(DataFlow::Node source | exists(DataFlow::Node sink,FunctionCall fc_post| DataFlow::localFlow(source, sink) and source.asExpr() instanceof FunctionCall and check_name_pre(source.asExpr().(FunctionCall).getTarget()) and check_name_post(fc_post.getTarget()) and fc_post.getAnArgument()=sink.asExpr() )) and freed!=total and freed*100/total > 80 and exists(| DataFlow::localFlow(sc, sk) and sc.asExpr() instanceof FunctionCall and check_name_pre(sc.asExpr().(FunctionCall).getTarget()) and check_name_post(func.getTarget()) and func.getAnArgument()=sk.asExpr() ) select sc.asExpr(),sc.asExpr().getLocation(),total,freed