From 02eb8de191cde6675df9983ee84df026a37ec2c6 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 9 Jun 2016 12:08:47 +0100 Subject: [PATCH] =?UTF-8?q?Rewrote=20Window::setNextFocusable()=20to=20add?= =?UTF-8?q?ress=20a=20bug=20in=20the=20management=20of=20the=20search=20it?= =?UTF-8?q?erator.=20Covertiy=20reported=20bug=20"Undefined=20behavior=20m?= =?UTF-8?q?ay=20result;=20the=20program=20may=20crash=20or=20subtly=20misb?= =?UTF-8?q?ehave.=20In=20osgWidget::=E2=80=8BWindow::=E2=80=8BsetNextFocus?= =?UTF-8?q?able():=20An=20invalid=20or=20past-the-end=20iterator=20is=20be?= =?UTF-8?q?ing=20used."?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/osgWidget/Window.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/osgWidget/Window.cpp b/src/osgWidget/Window.cpp index 4181d7074..b004fadd4 100644 --- a/src/osgWidget/Window.cpp +++ b/src/osgWidget/Window.cpp @@ -758,25 +758,28 @@ bool Window::setFirstFocusable() { return false; } -bool Window::setNextFocusable() { +bool Window::setNextFocusable() +{ WidgetList focusList; if(!getFocusList(focusList)) return false; - WidgetList::iterator w = focusList.begin(); - // TODO: This needs to be a more complicated object, since the focus may be // in a child Window instead of a Widget. - unsigned int focusedIndex = 0; - - for(unsigned int i = 0; w != focusList.end(); w++, i++) if(*w == _focused) { - focusedIndex = i; - - break; + WidgetList::iterator witr; + for(witr = focusList.begin(); + witr != focusList.end(); + ++witr) + { + if (*witr==_focused) + { + // found current focused widget, move to next widget the one we want to focus on + ++witr; + break; + } } - if(focusedIndex < focusList.size() - 1) _setFocused((++w)->get()); - + if (witr!=focusList.end()) _setFocused(witr->get()); else _setFocused(focusList.front().get()); return true;