17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-07 00:22:56 +01:00

fix(editor): guard the places where the editor could crash outright

Found with an address and undefined behaviour sanitizer build, random
monkey sessions on the test sceneries and broken inputs:

- A deleted model left its pointer in the pick list of the renderer, the
  bound node of the panels, the bindings of the infrastructure and the
  selection: the next pick or panel frame read freed memory. The renderer
  forgets it, and one erase_model does the cleaning for all the places
  which deleted a model by hand.
- The plan band of a route read the vertex before the first one in the
  frame a drag was let go.
- A scenery which can't be opened goes back to the launcher with the
  reason in the log, a track of an unknown type or a tributary loads as
  a normal track or a river with a message, and a spline with no finite
  length or step gets a sane number of segments.
- Typed numbers which aren't (NaN, inf, ends 0 m or 1000 km apart, a
  vector of 1e30, a diverging track part of 5 km, a curve threshold of 0,
  a zero history size, radius norms below zero) are refused or clamped,
  where they made the geometry run away or divide by zero.
- Folders which can't be read and file names which aren't valid in the
  locale no longer throw out of the scans of sceneries, textures, sweeps,
  roads and signal plates; the scans skip them.
- The launcher: Ctrl+Del with no trainset selected, a dropped vehicle
  whose skin is gone and a text with a percent sign.
- A terrain mesh made for another resolution of its chunk isn't put into
  the chunk, and a slot is reused only for a mesh of the same size.
- Vehicles on a track changed by the editor are moved over a copy of the
  list they leave.
- The table of the keyboard shortcuts gives its stretched column a
  weight, else ImGui divides 0 by 0 in the first frame of the window.
This commit is contained in:
maj00r
2026-10-21 08:42:29 +02:00
parent 9696b5f795
commit 4512ed4e0b
36 changed files with 311 additions and 111 deletions

View File

@@ -276,7 +276,7 @@ recommendation recommend(double const Speed, double const Radius, limits const &
result.cant_equilibrium = factor * Speed * Speed / Radius;
result.cant_min = std::max(0.0, result.cant_equilibrium - allowance);
result.cant = std::min(Limits.cant_max, ceil_to(std::max(result.cant_min, result.cant_equilibrium * 2.0 / 3.0), 5.0));
result.transition = transition_length(Speed, Radius, result.cant, Limits);
result.transition = std::min(5000.0, transition_length(Speed, Radius, result.cant, Limits));
return result;
}

View File

