Skip to content

Commit 991d2b7

Browse files
committed
Fix goto program hash function
The hash function for goto program targets previously dereferenced the target and returned the instruction's location number. However, in some circumstances the location number of an instruction might be modified. If some unordered_map/set is using an iterator to a modified instruction as a key, then the collection invariants will be broken, leading to undefined behaviour. This patch switches to using instruction addresses as hash values, allowing any field in the instruction to be modified without affecting the hash result.
1 parent ef929ea commit 991d2b7

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/goto-programs/goto_program_template.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,10 @@ struct const_target_hash_templatet
807807
{
808808
std::size_t operator()(
809809
const typename goto_program_templatet<codeT, guardT>::const_targett t) const
810-
{ return t->location_number; }
810+
{
811+
using hash_typet = decltype(&(*t));
812+
return std::hash<hash_typet>{}(&(*t));
813+
}
811814
};
812815

813816
/// Functor to check whether iterators from different collections point at the

0 commit comments

Comments
 (0)