16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-17 22:39:17 +02:00

fix: guard Random/LocalRandom against inverted min>max bounds

std::uniform_real/int_distribution has UB when min>max. The old "fancy Random"
used interpolate(a,b,t) which tolerated a>b; the migration to
std::uniform_real_distribution did not, and only one inverted call site was
fixed by hand. Swap the bounds inside Random()/Random(int)/LocalRandom() so
every call site is safe, and reorder the two inverted literal calls in
Driver.cpp (fActionTime buzzer / departure delay) for readability.
This commit is contained in:
maj00r
2026-07-02 00:16:04 +02:00
parent 03bfbb1345
commit 5fce417a51
2 changed files with 5 additions and 2 deletions

View File

@@ -125,12 +125,14 @@ bool ClearFlag(int &Flag, int const Value)
double Random(double min, double max) double Random(double min, double max)
{ {
if (max < min) { std::swap(min, max); } // std::uniform_real_distribution requires min <= max (inverted bounds are UB)
std::uniform_real_distribution<double> dist(min, max); std::uniform_real_distribution<double> dist(min, max);
return dist(Global.random_engine); return dist(Global.random_engine);
} }
int Random(int min, int max) int Random(int min, int max)
{ {
if (max < min) { std::swap(min, max); } // std::uniform_int_distribution requires min <= max (inverted bounds are UB)
std::uniform_int_distribution<int> dist(min, max); std::uniform_int_distribution<int> dist(min, max);
return dist(Global.random_engine); return dist(Global.random_engine);
} }
@@ -163,6 +165,7 @@ std::string generate_uuid_v4()
double LocalRandom(double a, double b) double LocalRandom(double a, double b)
{ {
if (b < a) { std::swap(a, b); } // std::uniform_real_distribution requires a <= b (inverted bounds are UB)
std::uniform_real_distribution<double> dist(a, b); std::uniform_real_distribution<double> dist(a, b);
return dist(Global.local_random_engine); return dist(Global.local_random_engine);
} }

View File

@@ -4293,12 +4293,12 @@ void TController::Doors( bool const Open, int const Side ) {
{ {
// Not warned yet, not warning yet - start the warning signal. // Not warned yet, not warning yet - start the warning signal.
cue_action( driver_hint::departuresignalon ); // załącenie bzyczka cue_action( driver_hint::departuresignalon ); // załącenie bzyczka
fActionTime = Random( -0.3, -0.8 ); // 0.3-0.8 second buzzer fActionTime = Random( -0.8, -0.3 ); // 0.3-0.8 second buzzer
return; return;
} }
// Not warned yet, warning now - stop the warning signal and mark it as done. // Not warned yet, warning now - stop the warning signal and mark it as done.
cue_action( driver_hint::departuresignaloff ); cue_action( driver_hint::departuresignaloff );
fActionTime = Random( 0.0, -0.2 ); // Wait just a bit more before departing fActionTime = Random( -0.2, 0.0 ); // Wait just a bit more before departing
iDrivigFlags |= moveDepartureWarned; iDrivigFlags |= moveDepartureWarned;
return; return;
} }