Skip to content

Commit 33be524

Browse files
Correct validation check for return statements from void functions
1 parent 73ff675 commit 33be524

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

src/goto-programs/goto_function.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,36 @@ void get_local_identifiers(
3131
dest.insert(identifier);
3232
}
3333
}
34+
35+
/// Check that the goto function is well-formed
36+
///
37+
/// The validation mode indicates whether well-formedness check failures are
38+
/// reported via DATA_INVARIANT violations or exceptions.
39+
void goto_functiont::validate(const namespacet &ns, const validation_modet vm)
40+
const
41+
{
42+
body.validate(ns, vm);
43+
44+
find_symbols_sett typetags;
45+
find_type_symbols(type, typetags);
46+
const symbolt *symbol;
47+
for(const auto &identifier : typetags)
48+
{
49+
DATA_CHECK(
50+
vm, !ns.lookup(identifier, symbol), id2string(identifier) + " not found");
51+
}
52+
53+
// Check that a void function does not contain any RETURN instructions
54+
if(to_code_type(type).return_type().id() == ID_empty)
55+
{
56+
forall_goto_program_instructions(instruction, body)
57+
{
58+
DATA_CHECK(
59+
vm,
60+
!instruction->is_return(),
61+
"void function should not return a value");
62+
}
63+
}
64+
65+
validate_full_type(type, ns, vm);
66+
}

src/goto-programs/goto_function.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,7 @@ class goto_functiont
116116
///
117117
/// The validation mode indicates whether well-formedness check failures are
118118
/// reported via DATA_INVARIANT violations or exceptions.
119-
void validate(const namespacet &ns, const validation_modet vm) const
120-
{
121-
body.validate(ns, vm);
122-
123-
find_symbols_sett typetags;
124-
find_type_symbols(type, typetags);
125-
const symbolt *symbol;
126-
for(const auto &identifier : typetags)
127-
{
128-
DATA_CHECK(
129-
vm,
130-
!ns.lookup(identifier, symbol),
131-
id2string(identifier) + " not found");
132-
}
133-
134-
validate_full_type(type, ns, vm);
135-
}
119+
void validate(const namespacet &ns, const validation_modet vm) const;
136120
};
137121

138122
void get_local_identifiers(const goto_functiont &, std::set<irep_idt> &dest);

src/util/std_code.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,12 +1135,7 @@ class code_function_callt:public codet
11351135
{
11361136
check(code, vm);
11371137

1138-
if(code.op0().id() == ID_nil)
1139-
DATA_CHECK(
1140-
vm,
1141-
to_code_type(code.op1().type()).return_type().id() == ID_empty,
1142-
"void function should not return value");
1143-
else
1138+
if(code.op0().id() != ID_nil)
11441139
DATA_CHECK(
11451140
vm,
11461141
base_type_eq(

0 commit comments

Comments
 (0)