Skip to content

Commit

Permalink
Replace a missing return value by nondet
Browse files Browse the repository at this point in the history
A missing return value is undefined behaviour. Previously the code would return
0, which is unsound, and wouldn't even yield a warning.
  • Loading branch information
tautschnig committed Apr 16, 2018
1 parent 61f14d8 commit 7872f7c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
23 changes: 23 additions & 0 deletions regression/cbmc/return2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
int missing_return(int x)
{
if(x)
return x;

// missing return statement
}

int missing_return_value(int x)
{
if(x)
return x;

return; // missing value
}

int main()
{
__CPROVER_assert(missing_return(0) == 0, "expected to fail");
__CPROVER_assert(missing_return_value(0) == 0, "expected to fail");

return 0;
}
11 changes: 11 additions & 0 deletions regression/cbmc/return2/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
main.c

^EXIT=10$
^SIGNAL=0$
^VERIFICATION FAILED$
\[main.assertion..\] expected to fail: FAILURE$
\*\* 2 of 2 failed
--
^warning: ignoring
\[main.assertion..\] expected to fail: SUCCESS$
10 changes: 4 additions & 6 deletions src/ansi-c/c_typecheck_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Author: Daniel Kroening, [email protected]
#include "c_typecheck_base.h"

#include <util/config.h>
#include <linking/zero_initializer.h>

#include "ansi_c_declaration.h"

Expand Down Expand Up @@ -670,11 +669,10 @@ void c_typecheck_baset::typecheck_return(codet &code)
return_type.id()!=ID_destructor)
{
// gcc doesn't actually complain, it just warns!
// We'll put a zero here, which is dubious.
exprt zero=
zero_initializer(
return_type, code.source_location(), *this, get_message_handler());
code.copy_to_operands(zero);
warning().source_location = code.source_location();
warning() << "non-void function should return a value" << eom;

code.copy_to_operands(side_effect_expr_nondett(return_type));
}
}
else if(code.operands().size()==1)
Expand Down

0 comments on commit 7872f7c

Please sign in to comment.