Skip to content

Expr-depth-iterator: support iterating over mutated expressions - #4385

Merged
tautschnig merged 2 commits into
diffblue:developfrom
smowton:smowton/fix/expr-iterator-mutated
Mar 14, 2019
Merged

tautschnig merged 2 commits into
diffblue:developfrom
smowton:smowton/fix/expr-iterator-mutated

Conversation

@smowton

@smowton smowton commented Mar 14, 2019

Copy link
Copy Markdown
Contributor

Expr-iterator has always had a mutate() function to enable editing part of an expression tree, and a "next_sibling_or_parent" method to skip walking over the changed expression. However as it turns out using the latter with the former was effectively mandatory because when an expression was replaced wholesale (e.g. it.mutate() = symbol_exprt("something_different", ...)), then a stale iterator referring to the replaced expr's operands vector led to a segfault on the next operator++.

This takes the simple approach to fixing that: use integer indices instead of iterators. I also add some basic unit tests, including one that exhibits the crash in question.

@tautschnig tautschnig left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest that @LAJW also takes a look, please?

Comment thread src/util/expr_iterator.h Outdated
operands_iteratort it,
operands_iteratort end):
expr(expr), it(it), end(end) { }
inline depth_iterator_expr_statet(const exprt &expr) : expr(expr), op_idx(0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing explicit (and you can get rid of the inline, which is actually implicit...).

Comment thread src/util/expr_iterator.h Outdated
// Get iterators into the operands of the new expr corresponding to the
// ones into the operands of the old expr
const auto it = operands.end() - (state.end - state.it);
(void)expr->operands();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe that should be (void)expr->write(); instead of a somewhat random function (operands)?

@smowton
smowton force-pushed the smowton/fix/expr-iterator-mutated branch 2 times, most recently from 04d1223 to fc35a18 Compare March 14, 2019 15:56

@allredj allredj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/util/expr_iterator.h Outdated
expr = &*it;
}
if(!(state == m_stack.back()))
expr = &const_cast<exprt &>(state.expr.get().operands()[state.op_idx]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Why is another const_cast necessary here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, replaced with the synonym *expr

smowton added 2 commits March 14, 2019 18:09
The iterators are appealing, but are invalidated when an expression is replaced via mutate(),
preventing iteration from continuing over the newly altered node.
@smowton
smowton force-pushed the smowton/fix/expr-iterator-mutated branch from fc35a18 to 17bee52 Compare March 14, 2019 18:09

@allredj allredj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tautschnig
tautschnig merged commit 4fa790b into diffblue:develop Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants