Skip to content

Commit 179aacb

Browse files
Factor out memory leak instrumentation into separate function
1 parent f5f870b commit 179aacb

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/analyses/goto_check.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class goto_checkt
103103

104104
void bounds_check(const index_exprt &, const guardt &);
105105
void div_by_zero_check(const div_exprt &, const guardt &);
106+
void memory_leak_check(const irep_idt &function_id);
106107
void mod_by_zero_check(const mod_exprt &, const guardt &);
107108
void undefined_shift_check(const shift_exprt &, const guardt &);
108109
void pointer_rel_check(const exprt &, const guardt &);
@@ -1515,6 +1516,30 @@ void goto_checkt::rw_ok_check(exprt &expr)
15151516
}
15161517
}
15171518

1519+
void goto_checkt::memory_leak_check(const irep_idt &function_id)
1520+
{
1521+
const symbolt &leak = ns.lookup(CPROVER_PREFIX "memory_leak");
1522+
const symbol_exprt leak_expr = leak.symbol_expr();
1523+
1524+
// add self-assignment to get helpful counterexample output
1525+
goto_programt::targett t = new_code.add_instruction();
1526+
t->make_assignment();
1527+
t->code = code_assignt(leak_expr, leak_expr);
1528+
1529+
source_locationt source_location;
1530+
source_location.set_function(function_id);
1531+
1532+
equal_exprt eq(leak_expr, null_pointer_exprt(to_pointer_type(leak.type)));
1533+
1534+
add_guarded_claim(
1535+
eq,
1536+
"dynamically allocated memory never freed",
1537+
"memory-leak",
1538+
source_location,
1539+
eq,
1540+
guardt());
1541+
}
1542+
15181543
void goto_checkt::goto_check(
15191544
goto_functiont &goto_function,
15201545
const irep_idt &_mode)
@@ -1713,30 +1738,11 @@ void goto_checkt::goto_check(
17131738
}
17141739
else if(i.is_end_function())
17151740
{
1716-
if(i.function==goto_functionst::entry_point() &&
1717-
enable_memory_leak_check)
1741+
if(
1742+
i.function == goto_functionst::entry_point() &&
1743+
enable_memory_leak_check)
17181744
{
1719-
const symbolt &leak=ns.lookup(CPROVER_PREFIX "memory_leak");
1720-
const symbol_exprt leak_expr=leak.symbol_expr();
1721-
1722-
// add self-assignment to get helpful counterexample output
1723-
goto_programt::targett t=new_code.add_instruction();
1724-
t->make_assignment();
1725-
t->code=code_assignt(leak_expr, leak_expr);
1726-
1727-
source_locationt source_location;
1728-
source_location.set_function(i.function);
1729-
1730-
equal_exprt eq(
1731-
leak_expr,
1732-
null_pointer_exprt(to_pointer_type(leak.type)));
1733-
add_guarded_claim(
1734-
eq,
1735-
"dynamically allocated memory never freed",
1736-
"memory-leak",
1737-
source_location,
1738-
eq,
1739-
guardt());
1745+
memory_leak_check(i.function);
17401746
}
17411747
}
17421748

0 commit comments

Comments
 (0)