Skip to content

Commit 7872f7c

Browse files
committed
Replace a missing return value by nondet
A missing return value is undefined behaviour. Previously the code would return 0, which is unsound, and wouldn't even yield a warning.
1 parent 61f14d8 commit 7872f7c

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

regression/cbmc/return2/main.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
int missing_return(int x)
2+
{
3+
if(x)
4+
return x;
5+
6+
// missing return statement
7+
}
8+
9+
int missing_return_value(int x)
10+
{
11+
if(x)
12+
return x;
13+
14+
return; // missing value
15+
}
16+
17+
int main()
18+
{
19+
__CPROVER_assert(missing_return(0) == 0, "expected to fail");
20+
__CPROVER_assert(missing_return_value(0) == 0, "expected to fail");
21+
22+
return 0;
23+
}

regression/cbmc/return2/test.desc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
\[main.assertion..\] expected to fail: FAILURE$
8+
\*\* 2 of 2 failed
9+
--
10+
^warning: ignoring
11+
\[main.assertion..\] expected to fail: SUCCESS$

src/ansi-c/c_typecheck_code.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Author: Daniel Kroening, [email protected]
1212
#include "c_typecheck_base.h"
1313

1414
#include <util/config.h>
15-
#include <linking/zero_initializer.h>
1615

1716
#include "ansi_c_declaration.h"
1817

@@ -670,11 +669,10 @@ void c_typecheck_baset::typecheck_return(codet &code)
670669
return_type.id()!=ID_destructor)
671670
{
672671
// gcc doesn't actually complain, it just warns!
673-
// We'll put a zero here, which is dubious.
674-
exprt zero=
675-
zero_initializer(
676-
return_type, code.source_location(), *this, get_message_handler());
677-
code.copy_to_operands(zero);
672+
warning().source_location = code.source_location();
673+
warning() << "non-void function should return a value" << eom;
674+
675+
code.copy_to_operands(side_effect_expr_nondett(return_type));
678676
}
679677
}
680678
else if(code.operands().size()==1)

0 commit comments

Comments
 (0)