1
0
Fork 0

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.
pull/10299/head
Rubidium 2023-01-06 23:43:50 +01:00 committed by rubidium42
parent bcfe0fb076
commit 90f1768006
13 changed files with 24 additions and 0 deletions

View File

@ -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); uint16 callback = GetVehicleCallback(CBID_VEHICLE_ADDITIONAL_TEXT, 0, 0, engine, nullptr);
if (callback == CALLBACK_FAILED || callback == 0x400) return y; if (callback == CALLBACK_FAILED || callback == 0x400) return y;
const GRFFile *grffile = Engine::Get(engine)->GetGRF(); const GRFFile *grffile = Engine::Get(engine)->GetGRF();
assert(grffile != nullptr);
if (callback > 0x400) { if (callback > 0x400) {
ErrorUnknownCallbackResult(grffile->grfid, CBID_VEHICLE_ADDITIONAL_TEXT, callback); ErrorUnknownCallbackResult(grffile->grfid, CBID_VEHICLE_ADDITIONAL_TEXT, callback);
return y; return y;

View File

@ -925,6 +925,7 @@ DEF_CONSOLE_CMD(ConResetCompany)
return false; return false;
} }
const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER); const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
assert(ci != nullptr);
if (ci->client_playas == index) { if (ci->client_playas == index) {
IConsolePrint(CC_ERROR, "Cannot remove company: the server is connected to that company."); IConsolePrint(CC_ERROR, "Cannot remove company: the server is connected to that company.");
return true; return true;

View File

@ -225,6 +225,7 @@ public:
GameStrings *LoadTranslations() GameStrings *LoadTranslations()
{ {
const GameInfo *info = Game::GetInfo(); const GameInfo *info = Game::GetInfo();
assert(info != nullptr);
std::string basename(info->GetMainScript()); std::string basename(info->GetMainScript());
auto e = basename.rfind(PATHSEPCHAR); auto e = basename.rfind(PATHSEPCHAR);
if (e == std::string::npos) return nullptr; if (e == std::string::npos) return nullptr;

View File

@ -1532,6 +1532,7 @@ static void NetworkAutoCleanCompanies()
if (!_network_dedicated) { if (!_network_dedicated) {
const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER); const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
assert(ci != nullptr);
if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true; 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; if (client_id == CLIENT_ID_SERVER && _network_dedicated) return;
NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id); NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
assert(ci != nullptr);
/* No need to waste network resources if the client is in the company already! */ /* No need to waste network resources if the client is in the company already! */
if (ci->client_playas == company_id) return; if (ci->client_playas == company_id) return;

View File

@ -518,6 +518,7 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object,
{ {
const Vehicle *w = v->Next(); const Vehicle *w = v->Next();
assert(w != nullptr);
uint16 altitude = ClampToU16(v->z_pos - w->z_pos); // Aircraft height - shadow height uint16 altitude = ClampToU16(v->z_pos - w->z_pos); // Aircraft height - shadow height
byte airporttype = ATP_TTDP_LARGE; byte airporttype = ATP_TTDP_LARGE;

View File

@ -1464,6 +1464,9 @@ private:
this->avails.push_back(c); this->avails.push_back(c);
} else { } else {
const GRFConfig *best = FindGRFConfig(c->ident.grfid, HasBit(c->flags, GCF_INVALID) ? FGCM_NEWEST : FGCM_NEWEST_VALID); 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 * If the best version is 0, then all NewGRF with this GRF ID
* have version 0, so for backward compatibility reasons we * have version 0, so for backward compatibility reasons we

View File

@ -1251,6 +1251,7 @@ CommandCost CmdModifyOrder(DoCommandFlag flags, VehicleID veh, VehicleOrderID se
if (sel_ord >= v->GetNumOrders()) return CMD_ERROR; if (sel_ord >= v->GetNumOrders()) return CMD_ERROR;
Order *order = v->GetOrder(sel_ord); Order *order = v->GetOrder(sel_ord);
assert(order != nullptr);
switch (order->GetType()) { switch (order->GetType()) {
case OT_GOTO_STATION: case OT_GOTO_STATION:
if (mof != MOF_NON_STOP && mof != MOF_STOP_LOCATION && mof != MOF_UNLOAD && mof != MOF_LOAD) return CMD_ERROR; if (mof != MOF_NON_STOP && mof != MOF_STOP_LOCATION && mof != MOF_UNLOAD && mof != MOF_LOAD) return CMD_ERROR;

View File

@ -1223,6 +1223,7 @@ public:
this->OrderClick_Nonstop(-1); this->OrderClick_Nonstop(-1);
} else { } else {
const Order *o = this->vehicle->GetOrder(this->OrderGetSel()); const Order *o = this->vehicle->GetOrder(this->OrderGetSel());
assert(o != nullptr);
ShowDropDownMenu(this, _order_non_stop_drowdown, o->GetNonStopType(), WID_O_NON_STOP, 0, 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)); o->IsType(OT_GOTO_STATION) ? 0 : (o->IsType(OT_GOTO_WAYPOINT) ? 3 : 12));
} }
@ -1299,12 +1300,14 @@ public:
case WID_O_COND_COMPARATOR: { case WID_O_COND_COMPARATOR: {
const Order *o = this->vehicle->GetOrder(this->OrderGetSel()); 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); ShowDropDownMenu(this, _order_conditional_condition, o->GetConditionComparator(), WID_O_COND_COMPARATOR, 0, (o->GetConditionVariable() == OCV_REQUIRES_SERVICE) ? 0x3F : 0xC0);
break; break;
} }
case WID_O_COND_VALUE: { case WID_O_COND_VALUE: {
const Order *order = this->vehicle->GetOrder(this->OrderGetSel()); const Order *order = this->vehicle->GetOrder(this->OrderGetSel());
assert(order != nullptr);
uint value = order->GetConditionValue(); uint value = order->GetConditionValue();
if (order->GetConditionVariable() == OCV_MAX_SPEED) value = ConvertSpeedToDisplaySpeed(value); if (order->GetConditionVariable() == OCV_MAX_SPEED) value = ConvertSpeedToDisplaySpeed(value);
SetDParam(0, value); SetDParam(0, value);

View File

@ -27,6 +27,7 @@
if (!IsValid(setting)) return -1; if (!IsValid(setting)) return -1;
const SettingDesc *sd = GetSettingFromName(setting); const SettingDesc *sd = GetSettingFromName(setting);
assert(sd != nullptr);
return sd->AsIntSetting()->Read(&_settings_game); return sd->AsIntSetting()->Read(&_settings_game);
} }
@ -35,6 +36,7 @@
if (!IsValid(setting)) return false; if (!IsValid(setting)) return false;
const SettingDesc *sd = GetSettingFromName(setting); const SettingDesc *sd = GetSettingFromName(setting);
assert(sd != nullptr);
if ((sd->flags & SF_NO_NETWORK_SYNC) != 0) return false; if ((sd->flags & SF_NO_NETWORK_SYNC) != 0) return false;

View File

@ -68,6 +68,7 @@ static const Order *ResolveOrder(VehicleID vehicle_id, ScriptOrder::OrderPositio
if (order_position == ScriptOrder::ORDER_INVALID) return nullptr; if (order_position == ScriptOrder::ORDER_INVALID) return nullptr;
} }
const Order *order = v->GetFirstOrder(); const Order *order = v->GetFirstOrder();
assert(order != nullptr);
while (order->GetType() == OT_IMPLICIT) order = order->next; while (order->GetType() == OT_IMPLICIT) order = order->next;
while (order_position > 0) { while (order_position > 0) {
order_position = (ScriptOrder::OrderPosition)(order_position - 1); order_position = (ScriptOrder::OrderPosition)(order_position - 1);
@ -92,6 +93,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
int res = (int)order_position; int res = (int)order_position;
const Order *order = v->orders->GetFirstOrder(); const Order *order = v->orders->GetFirstOrder();
assert(order != nullptr);
for (; order->GetType() == OT_IMPLICIT; order = order->next) res++; for (; order->GetType() == OT_IMPLICIT; order = order->next) res++;
while (order_position > 0) { while (order_position > 0) {
order_position = (ScriptOrder::OrderPosition)(order_position - 1); 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; if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
const Order *order = ::Vehicle::Get(vehicle_id)->GetOrder(ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position)); const Order *order = ::Vehicle::Get(vehicle_id)->GetOrder(ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position));
assert(order != nullptr);
return order->GetType() == OT_CONDITIONAL; return order->GetType() == OT_CONDITIONAL;
} }
@ -141,6 +144,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
if (!IsValidVehicleOrder(vehicle_id, order_position)) return false; if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
const Order *order = ::ResolveOrder(vehicle_id, order_position); const Order *order = ::ResolveOrder(vehicle_id, order_position);
assert(order != nullptr);
return order->GetType() == OT_DUMMY; return order->GetType() == OT_DUMMY;
} }
@ -172,6 +176,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
if (order_position == ORDER_CURRENT) { if (order_position == ORDER_CURRENT) {
int cur_order_pos = ::Vehicle::Get(vehicle_id)->cur_real_order_index; int cur_order_pos = ::Vehicle::Get(vehicle_id)->cur_real_order_index;
const Order *order = ::Vehicle::Get(vehicle_id)->GetFirstOrder(); const Order *order = ::Vehicle::Get(vehicle_id)->GetFirstOrder();
assert(order != nullptr);
int num_implicit_orders = 0; int num_implicit_orders = 0;
for (int i = 0; i < cur_order_pos; i++) { for (int i = 0; i < cur_order_pos; i++) {
if (order->GetType() == OT_IMPLICIT) num_implicit_orders++; if (order->GetType() == OT_IMPLICIT) num_implicit_orders++;

View File

@ -46,6 +46,7 @@
if (colour != TC_INVALID && (::TextColour)colour >= ::TC_END) return; if (colour != TC_INVALID && (::TextColour)colour >= ::TC_END) return;
Window *w = FindWindowById((::WindowClass)window, number); Window *w = FindWindowById((::WindowClass)window, number);
assert(w != nullptr);
if (widget == WIDGET_ALL) { if (widget == WIDGET_ALL) {
if (colour != TC_INVALID) return; if (colour != TC_INVALID) return;

View File

@ -30,6 +30,7 @@
static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 val, ModifyTimetableFlags mtf, bool timetabled) static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 val, ModifyTimetableFlags mtf, bool timetabled)
{ {
Order *order = v->GetOrder(order_number); Order *order = v->GetOrder(order_number);
assert(order != nullptr);
int total_delta = 0; int total_delta = 0;
int timetable_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; if (v->cur_real_order_index >= v->GetNumOrders()) return;
Order *real_current_order = v->GetOrder(v->cur_real_order_index); Order *real_current_order = v->GetOrder(v->cur_real_order_index);
assert(real_current_order != nullptr);
VehicleOrderID first_manual_order = 0; VehicleOrderID first_manual_order = 0;
for (Order *o = v->GetFirstOrder(); o != nullptr && o->IsType(OT_IMPLICIT); o = o->next) { for (Order *o = v->GetFirstOrder(); o != nullptr && o->IsType(OT_IMPLICIT); o = o->next) {

View File

@ -455,6 +455,7 @@ static std::tuple<CommandCost, uint, uint16, CargoArray> RefitVehicle(Vehicle *v
u->cargo_subtype = result.subtype; u->cargo_subtype = result.subtype;
if (u->type == VEH_AIRCRAFT) { if (u->type == VEH_AIRCRAFT) {
Vehicle *w = u->Next(); Vehicle *w = u->Next();
assert(w != nullptr);
w->refit_cap = std::min<uint16>(w->refit_cap, result.mail_capacity); w->refit_cap = std::min<uint16>(w->refit_cap, result.mail_capacity);
w->cargo_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); if (w->cargo.TotalCount() > w->refit_cap) w->cargo.Truncate(w->cargo.TotalCount() - w->refit_cap);