Skip to content

Commit 8a7ef67

Browse files
author
thk123
committed
Store error in string to avoid dangling pointers
1 parent 290b867 commit 8a7ef67

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/solvers/flattening/flatten_byte_extract_exceptions.h

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,24 @@ class non_const_array_sizet : public flatten_byte_extract_exceptiont
3636
: flatten_byte_extract_exceptiont("cannot unpack array of non-const size"),
3737
max_bytes(max_bytes),
3838
array_type(array_type)
39-
{
40-
}
41-
42-
const char *what() const noexcept override
4339
{
4440
std::ostringstream error_message;
4541
error_message << runtime_error::what() << "\n";
4642
error_message << "array_type: " << array_type.pretty();
4743
error_message << "\nmax_bytes: " << max_bytes.pretty();
48-
return error_message.str().c_str();
44+
computed_error_message = error_message.str();
45+
}
46+
47+
const char *what() const noexcept override
48+
{
49+
return computed_error_message.c_str();
4950
}
5051

5152
private:
5253
exprt max_bytes;
5354
array_typet array_type;
55+
56+
std::string computed_error_message;
5457
};
5558

5659
class non_byte_alignedt : public flatten_byte_extract_exceptiont
@@ -65,24 +68,27 @@ class non_byte_alignedt : public flatten_byte_extract_exceptiont
6568
struct_type(struct_type),
6669
component(component),
6770
byte_width(byte_width)
68-
{
69-
}
70-
71-
const char *what() const noexcept override
7271
{
7372
std::ostringstream error_message;
7473
error_message << runtime_error::what() << "\n";
7574
error_message << "width: " << byte_width << "\n";
7675
error_message << "component:" << component.get_name() << "\n";
7776
error_message << "struct_type: " << struct_type.pretty();
78-
return error_message.str().c_str();
77+
computed_error_message = error_message.str();
78+
}
79+
80+
const char *what() const noexcept override
81+
{
82+
return computed_error_message.c_str();
7983
}
8084

8185
private:
8286
const struct_typet struct_type;
8387
const struct_union_typet::componentt component;
8488
const mp_integer byte_width;
8589

90+
std::string computed_error_message;
91+
8692
};
8793

8894
class non_constant_widtht : public flatten_byte_extract_exceptiont
@@ -93,20 +99,23 @@ class non_constant_widtht : public flatten_byte_extract_exceptiont
9399
: flatten_byte_extract_exceptiont("cannot unpack object of non-constant width"),
94100
src(src),
95101
max_bytes(max_bytes)
96-
{
97-
}
98-
99-
const char *what() const noexcept override
100102
{
101103
std::ostringstream error_message;
102104
error_message << runtime_error::what() << "\n";
103105
error_message << "array_type: " << src.pretty();
104106
error_message << "\nmax_bytes: " << max_bytes.pretty();
105-
return error_message.str().c_str();
107+
computed_error_message = error_message.str();
108+
}
109+
110+
const char *what() const noexcept override
111+
{
112+
return computed_error_message.c_str();
106113
}
107114
private:
108115
exprt src;
109116
exprt max_bytes;
117+
118+
std::string computed_error_message;
110119
};
111120

112121
class non_const_byte_extraction_sizet : public flatten_byte_extract_exceptiont
@@ -117,18 +126,21 @@ class non_const_byte_extraction_sizet : public flatten_byte_extract_exceptiont
117126
"byte_extract flatting with non-constant size"),
118127
unpack_expr(unpack_expr)
119128
{
129+
std::ostringstream error_message;
130+
error_message << runtime_error::what() << "\n";
131+
error_message << "unpack_expr: " << unpack_expr.pretty();
132+
computed_error_message = error_message.str();
120133
}
121134

122135
const char *what() const noexcept override
123136
{
124-
std::ostringstream error_message;
125-
error_message << runtime_error::what() << "\n";
126-
error_message << "unpack_expr: " << unpack_expr.pretty();
127-
return error_message.str().c_str();
137+
return computed_error_message.c_str();
128138
}
129139

130140
private:
131141
const byte_extract_exprt unpack_expr;
142+
143+
std::string computed_error_message;
132144
};
133145

134146
#endif //CPROVER_SOLVERS_FLATTENING_FLATTEN_BYTE_EXTRACT_EXCEPTIONS_H

0 commit comments

Comments
 (0)