Skip to content

Commit de5bbd1

Browse files
committed
allocate_objectst::add_created_symbol: do not use pointers
Use references and symbol names to track created symbols rather than pointers. This avoids the risk of dereferencing pointers to objects that have been deallocated by the time dereferencing takes place. This is just defensive programming, no known bugs fixes or changes in behaviour intended.
1 parent 5bc1d55 commit de5bbd1

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class java_object_factoryt
104104
update_in_placet,
105105
const source_locationt &location);
106106

107-
void add_created_symbol(const symbolt *symbol);
107+
void add_created_symbol(const symbolt &symbol);
108108

109109
void declare_created_symbols(code_blockt &init_code);
110110

@@ -1111,9 +1111,9 @@ void java_object_factoryt::gen_nondet_init(
11111111
}
11121112
}
11131113

1114-
void java_object_factoryt::add_created_symbol(const symbolt *symbol_ptr)
1114+
void java_object_factoryt::add_created_symbol(const symbolt &symbol)
11151115
{
1116-
allocate_objects.add_created_symbol(symbol_ptr);
1116+
allocate_objects.add_created_symbol(symbol);
11171117
}
11181118

11191119
void java_object_factoryt::declare_created_symbols(code_blockt &init_code)
@@ -1578,7 +1578,7 @@ exprt object_factory(
15781578
update_in_placet::NO_UPDATE_IN_PLACE,
15791579
loc);
15801580

1581-
state.add_created_symbol(&main_symbol);
1581+
state.add_created_symbol(main_symbol);
15821582
state.declare_created_symbols(init_code);
15831583

15841584
assert_type_consistency(assignments);

src/ansi-c/c_nondet_symbol_factory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ symbol_exprt c_nondet_symbol_factory(
224224
code_blockt assignments;
225225
state.gen_nondet_init(assignments, main_symbol_expr);
226226

227-
state.add_created_symbol(&main_symbol);
227+
state.add_created_symbol(main_symbol);
228228
state.declare_created_symbols(init_code);
229229

230230
init_code.append(assignments);

src/ansi-c/c_nondet_symbol_factory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ class symbol_factoryt
5656
recursion_sett recursion_set = recursion_sett(),
5757
const bool assign_const = true);
5858

59-
void add_created_symbol(const symbolt *symbol_ptr)
59+
void add_created_symbol(const symbolt &symbol)
6060
{
61-
allocate_objects.add_created_symbol(symbol_ptr);
61+
allocate_objects.add_created_symbol(symbol);
6262
}
6363

6464
void declare_created_symbols(code_blockt &init_code)

src/goto-programs/allocate_objects.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ symbol_exprt allocate_objectst::allocate_automatic_local_object(
116116
symbol_mode,
117117
symbol_table);
118118

119-
symbols_created.push_back(&aux_symbol);
119+
add_created_symbol(aux_symbol);
120120

121121
return aux_symbol.symbol_expr();
122122
}
@@ -153,7 +153,7 @@ exprt allocate_objectst::allocate_dynamic_object_symbol(
153153
symbol_mode,
154154
symbol_table);
155155

156-
symbols_created.push_back(&malloc_sym);
156+
add_created_symbol(malloc_sym);
157157

158158
code_frontend_assignt assign =
159159
make_allocate_code(malloc_sym.symbol_expr(), object_size.value());
@@ -194,7 +194,7 @@ exprt allocate_objectst::allocate_non_dynamic_object(
194194
symbol_table);
195195

196196
aux_symbol.is_static_lifetime = static_lifetime;
197-
symbols_created.push_back(&aux_symbol);
197+
add_created_symbol(aux_symbol);
198198

199199
exprt aoe = typecast_exprt::conditional_cast(
200200
address_of_exprt(aux_symbol.symbol_expr()), target_expr.type());
@@ -215,10 +215,10 @@ exprt allocate_objectst::allocate_non_dynamic_object(
215215

216216
/// Add a pointer to a symbol to the list of pointers to symbols created so far
217217
///
218-
/// \param symbol_ptr: pointer to a symbol in the symbol table
219-
void allocate_objectst::add_created_symbol(const symbolt *symbol_ptr)
218+
/// \param symbol: symbol in the symbol table
219+
void allocate_objectst::add_created_symbol(const symbolt &symbol)
220220
{
221-
symbols_created.push_back(symbol_ptr);
221+
symbols_created.push_back(symbol.name);
222222
}
223223

224224
/// Adds declarations for all non-static symbols created
@@ -228,11 +228,12 @@ void allocate_objectst::declare_created_symbols(code_blockt &init_code)
228228
{
229229
// Add the following code to init_code for each symbol that's been created:
230230
// <type> <identifier>;
231-
for(const symbolt *const symbol_ptr : symbols_created)
231+
for(const auto &symbol_id : symbols_created)
232232
{
233-
if(!symbol_ptr->is_static_lifetime)
233+
const symbolt &symbol = ns.lookup(symbol_id);
234+
if(!symbol.is_static_lifetime)
234235
{
235-
code_declt decl(symbol_ptr->symbol_expr());
236+
code_declt decl{symbol.symbol_expr()};
236237
decl.add_source_location() = source_location;
237238
init_code.add(decl);
238239
}
@@ -246,10 +247,11 @@ void allocate_objectst::mark_created_symbols_as_input(code_blockt &init_code)
246247
{
247248
// Add the following code to init_code for each symbol that's been created:
248249
// INPUT("<identifier>", <identifier>);
249-
for(symbolt const *symbol_ptr : symbols_created)
250+
for(const auto &symbol_id : symbols_created)
250251
{
251-
init_code.add(code_inputt{
252-
symbol_ptr->base_name, symbol_ptr->symbol_expr(), source_location});
252+
const symbolt &symbol = ns.lookup(symbol_id);
253+
init_code.add(
254+
code_inputt{symbol.base_name, symbol.symbol_expr(), source_location});
253255
}
254256
}
255257

src/goto-programs/allocate_objects.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class allocate_objectst
9393
const typet &allocate_type,
9494
const irep_idt &basename_prefix = "tmp");
9595

96-
void add_created_symbol(const symbolt *symbol_ptr);
96+
void add_created_symbol(const symbolt &symbol);
9797

9898
void declare_created_symbols(code_blockt &init_code);
9999

@@ -107,7 +107,7 @@ class allocate_objectst
107107
symbol_table_baset &symbol_table;
108108
const namespacet ns;
109109

110-
std::vector<const symbolt *> symbols_created;
110+
std::vector<irep_idt> symbols_created;
111111

112112
exprt allocate_non_dynamic_object(
113113
code_blockt &assignments,

0 commit comments

Comments
 (0)