MainView: Fix possible null-pointer dereference 19/157619/1
authorLukasz Stanislawski <l.stanislaws@samsung.com>
Wed, 25 Oct 2017 09:56:33 +0000 (11:56 +0200)
committerLukasz Stanislawski <l.stanislaws@samsung.com>
Wed, 25 Oct 2017 10:06:53 +0000 (12:06 +0200)
Fix issue spotted by SVACE analysis.
Simplify code by applying small refactoring that will additionally
handle possible null-pointer dereference.

Change-Id: I5d0b871f1e2ee8acba6b7819c781a49b7ad00f7b

clock/inc/View/MainView.h
clock/src/View/MainView.cpp

index f62fa35d6b0d4b0fc8a753aadd84b662bc5b4a30..b37a2123244564b2ad6551e37cf5ffe0588c2964 100644 (file)
@@ -124,8 +124,10 @@ namespace view {
                         * @brief Creates toolbar item
                         * @param[in] icon_path absolute icon path
                         * @param[in] translation msgid
+                        * @param[in] type_ptr pointer to ViewType enum, which will be
+                        * passed as user_data to toolbar callback.
                         */
-                       void CreateToolbarItem(const char *icon_path, const char *msgid, IView **content);
+                       void CreateToolbarItem(const char *icon_path, const char *msgid, const ViewType *type_ptr);
 
                        Evas_Object *window_;
                        Evas_Object *conformant_;
index b70a063bb5420a6b6f1bac0255889e98a5bff877..23e7db857d1f3f2e2fb583ff691ec79d56c60877 100644 (file)
@@ -46,6 +46,11 @@ using namespace utils;
 using namespace model;
 
 
+static const ViewType cAlarm = ViewType::ALARM;
+static const ViewType cWorldClock = ViewType::WORLD_CLOCK;
+static const ViewType cStopWatch = ViewType::STOP_WATCH;
+static const ViewType cTimer = ViewType::TIMER;
+
 static void WinDeleteRequestCb(void *data, Evas_Object *obj, void *event_info)
 {
        elm_win_lower(obj);
@@ -61,23 +66,10 @@ static Eina_Bool naviframe_pop_cb(void *data, Elm_Object_Item *it)
 
 void MainView::ToolbarItemCb(void *data, Evas_Object *obj, void *event_info)
 {
-       ui::IView **page = static_cast<ui::IView **>(data);
+       ViewType *type = static_cast<ViewType*>(data);
        MainView *mainView = static_cast<MainView *>(evas_object_data_get(obj, "parent"));
-       if (!*page) {
-               ViewType type;
-               if (page == &(mainView->alarm_))
-                       type = ViewType::ALARM;
-               else if (page == &(mainView->world_clock_))
-                       type = ViewType::WORLD_CLOCK;
-               else if (page == &(mainView->stop_watch_))
-                       type = ViewType::STOP_WATCH;
-               else if (page == &(mainView->timer_))
-                       type = ViewType::TIMER;
-               else
-                       return;
-
-               mainView->GetView(type);
-       }
+       ui::IView *page = mainView->GetView(*type);
+       if (!page) return;
 
        Evas_Object *layout = elm_object_parent_widget_get(obj);
        Evas_Object *navi = elm_object_parent_widget_get(layout);
@@ -87,8 +79,8 @@ void MainView::ToolbarItemCb(void *data, Evas_Object *obj, void *event_info)
 
        Evas_Object *prev = elm_object_item_content_unset(top);
        evas_object_hide(prev);
-       evas_object_show((*page)->GetEvasObject());
-       elm_object_item_content_set(top, (*page)->GetEvasObject());
+       evas_object_show(page->GetEvasObject());
+       elm_object_item_content_set(top, page->GetEvasObject());
 }
 
 MainView::MainView()
@@ -156,10 +148,10 @@ void MainView::NaviframeAdd()
        elm_object_content_set(conformant_, naviframe_);
 }
 
-void MainView::CreateToolbarItem(const char *icon_path, const char *msgid, IView **content)
+void MainView::CreateToolbarItem(const char *icon_path, const char *msgid, const ViewType *type_ptr)
 {
        Elm_Object_Item *it = elm_toolbar_item_append(toolbar_, icon_path,
-                       Translate::Sprintf(msgid).c_str(), ToolbarItemCb, content);
+                       Translate::Sprintf(msgid).c_str(), ToolbarItemCb, (void*)type_ptr);
        elm_object_item_translatable_text_set(it, msgid);
 
        evas_object_data_set(toolbar_, "parent", this);
@@ -168,13 +160,13 @@ void MainView::CreateToolbarItem(const char *icon_path, const char *msgid, IView
 void MainView::CreateToolbarButtons()
 {
        CreateToolbarItem(TizenAppUtils::GetResourcePath(TizenAppUtils::APP_DIR_RESOURCE,
-                       "images/tabs/clock_tabs_ic_alarm.png"), "IDS_CLOCK_BODY_ALARM", &alarm_);
+                       "images/tabs/clock_tabs_ic_alarm.png"), "IDS_CLOCK_BODY_ALARM", &cAlarm);
        CreateToolbarItem(TizenAppUtils::GetResourcePath(TizenAppUtils::APP_DIR_RESOURCE,
-                       "images/tabs/clock_tabs_ic_worldclock.png"), "IDS_CLOCK_BODY_WORLD_CLOCK", &world_clock_);
+                       "images/tabs/clock_tabs_ic_worldclock.png"), "IDS_CLOCK_BODY_WORLD_CLOCK", &cWorldClock);
        CreateToolbarItem(TizenAppUtils::GetResourcePath(TizenAppUtils::APP_DIR_RESOURCE,
-                       "images/tabs/clock_tabs_ic_stopwatch.png"), "IDS_CLOCK_BODY_STOPWATCH", &stop_watch_);
+                       "images/tabs/clock_tabs_ic_stopwatch.png"), "IDS_CLOCK_BODY_STOPWATCH", &cStopWatch);
        CreateToolbarItem(TizenAppUtils::GetResourcePath(TizenAppUtils::APP_DIR_RESOURCE,
-                       "images/tabs/clock_tabs_ic_timer.png"), "IDS_CLOCK_BODY_TIMER", &timer_);
+                       "images/tabs/clock_tabs_ic_timer.png"), "IDS_CLOCK_BODY_TIMER", &cTimer);
 }
 
 void MainView::CreateToolbar()