Skip to content

Commit 6de4be5

Browse files
committed
Return value removal: handle missing declarations more gracefully
If a function is used before it is defined, a signature of int f(void) is assumed. Then trying to use the (possibly non-existent) return value fails during return-statement removal. In such cases, just assume a non-deterministic value is being returned. Found by running C-Reduce on a CSmith-generated example.
1 parent fd8af8a commit 6de4be5

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

regression/cbmc/return7/main.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
void a()
2+
{
3+
// Uses the implicit signature of undefined functions: int c(void)
4+
int b = c();
5+
__CPROVER_assert(b == 0, "expected to fail");
6+
}
7+
void c(void)
8+
{
9+
// Actually... don't return anything
10+
// So the results will be non-deterministic
11+
}
12+
13+
int main(int argc, char **argv)
14+
{
15+
a();
16+
return 0;
17+
}

regression/cbmc/return7/test.desc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
4+
^VERIFICATION FAILED$
5+
^EXIT=10$
6+
^SIGNAL=0$
7+
--
8+
Reason: Check return value
9+
^warning: ignoring

src/goto-programs/remove_returns.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ bool remove_returnst::do_function_calls(
174174
optionalt<symbol_exprt> return_value;
175175

176176
if(!is_stub)
177-
{
178177
return_value = get_or_create_return_value_symbol(function_id);
179-
CHECK_RETURN(return_value.has_value());
180178

179+
if(return_value.has_value())
180+
{
181181
// The return type in the definition of the function may differ
182182
// from the return type in the declaration. We therefore do a
183183
// cast.
@@ -198,7 +198,7 @@ bool remove_returnst::do_function_calls(
198198
// fry the previous assignment
199199
function_call.lhs().make_nil();
200200

201-
if(!is_stub)
201+
if(return_value.has_value())
202202
{
203203
goto_program.insert_after(
204204
t_a,

0 commit comments

Comments
 (0)