Skip to content

Commit 03d003f

Browse files
committed
replace_symbolt: Check type consistency
1 parent 3922701 commit 03d003f

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

src/goto-instrument/dump_c.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,10 @@ void dump_ct::cleanup_harness(code_blockt &b)
923923
if(!ns.lookup("argv'", argv_sym))
924924
{
925925
symbol_exprt argv("argv", argv_sym->type);
926-
replace.insert(argv_sym->symbol_expr(), argv);
926+
// replace argc' by argc in the type of argv['] to maintain type consistency
927+
// while replacing
928+
replace(argv);
929+
replace.insert(symbol_exprt(argv_sym->name, argv.type()), argv);
927930
code_declt d(argv);
928931
decls.add(d);
929932
}

src/util/replace_symbol.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: Daniel Kroening, [email protected]
88

99
#include "replace_symbol.h"
1010

11+
#include "invariant.h"
1112
#include "std_types.h"
1213
#include "std_expr.h"
1314

@@ -23,6 +24,7 @@ void replace_symbolt::insert(
2324
const symbol_exprt &old_expr,
2425
const exprt &new_expr)
2526
{
27+
PRECONDITION(old_expr.type() == new_expr.type());
2628
expr_map.insert(std::pair<irep_idt, exprt>(
2729
old_expr.get_identifier(), new_expr));
2830
}
@@ -34,6 +36,9 @@ bool replace_symbolt::replace_symbol_expr(symbol_exprt &s) const
3436
if(it == expr_map.end())
3537
return true;
3638

39+
DATA_INVARIANT(
40+
s.type() == it->second.type(),
41+
"type of symbol to be replaced should match");
3742
static_cast<exprt &>(s) = it->second;
3843

3944
return false;
@@ -249,6 +254,18 @@ void unchecked_replace_symbolt::insert(
249254
expr_map.emplace(old_expr.get_identifier(), new_expr);
250255
}
251256

257+
bool unchecked_replace_symbolt::replace_symbol_expr(symbol_exprt &s) const
258+
{
259+
expr_mapt::const_iterator it = expr_map.find(s.get_identifier());
260+
261+
if(it == expr_map.end())
262+
return true;
263+
264+
static_cast<exprt &>(s) = it->second;
265+
266+
return false;
267+
}
268+
252269
bool address_of_aware_replace_symbolt::replace(exprt &dest) const
253270
{
254271
const exprt &const_dest(dest);

src/util/replace_symbol.h

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Author: Daniel Kroening, [email protected]
2020
#include <unordered_map>
2121

2222
/// Replace expression or type symbols by an expression or type, respectively.
23+
/// The resolved type of the symbol must match the type of the replacement.
2324
class replace_symbolt
2425
{
2526
public:
@@ -91,6 +92,9 @@ class unchecked_replace_symbolt : public replace_symbolt
9192
}
9293

9394
void insert(const symbol_exprt &old_expr, const exprt &new_expr);
95+
96+
private:
97+
bool replace_symbol_expr(symbol_exprt &dest) const override;
9498
};
9599

96100
/// Replace symbols with constants while maintaining syntactically valid

unit/util/replace_symbol.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ TEST_CASE("Replace all symbols in expression", "[core][util][replace_symbol]")
2323
array_typet array_type(typet("sub-type"), s1);
2424
REQUIRE(array_type.size() == s1);
2525

26-
exprt other_expr("other");
26+
exprt other_expr("other", typet("some_type"));
2727

2828
replace_symbolt r;
2929
REQUIRE(r.empty());

0 commit comments

Comments
 (0)