1
0
Fork 0

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.
This commit is contained in:
Julian Smith 2022-01-06 22:11:59 +00:00
parent 76dcb745e1
commit cfe5ed0ef4

View file

@ -656,7 +656,7 @@ FGAIAircraft * TrafficTests::flyAI(SGSharedPtr<FGAIAircraft> aiAircraft, std::st
char fname [160]; char fname [160];
time_t t = time(0); // get time now 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); SGPath p = SGPath::desktop() / (testname + fname);
sg_ofstream csvFile; sg_ofstream csvFile;
csvFile.open(p); csvFile.open(p);
@ -697,7 +697,7 @@ FGAIAircraft * TrafficTests::flyAI(SGSharedPtr<FGAIAircraft> aiAircraft, std::st
if (aiAircraft->GetFlightPlan()->getLeg() != lastLeg ) { if (aiAircraft->GetFlightPlan()->getLeg() != lastLeg ) {
// The current WP is really in our new leg // The current WP is really in our new leg
if (sglog().get_log_priority() <= SG_DEBUG) { 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); FGTestApi::writeGeodsToKML(buffer, geods);
} }
if (aiAircraft->GetFlightPlan()->getLeg() < lastLeg) { if (aiAircraft->GetFlightPlan()->getLeg() < lastLeg) {
@ -733,7 +733,7 @@ FGAIAircraft * TrafficTests::flyAI(SGSharedPtr<FGAIAircraft> aiAircraft, std::st
FGTestApi::adjustSimulationWorldTime(++this->currentWorldTime); FGTestApi::adjustSimulationWorldTime(++this->currentWorldTime);
} }
lastLeg = aiAircraft->GetFlightPlan()->getLeg(); 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) { if (sglog().get_log_priority() <= SG_DEBUG) {
FGTestApi::writeGeodsToKML(buffer, geods); FGTestApi::writeGeodsToKML(buffer, geods);
} }