mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-05 16:55:32 +01:00
fix(sonar): the reliability and security issues of the derailment
SonarCloud failed the quality gate of the pull request on the new reliability and security ratings: - cpp:S836 (dereference of an undefined pointer value) in the timing watch of TDynamicObject::Update: the analyser lost the reference held in a std::optional. The watch is now always made, with a null vehicle when it is on the track, and does nothing then; copying it is deleted. Only the timing of the update off the track and its warning use it, the physics is the same. - cpp:S2245 (pseudorandom number generator) on the std::mt19937 of draw_scatter: it is seeded on purpose so that a derailment repeats, and nothing secret is drawn; marked NOSONAR like the seeded generators of the editor self tests. - cpp:S2193 (a for loop counter of floating type) in three loops of tests/unit/test_derail_motion.cpp: they count integer steps now.
This commit is contained in:
@@ -517,7 +517,7 @@ std::vector<car> run_train(std::vector<std::pair<dm::start, dm::scatter>> const
|
||||
constexpr double stiffness{2e6};
|
||||
auto const damping{0.3 * 2.0 * std::sqrt(stiffness * Shape.mass)};
|
||||
constexpr double slack{0.05};
|
||||
for (double time = 0.0; time < 30.0; time += Dt) {
|
||||
for (int step = 0; step * Dt < 30.0; ++step) {
|
||||
std::vector<std::array<dm::end_force, 2>> ends(train.size());
|
||||
for (std::size_t index = 0; index + 1 < train.size(); ++index) {
|
||||
auto &ahead{train[index]};
|
||||
@@ -902,7 +902,7 @@ hit_outcome run_into(double const Speed, double const Angle, double const Across
|
||||
standing.front = glm::dvec3{std::sin(Angle), 0.0, std::cos(Angle)};
|
||||
second.body.begin(standing, coach(), ballast);
|
||||
hit_outcome result;
|
||||
for (double time = 0.0; time < 30.0; time += Dt) {
|
||||
for (int step = 0; step * Dt < 30.0; ++step) {
|
||||
result.most = std::max(result.most, touch(first, second));
|
||||
slide_on(first, Dt);
|
||||
slide_on(second, Dt);
|
||||
@@ -1032,9 +1032,9 @@ TEST_CASE("derail motion: two bodies folded past a right angle stay apart") {
|
||||
second.body.begin(other, coach(), ballast);
|
||||
second.speed = 5.0;
|
||||
auto most{0.0};
|
||||
for (double time = 0.0; time < 20.0; time += 0.01) {
|
||||
for (int step = 0; step < 2000; ++step) { // 20 s in steps of 0.01 s
|
||||
auto const depth{touch(first, second)};
|
||||
if (time > 0.5) {
|
||||
if (step > 50) { // after half a second
|
||||
most = std::max(most, depth);
|
||||
}
|
||||
slide_on(first, 0.01);
|
||||
|
||||
+25
-14
@@ -3698,23 +3698,34 @@ bool TDynamicObject::Update(double dt, double dt1)
|
||||
return true; // Ra: pauza
|
||||
// physics.derailment new: all of the update of a vehicle off the track, timed; a warning once a second at most when it
|
||||
// takes long, to find what makes a derailment slow
|
||||
struct off_track_watch {
|
||||
TDynamicObject &vehicle;
|
||||
double frame;
|
||||
off_track_clock::time_point started { off_track_clock::now() };
|
||||
~off_track_watch() {
|
||||
auto const took { seconds_since( started ) };
|
||||
vehicle.m_offtrackupdatedue -= frame;
|
||||
if( took > off_track_slow && vehicle.m_offtrackupdatedue <= 0.0 ) {
|
||||
vehicle.m_offtrackupdatedue = 1.0;
|
||||
ErrorLog( std::format( "derailment: warning: the update of {} off the track took {:.0f} ms in a frame of {:.3f} s", vehicle.MoverParameters->Name, took * 1000.0, frame ) );
|
||||
// (no vehicle when it is on the track: nothing is timed then)
|
||||
class off_track_watch {
|
||||
public:
|
||||
off_track_watch( TDynamicObject *Vehicle, double const Frame ) :
|
||||
vehicle { Vehicle }, frame { Frame } {
|
||||
if( vehicle != nullptr ) {
|
||||
started = off_track_clock::now();
|
||||
}
|
||||
}
|
||||
off_track_watch( off_track_watch const & ) = delete;
|
||||
off_track_watch &operator=( off_track_watch const & ) = delete;
|
||||
~off_track_watch() {
|
||||
if( vehicle == nullptr ) {
|
||||
return;
|
||||
}
|
||||
auto const took { seconds_since( started ) };
|
||||
vehicle->m_offtrackupdatedue -= frame;
|
||||
if( took > off_track_slow && vehicle->m_offtrackupdatedue <= 0.0 ) {
|
||||
vehicle->m_offtrackupdatedue = 1.0;
|
||||
ErrorLog( std::format( "derailment: warning: the update of {} off the track took {:.0f} ms in a frame of {:.3f} s", vehicle->MoverParameters->Name, took * 1000.0, frame ) );
|
||||
}
|
||||
}
|
||||
private:
|
||||
TDynamicObject *vehicle;
|
||||
double frame;
|
||||
off_track_clock::time_point started {};
|
||||
};
|
||||
std::optional<off_track_watch> watch;
|
||||
if( MoverParameters->OffTrack.active() ) {
|
||||
watch.emplace( *this, dt1 );
|
||||
}
|
||||
off_track_watch const watch { MoverParameters->OffTrack.active() ? this : nullptr, dt1 };
|
||||
if (!MoverParameters->PhysicActivation &&
|
||||
!MechInside) { // to drugie, bo będąc w maszynowym blokuje się fizyka
|
||||
// physics.derailment new: a vehicle off the track stopped along its heading still settles: it slides down a
|
||||
|
||||
@@ -105,7 +105,7 @@ double departure_angle(cause const Cause, double const Speed, double const Accel
|
||||
|
||||
scatter draw_scatter(std::uint32_t const Seed) {
|
||||
// the raw numbers of the engine are the same with every library, the distributions of <random> aren't
|
||||
std::mt19937 engine{Seed};
|
||||
std::mt19937 engine{Seed}; // NOSONAR: seeded on purpose, so that a derailment can be repeated; nothing secret is drawn
|
||||
auto const uniform = [&engine](double const Low, double const High) { return Low + (High - Low) * ((static_cast<double>(engine()) + 0.5) / 4294967296.0); };
|
||||
scatter result;
|
||||
result.departure = uniform(-0.01, 0.01);
|
||||
|
||||
Reference in New Issue
Block a user