From 0de7fd3c2474c76413e2c3d2277b735a348209bd Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 9 Mar 2025 18:05:53 +0100 Subject: [PATCH] Codefix: check the result of dynamic_cast for nullptr --- src/misc_gui.cpp | 1 + src/newgrf_gui.cpp | 1 + src/toolbar_gui.cpp | 4 +++- src/window.cpp | 1 + 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index ee2f3fab45..4e4047e8ed 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -1156,6 +1156,7 @@ void ShowQuery(EncodedString &&caption, EncodedString &&message, Window *parent, if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue; QueryWindow *qw = dynamic_cast(w); + assert(qw != nullptr); if (qw->parent != parent || qw->proc != callback) continue; qw->Close(); diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 19e4ef11b4..19ed032835 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -1925,6 +1925,7 @@ static void NewGRFConfirmationCallback(Window *w, bool confirmed) CloseWindowByClass(WC_GRF_PARAMETERS); CloseWindowByClass(WC_TEXTFILE); NewGRFWindow *nw = dynamic_cast(w); + assert(nw != nullptr); _gamelog.StartAction(GLAT_GRF); _gamelog.GRFUpdate(_grfconfig, nw->actives); // log GRF changes diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 56462d8ff9..83da2abf94 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -1441,7 +1441,9 @@ public: NWidgetBase *nwid = it->get(); nwid->current_x = 0; /* Hide widget, it will be revealed in the next step. */ if (nwid->type == NWID_SPACER) continue; - lookup[dynamic_cast(nwid)->GetIndex()] = std::distance(this->children.begin(), it); + NWidgetCore *nwc = dynamic_cast(nwid); + assert(nwc != nullptr); + lookup[nwc->GetIndex()] = std::distance(this->children.begin(), it); } /* Now assign the widgets to their rightful place */ diff --git a/src/window.cpp b/src/window.cpp index 4faa23342e..9b50cdc793 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -530,6 +530,7 @@ void Window::RaiseButtons(bool autoraise) for (auto &pair : this->widget_lookup) { WidgetType type = pair.second->type; NWidgetCore *wid = dynamic_cast(pair.second); + assert(wid != nullptr); if (((type & ~WWB_PUSHBUTTON) < WWT_LAST || type == NWID_PUSHBUTTON_DROPDOWN) && (!autoraise || (type & WWB_PUSHBUTTON) || type == WWT_EDITBOX) && wid->IsLowered()) { wid->SetLowered(false);