From a1c0c9904568c5c62d73980b67982557763cc628 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Wed, 11 Nov 2015 19:30:04 +0100 Subject: [PATCH] nitpicker: fix corner case in view stack operation This patch addresses the corner case that the specified neighbor view is the first view of the view stack. If this is the case when inserting the view in front of the neighbor (behind == false), the target position within the view stack must be a null pointer, not the first view. Because the conditions have become rather complicated, both cases of 'behind' are handled separately now. --- repos/os/src/server/nitpicker/view_stack.cc | 34 ++++++++++++--------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/repos/os/src/server/nitpicker/view_stack.cc b/repos/os/src/server/nitpicker/view_stack.cc index f803de9a5..2f10853d1 100644 --- a/repos/os/src/server/nitpicker/view_stack.cc +++ b/repos/os/src/server/nitpicker/view_stack.cc @@ -61,28 +61,32 @@ Rect View_stack::_outline(View const &view) const View const *View_stack::_target_stack_position(View const *neighbor, bool behind) { - View const *cv = _first_view(); + if (behind) { - for (; cv; cv = _next_view(*cv)) { + if (!neighbor) + return nullptr; - /* bring view to front? */ - if (behind && !neighbor) - break; + /* find target position behind neighbor */ + for (View const *cv = _first_view(); cv; cv = _next_view(*cv)) + if (cv == neighbor) + return cv; - /* insert view after cv? */ - if (behind && (cv == neighbor)) - break; + } else { - /* insert view in front of cv? */ - if (!behind && (_next_view(*cv) == neighbor)) - break; + if (neighbor == _first_view()) + return nullptr; - /* insert view in front of the background? */ - if (!behind && !neighbor && _next_view(*cv)->background()) - break; + /* find target position in front of neighbor */ + for (View const *cv = _first_view(), *next = nullptr; cv; cv = next) { + + next = _next_view(*cv); + if (!next || next == neighbor || next->background()) + return cv; + } } - return cv ? cv : _first_view(); + /* we should never reach this point */ + return nullptr; }