Codechange: Make sort list function lists safer. (#12574)

GUIList has a pointer only to the start of each sort/filter func list, which has the potential for UB as it is unable to validate that the selected sort or filter type is in range.

Use a std::span instead and check if the selected type is in range before using it.
This commit is contained in:
Peter Nelson
2024-04-25 21:00:49 +01:00
committed by GitHub
parent 9b747a173d
commit 5bc9854be2
14 changed files with 61 additions and 57 deletions

View File

@@ -78,7 +78,7 @@ static bool VehicleIndividualToGroupSorterWrapper(GUIVehicleGroup const &a, GUIV
return func(*(a.vehicles_begin), *(b.vehicles_begin));
}
BaseVehicleListWindow::VehicleGroupSortFunction * const BaseVehicleListWindow::vehicle_group_none_sorter_funcs[] = {
const std::initializer_list<BaseVehicleListWindow::VehicleGroupSortFunction * const> BaseVehicleListWindow::vehicle_group_none_sorter_funcs = {
&VehicleIndividualToGroupSorterWrapper<VehicleNumberSorter>,
&VehicleIndividualToGroupSorterWrapper<VehicleNameSorter>,
&VehicleIndividualToGroupSorterWrapper<VehicleAgeSorter>,
@@ -128,7 +128,7 @@ const StringID BaseVehicleListWindow::vehicle_group_none_sorter_names_wallclock[
INVALID_STRING_ID
};
BaseVehicleListWindow::VehicleGroupSortFunction * const BaseVehicleListWindow::vehicle_group_shared_orders_sorter_funcs[] = {
const std::initializer_list<BaseVehicleListWindow::VehicleGroupSortFunction * const> BaseVehicleListWindow::vehicle_group_shared_orders_sorter_funcs = {
&VehicleGroupLengthSorter,
&VehicleGroupTotalProfitThisYearSorter,
&VehicleGroupTotalProfitLastYearSorter,