From cfe5ed0ef4d069597de1ceb27c240844345a10f5 Mon Sep 17 00:00:00 2001 From: Julian Smith Date: Thu, 6 Jan 2022 22:11:59 +0000 Subject: [PATCH] test_suite/unit_tests/AI/test_traffic.cxx: minor improvements to sprintf code. Use snprintf() instead of sprintf() to avoid potential memory corruption. Fixed sprintf time_t call - cast to long long. --- test_suite/unit_tests/AI/test_traffic.cxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test_suite/unit_tests/AI/test_traffic.cxx b/test_suite/unit_tests/AI/test_traffic.cxx index ebce1dce5..a37180ab1 100644 --- a/test_suite/unit_tests/AI/test_traffic.cxx +++ b/test_suite/unit_tests/AI/test_traffic.cxx @@ -656,7 +656,7 @@ FGAIAircraft * TrafficTests::flyAI(SGSharedPtr aiAircraft, std::st char fname [160]; time_t t = time(0); // get time now - sprintf (fname, "%ld.csv", t); + snprintf (fname, sizeof(fname), "%lld.csv", (long long) t); SGPath p = SGPath::desktop() / (testname + fname); sg_ofstream csvFile; csvFile.open(p); @@ -697,7 +697,7 @@ FGAIAircraft * TrafficTests::flyAI(SGSharedPtr aiAircraft, std::st if (aiAircraft->GetFlightPlan()->getLeg() != lastLeg ) { // The current WP is really in our new leg if (sglog().get_log_priority() <= SG_DEBUG) { - sprintf(buffer, "AI Leg %d Callsign %s Iteration %d", lastLeg, aiAircraft->getCallSign().c_str(), iteration); + snprintf(buffer, sizeof(buffer), "AI Leg %d Callsign %s Iteration %d", lastLeg, aiAircraft->getCallSign().c_str(), iteration); FGTestApi::writeGeodsToKML(buffer, geods); } if (aiAircraft->GetFlightPlan()->getLeg() < lastLeg) { @@ -733,7 +733,7 @@ FGAIAircraft * TrafficTests::flyAI(SGSharedPtr aiAircraft, std::st FGTestApi::adjustSimulationWorldTime(++this->currentWorldTime); } lastLeg = aiAircraft->GetFlightPlan()->getLeg(); - sprintf(buffer, "AI Leg %d Callsign %s Iteration %d", lastLeg, aiAircraft->getCallSign().c_str(), iteration); + snprintf(buffer, sizeof(buffer), "AI Leg %d Callsign %s Iteration %d", lastLeg, aiAircraft->getCallSign().c_str(), iteration); if (sglog().get_log_priority() <= SG_DEBUG) { FGTestApi::writeGeodsToKML(buffer, geods); }