From 90f1768006f29b979d7f80cff46e36277c5e3394 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Fri, 6 Jan 2023 23:43:50 +0100 Subject: [PATCH] Codechange: add non-nullptr asserts in cases where it should never be nullptr Though where similar calls are checked for nullptr as in those instances of the use of that function it can actually return nullptr. In other words, write down the assumption that the function never returns nullptr in an assert. --- src/build_vehicle_gui.cpp | 1 + src/console_cmds.cpp | 1 + src/game/game_text.cpp | 1 + src/network/network_server.cpp | 2 ++ src/newgrf_engine.cpp | 1 + src/newgrf_gui.cpp | 3 +++ src/order_cmd.cpp | 1 + src/order_gui.cpp | 3 +++ src/script/api/script_gamesettings.cpp | 2 ++ src/script/api/script_order.cpp | 5 +++++ src/script/api/script_window.cpp | 1 + src/timetable_cmd.cpp | 2 ++ src/vehicle_cmd.cpp | 1 + 13 files changed, 24 insertions(+) diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index eb4860cd94..91b94d652f 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -866,6 +866,7 @@ static uint ShowAdditionalText(int left, int right, int y, EngineID engine) uint16 callback = GetVehicleCallback(CBID_VEHICLE_ADDITIONAL_TEXT, 0, 0, engine, nullptr); if (callback == CALLBACK_FAILED || callback == 0x400) return y; const GRFFile *grffile = Engine::Get(engine)->GetGRF(); + assert(grffile != nullptr); if (callback > 0x400) { ErrorUnknownCallbackResult(grffile->grfid, CBID_VEHICLE_ADDITIONAL_TEXT, callback); return y; diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index e652e64111..65343aba5d 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -925,6 +925,7 @@ DEF_CONSOLE_CMD(ConResetCompany) return false; } const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER); + assert(ci != nullptr); if (ci->client_playas == index) { IConsolePrint(CC_ERROR, "Cannot remove company: the server is connected to that company."); return true; diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp index ca102f9120..85d9b03ced 100644 --- a/src/game/game_text.cpp +++ b/src/game/game_text.cpp @@ -225,6 +225,7 @@ public: GameStrings *LoadTranslations() { const GameInfo *info = Game::GetInfo(); + assert(info != nullptr); std::string basename(info->GetMainScript()); auto e = basename.rfind(PATHSEPCHAR); if (e == std::string::npos) return nullptr; diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index d3cbdf9e3a..e40b973ea5 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -1532,6 +1532,7 @@ static void NetworkAutoCleanCompanies() if (!_network_dedicated) { const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER); + assert(ci != nullptr); if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true; } @@ -1918,6 +1919,7 @@ void NetworkServerDoMove(ClientID client_id, CompanyID company_id) if (client_id == CLIENT_ID_SERVER && _network_dedicated) return; NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id); + assert(ci != nullptr); /* No need to waste network resources if the client is in the company already! */ if (ci->client_playas == company_id) return; diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index af72c65ed1..eeccf30e96 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -518,6 +518,7 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object, { const Vehicle *w = v->Next(); + assert(w != nullptr); uint16 altitude = ClampToU16(v->z_pos - w->z_pos); // Aircraft height - shadow height byte airporttype = ATP_TTDP_LARGE; diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 223c1e21af..89e1a6e5e8 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -1464,6 +1464,9 @@ private: this->avails.push_back(c); } else { const GRFConfig *best = FindGRFConfig(c->ident.grfid, HasBit(c->flags, GCF_INVALID) ? FGCM_NEWEST : FGCM_NEWEST_VALID); + /* Never triggers; FindGRFConfig returns either c, or a newer version of c. */ + assert(best != nullptr); + /* * If the best version is 0, then all NewGRF with this GRF ID * have version 0, so for backward compatibility reasons we diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index ca3207ee73..ab2f9fb647 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -1251,6 +1251,7 @@ CommandCost CmdModifyOrder(DoCommandFlag flags, VehicleID veh, VehicleOrderID se if (sel_ord >= v->GetNumOrders()) return CMD_ERROR; Order *order = v->GetOrder(sel_ord); + assert(order != nullptr); switch (order->GetType()) { case OT_GOTO_STATION: if (mof != MOF_NON_STOP && mof != MOF_STOP_LOCATION && mof != MOF_UNLOAD && mof != MOF_LOAD) return CMD_ERROR; diff --git a/src/order_gui.cpp b/src/order_gui.cpp index 816a1b85b3..134b488960 100644 --- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -1223,6 +1223,7 @@ public: this->OrderClick_Nonstop(-1); } else { const Order *o = this->vehicle->GetOrder(this->OrderGetSel()); + assert(o != nullptr); ShowDropDownMenu(this, _order_non_stop_drowdown, o->GetNonStopType(), WID_O_NON_STOP, 0, o->IsType(OT_GOTO_STATION) ? 0 : (o->IsType(OT_GOTO_WAYPOINT) ? 3 : 12)); } @@ -1299,12 +1300,14 @@ public: case WID_O_COND_COMPARATOR: { const Order *o = this->vehicle->GetOrder(this->OrderGetSel()); + assert(o != nullptr); ShowDropDownMenu(this, _order_conditional_condition, o->GetConditionComparator(), WID_O_COND_COMPARATOR, 0, (o->GetConditionVariable() == OCV_REQUIRES_SERVICE) ? 0x3F : 0xC0); break; } case WID_O_COND_VALUE: { const Order *order = this->vehicle->GetOrder(this->OrderGetSel()); + assert(order != nullptr); uint value = order->GetConditionValue(); if (order->GetConditionVariable() == OCV_MAX_SPEED) value = ConvertSpeedToDisplaySpeed(value); SetDParam(0, value); diff --git a/src/script/api/script_gamesettings.cpp b/src/script/api/script_gamesettings.cpp index 2791aba006..425531cce7 100644 --- a/src/script/api/script_gamesettings.cpp +++ b/src/script/api/script_gamesettings.cpp @@ -27,6 +27,7 @@ if (!IsValid(setting)) return -1; const SettingDesc *sd = GetSettingFromName(setting); + assert(sd != nullptr); return sd->AsIntSetting()->Read(&_settings_game); } @@ -35,6 +36,7 @@ if (!IsValid(setting)) return false; const SettingDesc *sd = GetSettingFromName(setting); + assert(sd != nullptr); if ((sd->flags & SF_NO_NETWORK_SYNC) != 0) return false; diff --git a/src/script/api/script_order.cpp b/src/script/api/script_order.cpp index 1886080aa7..44427bb19b 100644 --- a/src/script/api/script_order.cpp +++ b/src/script/api/script_order.cpp @@ -68,6 +68,7 @@ static const Order *ResolveOrder(VehicleID vehicle_id, ScriptOrder::OrderPositio if (order_position == ScriptOrder::ORDER_INVALID) return nullptr; } const Order *order = v->GetFirstOrder(); + assert(order != nullptr); while (order->GetType() == OT_IMPLICIT) order = order->next; while (order_position > 0) { order_position = (ScriptOrder::OrderPosition)(order_position - 1); @@ -92,6 +93,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr int res = (int)order_position; const Order *order = v->orders->GetFirstOrder(); + assert(order != nullptr); for (; order->GetType() == OT_IMPLICIT; order = order->next) res++; while (order_position > 0) { order_position = (ScriptOrder::OrderPosition)(order_position - 1); @@ -132,6 +134,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr if (!IsValidVehicleOrder(vehicle_id, order_position)) return false; const Order *order = ::Vehicle::Get(vehicle_id)->GetOrder(ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position)); + assert(order != nullptr); return order->GetType() == OT_CONDITIONAL; } @@ -141,6 +144,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr if (!IsValidVehicleOrder(vehicle_id, order_position)) return false; const Order *order = ::ResolveOrder(vehicle_id, order_position); + assert(order != nullptr); return order->GetType() == OT_DUMMY; } @@ -172,6 +176,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr if (order_position == ORDER_CURRENT) { int cur_order_pos = ::Vehicle::Get(vehicle_id)->cur_real_order_index; const Order *order = ::Vehicle::Get(vehicle_id)->GetFirstOrder(); + assert(order != nullptr); int num_implicit_orders = 0; for (int i = 0; i < cur_order_pos; i++) { if (order->GetType() == OT_IMPLICIT) num_implicit_orders++; diff --git a/src/script/api/script_window.cpp b/src/script/api/script_window.cpp index 7969380cb3..a164d1f171 100644 --- a/src/script/api/script_window.cpp +++ b/src/script/api/script_window.cpp @@ -46,6 +46,7 @@ if (colour != TC_INVALID && (::TextColour)colour >= ::TC_END) return; Window *w = FindWindowById((::WindowClass)window, number); + assert(w != nullptr); if (widget == WIDGET_ALL) { if (colour != TC_INVALID) return; diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp index f800b25ae6..a1f4ea4ebe 100644 --- a/src/timetable_cmd.cpp +++ b/src/timetable_cmd.cpp @@ -30,6 +30,7 @@ static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 val, ModifyTimetableFlags mtf, bool timetabled) { Order *order = v->GetOrder(order_number); + assert(order != nullptr); int total_delta = 0; int timetable_delta = 0; @@ -390,6 +391,7 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling) if (v->cur_real_order_index >= v->GetNumOrders()) return; Order *real_current_order = v->GetOrder(v->cur_real_order_index); + assert(real_current_order != nullptr); VehicleOrderID first_manual_order = 0; for (Order *o = v->GetFirstOrder(); o != nullptr && o->IsType(OT_IMPLICIT); o = o->next) { diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index fa1c6e54c7..4c432d9eef 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -455,6 +455,7 @@ static std::tuple RefitVehicle(Vehicle *v u->cargo_subtype = result.subtype; if (u->type == VEH_AIRCRAFT) { Vehicle *w = u->Next(); + assert(w != nullptr); w->refit_cap = std::min(w->refit_cap, result.mail_capacity); w->cargo_cap = result.mail_capacity; if (w->cargo.TotalCount() > w->refit_cap) w->cargo.Truncate(w->cargo.TotalCount() - w->refit_cap);