From ecf524063e0bb453841a5ff0ddabb6c25bfd99f8 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Tue, 1 Dec 2026 16:59:35 +0100 Subject: [PATCH] refactor: std::array for the buffers of the physics log line and the seed of the lazy engine, ownP in the if Sonar's remarks on the new code of this branch: the C arrays of append_fixed(), append_int() and lazy_random_engine are std::arrays (S5945), the pressure of the ESt3 relays read once is declared in the if that uses it (S6004), and the thread_local pointer to the lazy engine has the NOSONAR of the one above it. The snprintf of the infinities and NaNs of the physics log stays, with a NOSONAR: std::format writes them otherwise than printf's inf and nan(ind). No change of what the code does. --- physics/oerlikonest.cpp | 10 +++------- utilities/lazyrandom.h | 3 ++- utilities/utilities.cpp | 2 +- vehicle/ai/physicslog.cpp | 17 +++++++++-------- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/physics/oerlikonest.cpp b/physics/oerlikonest.cpp index 4eab8d582..524624a21 100644 --- a/physics/oerlikonest.cpp +++ b/physics/oerlikonest.cpp @@ -67,9 +67,7 @@ void TPrzekladnik::Update(double dt) double BCP{ Next->P() }; double BVP{ BrakeRes->P() }; // the pressure of the relay itself, read once: nothing changes it before the flows are applied (Act()) - double const ownP{ P() }; - - if ( BCP > ownP ) + if ( double const ownP{ P() }; BCP > ownP ) dV = -PFVd(BCP, 0, d2A(10), ownP) * dt; else { if( BCP < ownP ) @@ -122,8 +120,7 @@ void TRapid::Update(double dt) ActMult = 1.0; } - double const ownP{ P() }; - if (BCP * RapidMult > ownP * ActMult) + if (double const ownP{ P() }; BCP * RapidMult > ownP * ActMult) dV = -PFVd(BCP, 0, DL, ownP * ActMult / RapidMult) * dt; else if (BCP * RapidMult < ownP * ActMult) dV = PFVa(BVP, BCP, DN, ownP * ActMult / RapidMult) * dt; @@ -148,8 +145,7 @@ void TPrzekCiagly::Update(double dt) double const BCP{ Next->P() }; double dV; - double const ownP{ P() }; - if ( BCP > ownP * Mult) + if ( double const ownP{ P() }; BCP > ownP * Mult) dV = -PFVd(BCP, 0, d2A(8.0), ownP * Mult) * dt; else if (BCP < ownP * Mult) dV = PFVa(BVP, BCP, d2A(8.0), ownP * Mult) * dt; diff --git a/utilities/lazyrandom.h b/utilities/lazyrandom.h index aed55184b..c55ebedd5 100644 --- a/utilities/lazyrandom.h +++ b/utilities/lazyrandom.h @@ -13,13 +13,14 @@ http://mozilla.org/MPL/2.0/. #pragma once +#include #include #include #include struct lazy_random_engine { - std::uint32_t seed[2]{}; + std::array seed{}; std::optional engine; // NOSONAR: randomness of the simulation, not of anything security-relevant }; diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index 8b7c3868b..d38e4baa0 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -182,7 +182,7 @@ bool ClearFlag(int &Flag, int const Value) namespace { thread_local std::mt19937 *t_random_engine{nullptr}; // NOSONAR: randomness of the simulation, not of anything security-relevant -thread_local lazy_random_engine *t_lazy_random_engine{nullptr}; +thread_local lazy_random_engine *t_lazy_random_engine{nullptr}; // NOSONAR: randomness of the simulation, not of anything security-relevant std::mt19937 &random_engine() // NOSONAR: randomness of the simulation, not of anything security-relevant { diff --git a/vehicle/ai/physicslog.cpp b/vehicle/ai/physicslog.cpp index ffd4118bc..d7aff2512 100644 --- a/vehicle/ai/physicslog.cpp +++ b/vehicle/ai/physicslog.cpp @@ -23,6 +23,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/physicslog.h" #include "vehicle/Train.h" #include "vehicle/brakereleaser.h" +#include #include #include #include @@ -71,25 +72,25 @@ namespace { // stream and the locale. infinities and NaNs (printf's inf, nan(ind)) and numbers too long for the buffer go through // snprintf with the same format void append_fixed( std::string &Line, double const Value ) { - char buffer[ 128 ]; + std::array buffer; if( std::isfinite( Value ) ) { - auto const result { std::to_chars( buffer, buffer + sizeof( buffer ), Value, std::chars_format::fixed, 4 ) }; + auto const result { std::to_chars( buffer.data(), buffer.data() + buffer.size(), Value, std::chars_format::fixed, 4 ) }; if( result.ec == std::errc{} ) { - Line.append( buffer, result.ptr ); + Line.append( buffer.data(), result.ptr ); return; } } - auto const length { std::snprintf( nullptr, 0, "%.4f", Value ) }; + auto const length { std::snprintf( nullptr, 0, "%.4f", Value ) }; // NOSONAR: printf's own text of inf and NaN, which std::format writes otherwise std::string text( static_cast( length ) + 1, '\0' ); - std::snprintf( text.data(), text.size(), "%.4f", Value ); + std::snprintf( text.data(), text.size(), "%.4f", Value ); // NOSONAR: printf's own text of inf and NaN, which std::format writes otherwise text.resize( static_cast( length ) ); Line += text; } void append_int( std::string &Line, int const Value ) { - char buffer[ 16 ]; - auto const result { std::to_chars( buffer, buffer + sizeof( buffer ), Value ) }; - Line.append( buffer, result.ptr ); + std::array buffer; + auto const result { std::to_chars( buffer.data(), buffer.data() + buffer.size(), Value ) }; + Line.append( buffer.data(), result.ptr ); } } // namespace