Skip to content

Commit 14cfc08

Browse files
committed
Copy constructor parameter instead of storing reference
Previously we stored a reference to the name_prefix parameter in allocate objects that led to segfaults if it was constructed with a temporary. Now we store a copy instead, which prevents that from happening.
1 parent 3a8b34d commit 14cfc08

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/util/allocate_objects.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ class allocate_objectst
7979
void mark_created_symbols_as_input(code_blockt &init_code);
8080

8181
private:
82-
const irep_idt &symbol_mode;
83-
const source_locationt &source_location;
84-
const irep_idt &name_prefix;
82+
const irep_idt symbol_mode;
83+
const source_locationt source_location;
84+
const irep_idt name_prefix;
8585

8686
symbol_table_baset &symbol_table;
8787
const namespacet ns;

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ SRC += analyses/ai/ai.cpp \
4242
solvers/strings/string_refinement/sparse_array.cpp \
4343
solvers/strings/string_refinement/substitute_array_list.cpp \
4444
solvers/strings/string_refinement/union_find_replace.cpp \
45+
util/allocate_objects.cpp \
4546
util/cmdline.cpp \
4647
util/expr_cast/expr_cast.cpp \
4748
util/expr.cpp \

unit/util/allocate_objects.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test for allocate_objectst
4+
5+
Author: Diffblue Ltd
6+
7+
\*******************************************************************/
8+
9+
#include <util/allocate_objects.h>
10+
#include <util/c_types.h>
11+
#include <util/irep_ids.h>
12+
#include <util/source_location.h>
13+
#include <util/symbol_table.h>
14+
15+
#include <testing-utils/use_catch.h>
16+
17+
TEST_CASE(
18+
"Tests the absence of a bug that crashed allocate_objects",
19+
"[core][util][allocate_objects]")
20+
{
21+
symbol_tablet symtab{};
22+
// Because __a_temp will return a const reference to temporary
23+
// irep_idt, and we stored the reference instead of copying the
24+
// value ...
25+
allocate_objectst allocate_object{
26+
ID_C, source_locationt{}, "__a_temp", symtab};
27+
// This crashed because it tried to access the invalid reference
28+
// to the name_prefix irep_idt
29+
allocate_object.allocate_automatic_local_object(size_type());
30+
}

0 commit comments

Comments
 (0)