@@ -117,7 +117,7 @@ bool read_geotiff(std::vector<std::uint8_t> const &Data, Square &Out)
};
auto const width{static_cast<int>(number(256, 0))};
auto const height{static_cast<int>(number(257, 0))};
if (width < 2 || height < 2 || number(258, 0) != 32 || number(339, 0) != 3 || std::max(1u, number(259, 0)) != 1 || false == entries.contains(273) || false == entries.contains(33550) ||
if (width < 2 || height < 2 || width > 8192 || height > 8192 || number(258, 0) != 32 || number(339, 0) != 3 || std::max(1u, number(259, 0)) != 1 || false == entries.contains(273) || false == entries.contains(33550) ||
false == entries.contains(33922))
return false;
auto const rows{std::max(1u, number(278, 0))};

View File

@@ -56,7 +56,7 @@ double grade_after(line const &Line, std::size_t const Index)
double required_radius(line const &Line)
{
return std::max(Line.norms.radius_min, Line.norms.radius_factor * Line.speed * Line.speed);
return std::max({10.0, Line.norms.radius_min, Line.norms.radius_factor * Line.speed * Line.speed});
}
void assign_radii(line &Line)

View File

@@ -2069,11 +2069,20 @@ std::vector<std::string> const &editor_road::materials()
{
if (error || false == file->is_regular_file(error))
continue;
auto extension{file->path().extension().string()};
std::string extension;
std::string name;
try
{
extension = file->path().extension().string();
name = file->path().stem().string();
}
catch (std::exception const &)
{
continue;
}
std::transform(extension.begin(), extension.end(), extension.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
if (extension != ".mat" && extension != ".dds" && extension != ".tga" && extension != ".png" && extension != ".bmp" && extension != ".jpg" && extension != ".ktx" && extension != ".tex")
continue;
auto name{file->path().stem().string()};
std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
// names with spaces can't be put in a scenery file
if (name.empty() || name.find(' ') != std::string::npos)

View File

@@ -160,10 +160,12 @@ std::string checked(std::vector<part> const &Parts)
return "The diverging track has no parts";
for (auto const &part : Parts)
{
if (part.length < 0.05)
if (false == (part.length >= 0.05))
return "A part of the diverging track is shorter than 5 cm";
if (part.length > 1000.0)
return "A part of the diverging track is longer than 1000 m";
for (auto const radius : {part.radius1, part.radius2})
if (radius != 0.0 && std::abs(radius) < kMinRadius)
if (std::isnan(radius) || (radius != 0.0 && std::abs(radius) < kMinRadius))
return "A radius of the diverging track is under 10 m";
}
return {};
@@ -362,9 +364,9 @@ shape build(design const &Design, std::string &Error)
bool const mirrored{Design.type == "Rps"};
if (Error.empty() && twofold && false == mirrored && false == Design.parts2.empty())
Error = checked(Design.parts2);
if (Error.empty() && (Design.lead < 0.0 || Design.lead > 30.0))
if (Error.empty() && false == (Design.lead >= 0.0 && Design.lead <= 30.0))
Error = "The pre-blade stretch has to be between 0 and 30 m";
if (Error.empty() && (Design.entry < 0.0 || Design.entry > 5.0))
if (Error.empty() && false == (Design.entry >= 0.0 && Design.entry <= 5.0))
Error = "The angle of the blade has to be between 0 and 5 degrees";
if (false == Error.empty())
return result;

View File

@@ -19,6 +19,7 @@ http://mozilla.org/MPL/2.0/.
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <set>
#include <sstream>
@@ -167,7 +168,7 @@ void load()
if (entry.kind != "switch" && entry.kind != "slip" && entry.kind != "crossing" && entry.kind != "double" && entry.kind != "connection")
continue;
line >> entry.name >> entry.type >> entry.rail >> entry.radius >> entry.ratio >> hand >> entry.a >> entry.b >> entry.main_radius >> keyword >> count;
if (line.fail() || keyword != "parts" || count > 64)
if (line.fail() || keyword != "parts" || count == 0 || count > 64)
continue;
entry.hand = hand.empty() ? '-' : hand.front();
entry.parts.resize(count);
@@ -202,7 +203,12 @@ void load()
entry.direction = plan_of(ahead - entry.start);
entry.direction = glm::length(entry.direction) > 1e-12 ? glm::normalize(entry.direction) : glm::dvec2{0.0, 1.0};
if (km != "-")
entry.km = std::stod(km) * 1000.0;
{
char *end{nullptr};
auto const value{std::strtod(km.c_str(), &end)};
if (end != km.c_str() && *end == '\0' && std::isfinite(value))
entry.km = value * 1000.0;
}
g_records.push_back(std::move(entry));
}
}

View File

@@ -260,6 +260,7 @@ void terrain_streamer::worker()
}
case job::kind::mesh:
{
done.cells = task.data->cells;
auto const &neighbours{task.neighbours};
heightmap::build_mesh(*task.data, task.stride, task.origin,
[&neighbours](double const X, double const Z, double &Height) {
@@ -332,7 +333,7 @@ void terrain_streamer::process(result &Result)
break;
auto &chunk{*lookup->second};
chunk.m_building = false;
if (Result.version < chunk.m_builtversion)
if (Result.version < chunk.m_builtversion || Result.cells != chunk.cells())
break;
upload(chunk, Result.mesh, Result.stride);
chunk.m_builtversion = Result.version;
@@ -386,7 +387,9 @@ void terrain_streamer::upload(editor_terrain &Chunk, heightmap::mesh &Mesh, int
gfx::userdata_array nouserdata;
// the mesh goes to a geometry slot made for its grid; a slot of the same kind is refilled in place
auto fits = [&](geometry_slot const &Slot) { return Slot.cells == cells && Slot.stride == Stride; };
auto fits = [&](geometry_slot const &Slot) {
return Slot.cells == cells && Slot.stride == Stride && GfxRenderer->Indices(Slot.geometry).size() == Mesh.indices.size() && GfxRenderer->Vertices(Slot.geometry).size() == Mesh.vertices.size();
};
if (Chunk.m_slot < 0 || false == fits(m_slots[Chunk.m_slot]))
{
if (Chunk.m_slot >= 0)

View File

@@ -209,6 +209,7 @@ class terrain_streamer
std::shared_ptr<heightmap::chunk_data> data;
heightmap::mesh mesh;
int stride{1};
int cells{0};
std::uint64_t version{0};
std::string error;
};

View File

@@ -472,8 +472,11 @@ void editor_track::commit(std::vector<TTrack *> const &Tracks)
for (auto *track : Tracks)
track->mark_dirty();
for (auto *track : affected)
for (auto *vehicle : track->Dynamics)
{
auto const vehicles{track->Dynamics};
for (auto *vehicle : vehicles)
vehicle->Move(0.000001);
}
if (s_observer != nullptr)
s_observer->tracks_committed(Tracks);
}