From 4c42600898f87a4d3acc46005e8874061925a493 Mon Sep 17 00:00:00 2001 From: James Turner Date: Sun, 8 Jan 2023 19:33:14 +0000 Subject: [PATCH] Sprintf to snprintf fixes Reduce warning from Clang about use of sprintf. --- 3rdparty/hidapi/mac/hid.c | 8 ++-- src/Airports/runways.cxx | 5 --- src/Cockpit/panel.cxx | 7 ++-- src/FDM/ExternalNet/ExternalNet.cxx | 16 ++++---- src/FDM/ExternalPipe/ExternalPipe.cxx | 54 +++++++++++++-------------- src/FDM/YASim/FGFDM.cpp | 12 +++--- src/FDM/YASim/YASim.cxx | 12 +++--- src/Instrumentation/navradio.cxx | 4 +- src/Main/fg_props.cxx | 6 +-- src/Main/locale.cxx | 2 +- src/Network/rul.cxx | 6 +-- src/Scripting/NasalSys.cxx | 2 +- 12 files changed, 64 insertions(+), 70 deletions(-) diff --git a/3rdparty/hidapi/mac/hid.c b/3rdparty/hidapi/mac/hid.c index 28f24ca81..19e9d4107 100644 --- a/3rdparty/hidapi/mac/hid.c +++ b/3rdparty/hidapi/mac/hid.c @@ -720,11 +720,11 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) /* Create the Run Loop Mode for this device. printing the reference seems to work. */ - sprintf(str, "HIDAPI_%p", dev->device_handle); - dev->run_loop_mode = - CFStringCreateWithCString(NULL, str, kCFStringEncodingASCII); + snprintf(str, 32, "HIDAPI_%p", dev->device_handle); + dev->run_loop_mode = + CFStringCreateWithCString(NULL, str, kCFStringEncodingASCII); - /* Attach the device to a Run Loop */ + /* Attach the device to a Run Loop */ IOHIDDeviceRegisterInputReportCallback( dev->device_handle, dev->input_report_buf, dev->max_input_report_len, &hid_report_callback, dev); diff --git a/src/Airports/runways.cxx b/src/Airports/runways.cxx index d2b25f193..da7a59821 100644 --- a/src/Airports/runways.cxx +++ b/src/Airports/runways.cxx @@ -21,14 +21,9 @@ // $Id$ -#ifdef HAVE_CONFIG_H #include -#endif #include -#include // sprintf() -#include // atoi() - #include #include diff --git a/src/Cockpit/panel.cxx b/src/Cockpit/panel.cxx index 072c0ddbf..a2940841c 100644 --- a/src/Cockpit/panel.cxx +++ b/src/Cockpit/panel.cxx @@ -1198,19 +1198,20 @@ FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node, const char * FGTextLayer::Chunk::getValue () const { + const int bufSize = 1024; if (test()) { _buf[0] = '\0'; switch (_type) { case TEXT: - sprintf(_buf, _fmt.c_str(), _text.c_str()); + snprintf(_buf, bufSize, _fmt.c_str(), _text.c_str()); return _buf; case TEXT_VALUE: - sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str()); + snprintf(_buf, bufSize, _fmt.c_str(), _node->getStringValue().c_str()); break; case DOUBLE_VALUE: double d = _offs + _node->getFloatValue() * _mult; if (_trunc) d = (d < 0) ? -floor(-d) : floor(d); - sprintf(_buf, _fmt.c_str(), d); + snprintf(_buf, bufSize, _fmt.c_str(), d); break; } return _buf; diff --git a/src/FDM/ExternalNet/ExternalNet.cxx b/src/FDM/ExternalNet/ExternalNet.cxx index 84667822f..98aac573f 100644 --- a/src/FDM/ExternalNet/ExternalNet.cxx +++ b/src/FDM/ExternalNet/ExternalNet.cxx @@ -166,32 +166,32 @@ void FGExternalNet::init() char cmd[256]; HTTPClient* http; - sprintf(cmd, "/longitude-deg?value=%.8f", lon); + snprintf(cmd, 256, "/longitude-deg?value=%.8f", lon); http = new HTTPClient(fdm_host.c_str(), cmd_port, cmd); while (!http->isDone(1000000)) http->poll(0); delete http; - sprintf(cmd, "/latitude-deg?value=%.8f", lat); + snprintf(cmd, 256, "/latitude-deg?value=%.8f", lat); http = new HTTPClient(fdm_host.c_str(), cmd_port, cmd); while (!http->isDone(1000000)) http->poll(0); delete http; - sprintf(cmd, "/altitude-ft?value=%.8f", alt); + snprintf(cmd, 256, "/altitude-ft?value=%.8f", alt); http = new HTTPClient(fdm_host.c_str(), cmd_port, cmd); while (!http->isDone(1000000)) http->poll(0); delete http; - sprintf(cmd, "/ground-m?value=%.8f", ground); + snprintf(cmd, 256, "/ground-m?value=%.8f", ground); http = new HTTPClient(fdm_host.c_str(), cmd_port, cmd); while (!http->isDone(1000000)) http->poll(0); delete http; - sprintf(cmd, "/speed-kts?value=%.8f", speed); + snprintf(cmd, 256, "/speed-kts?value=%.8f", speed); http = new HTTPClient(fdm_host.c_str(), cmd_port, cmd); while (!http->isDone(1000000)) http->poll(0); delete http; - sprintf(cmd, "/heading-deg?value=%.8f", heading); + snprintf(cmd, 256, "/heading-deg?value=%.8f", heading); http = new HTTPClient(fdm_host.c_str(), cmd_port, cmd); while (!http->isDone(1000000)) http->poll(0); delete http; @@ -199,9 +199,9 @@ void FGExternalNet::init() SG_LOG(SG_IO, SG_INFO, "before sending reset command."); if (fgGetBool("/sim/presets/onground")) { - sprintf(cmd, "/reset?value=ground"); + snprintf(cmd, 256, "/reset?value=ground"); } else { - sprintf(cmd, "/reset?value=air"); + snprintf(cmd, 256, "/reset?value=air"); } http = new HTTPClient(fdm_host.c_str(), cmd_port, cmd); while (!http->isDone(1000000)) http->poll(0); diff --git a/src/FDM/ExternalPipe/ExternalPipe.cxx b/src/FDM/ExternalPipe/ExternalPipe.cxx index d73d0a420..7ce8f0865 100644 --- a/src/FDM/ExternalPipe/ExternalPipe.cxx +++ b/src/FDM/ExternalPipe/ExternalPipe.cxx @@ -246,32 +246,32 @@ void FGExternalPipe::init_binary() { char cmd[256]; int result; - sprintf( cmd, "longitude-deg=%.8f", lon ); + snprintf(cmd, 256, "longitude-deg=%.8f", lon); result = write_binary( '1', pd1, cmd, strlen(cmd) ); - sprintf( cmd, "latitude-deg=%.8f", lat ); + snprintf(cmd, 256, "latitude-deg=%.8f", lat); result = write_binary( '1', pd1, cmd, strlen(cmd) ); - sprintf( cmd, "altitude-ft=%.8f", alt ); + snprintf(cmd, 256, "altitude-ft=%.8f", alt); result = write_binary( '1', pd1, cmd, strlen(cmd) ); - sprintf( cmd, "ground-m=%.8f", ground ); + snprintf(cmd, 256, "ground-m=%.8f", ground); result = write_binary( '1', pd1, cmd, strlen(cmd) ); - sprintf( cmd, "speed-kts=%.8f", speed ); + snprintf(cmd, 256, "speed-kts=%.8f", speed); result = write_binary( '1', pd1, cmd, strlen(cmd) ); - sprintf( cmd, "heading-deg=%.8f", heading ); + snprintf(cmd, 256, "heading-deg=%.8f", heading); result = write_binary( '1', pd1, cmd, strlen(cmd) ); if ( weight > 1000.0 ) { - sprintf( cmd, "aircraft-weight-lbs=%.2f", weight ); + snprintf(cmd, 256, "aircraft-weight-lbs=%.2f", weight); result = write_binary( '1', pd1, cmd, strlen(cmd) ); } last_weight = weight; if ( cg_offset > -5.0 || cg_offset < 5.0 ) { - sprintf( cmd, "aircraft-cg-offset-inches=%.2f", cg_offset ); + snprintf(cmd, 256, "aircraft-cg-offset-inches=%.2f", cg_offset); result = write_binary( '1', pd1, cmd, strlen(cmd) ); } last_cg_offset = cg_offset; @@ -279,9 +279,9 @@ void FGExternalPipe::init_binary() { SG_LOG( SG_IO, SG_ALERT, "before sending reset command." ); if( fgGetBool("/sim/presets/onground") ) { - sprintf( cmd, "reset=ground" ); + snprintf(cmd, 256, "reset=ground"); } else { - sprintf( cmd, "reset=air" ); + snprintf(cmd, 256, "reset=air"); } result = write_binary( '1', pd1, cmd, strlen(cmd) ); @@ -311,32 +311,32 @@ void FGExternalPipe::init_property() { char cmd[256]; int result; - sprintf( cmd, "init longitude-deg=%.8f", lon ); + snprintf(cmd, 256, "init longitude-deg=%.8f", lon); result = write_property( pd1, cmd ); - sprintf( cmd, "init latitude-deg=%.8f", lat ); + snprintf(cmd, 256, "init latitude-deg=%.8f", lat); result = write_property( pd1, cmd ); - sprintf( cmd, "init altitude-ft=%.8f", alt ); + snprintf(cmd, 256, "init altitude-ft=%.8f", alt); result = write_property( pd1, cmd ); - sprintf( cmd, "init ground-m=%.8f", ground ); + snprintf(cmd, 256, "init ground-m=%.8f", ground); result = write_property( pd1, cmd ); - sprintf( cmd, "init speed-kts=%.8f", speed ); + snprintf(cmd, 256, "init speed-kts=%.8f", speed); result = write_property( pd1, cmd ); - sprintf( cmd, "init heading-deg=%.8f", heading ); + snprintf(cmd, 256, "init heading-deg=%.8f", heading); result = write_property( pd1, cmd ); if ( weight > 1000.0 ) { - sprintf( cmd, "init aircraft-weight-lbs=%.2f", weight ); + snprintf(cmd, 256, "init aircraft-weight-lbs=%.2f", weight); result = write_property( pd1, cmd ); } last_weight = weight; if ( cg_offset > -5.0 || cg_offset < 5.0 ) { - sprintf( cmd, "init aircraft-cg-offset-inches=%.2f", cg_offset ); + snprintf(cmd, 256, "init aircraft-cg-offset-inches=%.2f", cg_offset); result = write_property( pd1, cmd ); } last_cg_offset = cg_offset; @@ -344,9 +344,9 @@ void FGExternalPipe::init_property() { SG_LOG( SG_IO, SG_ALERT, "before sending reset command." ); if( fgGetBool("/sim/presets/onground") ) { - sprintf( cmd, "reset ground" ); + snprintf(cmd, 256, "reset ground"); } else { - sprintf( cmd, "reset air" ); + snprintf(cmd, 256, "reset air"); } result = write_property( pd1, cmd ); @@ -391,7 +391,7 @@ void FGExternalPipe::update_binary( double dt ) { static double last_weight = 0.0; if ( fabs( weight - last_weight ) > 0.01 ) { char cmd[256]; - sprintf( cmd, "aircraft-weight-lbs=%.2f", weight ); + snprintf(cmd, 256, "aircraft-weight-lbs=%.2f", weight); result = write_binary( '1', pd1, cmd, strlen(cmd) ); } last_weight = weight; @@ -399,7 +399,7 @@ void FGExternalPipe::update_binary( double dt ) { double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" ); if ( fabs( cg_offset - last_cg_offset ) > 0.01 ) { char cmd[256]; - sprintf( cmd, "aircraft-cg-offset-inches=%.2f", cg_offset ); + snprintf(cmd, 256, "aircraft-cg-offset-inches=%.2f", cg_offset); result = write_binary( '1', pd1, cmd, strlen(cmd) ); } last_cg_offset = cg_offset; @@ -504,27 +504,27 @@ void FGExternalPipe::update_property( double dt ) { double weight = fgGetDouble( "/sim/aircraft-weight-lbs" ); static double last_weight = 0.0; if ( fabs( weight - last_weight ) > 0.01 ) { - sprintf( cmd, "init aircraft-weight-lbs=%.2f", weight ); + snprintf(cmd, 256, "init aircraft-weight-lbs=%.2f", weight); result = write_property( pd1, cmd ); } last_weight = weight; double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" ); if ( fabs( cg_offset - last_cg_offset ) > 0.01 ) { - sprintf( cmd, "init aircraft-cg-offset-inches=%.2f", cg_offset ); + snprintf(cmd, 256, "init aircraft-cg-offset-inches=%.2f", cg_offset); result = write_property( pd1, cmd ); } last_cg_offset = cg_offset; // Send requested property values to fdm for ( unsigned int i = 0; i < nodes.size(); i++ ) { - sprintf( cmd, "set %s %s", property_names[i].c_str(), - nodes[i]->getStringValue().c_str() ); + snprintf(cmd, 256, "set %s %s", property_names[i].c_str(), + nodes[i]->getStringValue().c_str()); // cout << " sending " << cmd << endl; result = write_property( pd1, cmd ); } - sprintf( cmd, "update %d", iterations ); + snprintf(cmd, 256, "update %d", iterations); write_property( pd1, cmd ); fflush( pd1 ); diff --git a/src/FDM/YASim/FGFDM.cpp b/src/FDM/YASim/FGFDM.cpp index 78b28d469..996cb99d5 100644 --- a/src/FDM/YASim/FGFDM.cpp +++ b/src/FDM/YASim/FGFDM.cpp @@ -199,19 +199,19 @@ void FGFDM::init() _tank_level_lbs.clear(); for(int i=0; i<_airplane.numTanks(); i++) { char buf[256]; - sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i); + snprintf(buf, 256, "/consumables/fuel/tank[%d]/level-lbs", i); fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS); _tank_level_lbs.push_back(fgGetNode(buf, true)); double density = _airplane.getFuelDensity(i); - sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i); + snprintf(buf, 256, "/consumables/fuel/tank[%d]/density-ppg", i); fgSetDouble(buf, density * (KG2LBS/CM2GALS)); // set in TankProperties class // sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i); // fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density); - sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i); + snprintf(buf, 256, "/consumables/fuel/tank[%d]/capacity-gal_us", i); fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density); } @@ -415,7 +415,7 @@ void FGFDM::getExternalInput(float dt) if(t->getPropEngine()) { PropEngine* p = t->getPropEngine(); - sprintf(buf, "%s/rpm", er->prefix.c_str()); + snprintf(buf, 256, "%s/rpm", er->prefix.c_str()); p->setOmega(fgGetFloat(buf, 500) * RPM2RAD); } } @@ -923,7 +923,7 @@ void FGFDM::parsePropeller(const XMLAttributes* a) thruster->setGearRatio(attrf(a, "gear-ratio", 1)); char buf[64]; - sprintf(buf, "/engines/engine[%d]", _nextEngine++); + snprintf(buf, 64, "/engines/engine[%d]", _nextEngine++); EngRec* er = new EngRec(); er->eng = thruster; er->prefix = buf; @@ -981,7 +981,7 @@ void FGFDM::parseJet(const XMLAttributes* a) j->setPosition(v); _airplane.addThruster(j, mass, v); char buf[64]; - sprintf(buf, "/engines/engine[%d]", _nextEngine++); + snprintf(buf, 64, "/engines/engine[%d]", _nextEngine++); EngRec* er = new EngRec(); er->eng = j; er->prefix = buf; diff --git a/src/FDM/YASim/YASim.cxx b/src/FDM/YASim/YASim.cxx index c6e0e78f9..101a5b78a 100644 --- a/src/FDM/YASim/YASim.cxx +++ b/src/FDM/YASim/YASim.cxx @@ -80,7 +80,7 @@ void YASim::report() float cg[3]; char buf[256]; a->getModel()->getBody()->getCG(cg); - sprintf(buf, " CG: %.3f, %.3f, %.3f", cg[0], cg[1], cg[2]); + snprintf(buf, 256, " CG: %.3f, %.3f, %.3f", cg[0], cg[1], cg[2]); SG_LOG(SG_FLIGHT, SG_INFO, buf); if(a->getFailureMsg()) { @@ -101,15 +101,15 @@ void YASim::bind() char buf[256]; for(int i=0; i<_fdm->getAirplane()->getModel()->numThrusters(); i++) { - sprintf(buf, "/engines/engine[%d]/fuel-flow-gph", i); + snprintf(buf, 256, "/engines/engine[%d]/fuel-flow-gph", i); fgUntieIfDefined(buf); - sprintf(buf, "/engines/engine[%d]/rpm", i); + snprintf(buf, 256, "/engines/engine[%d]/rpm", i); fgUntieIfDefined(buf); - sprintf(buf, "/engines/engine[%d]/mp-osi", i); + snprintf(buf, 256, "/engines/engine[%d]/mp-osi", i); fgUntieIfDefined(buf); - sprintf(buf, "/engines/engine[%d]/egt-degf", i); + snprintf(buf, 256, "/engines/engine[%d]/egt-degf", i); fgUntieIfDefined(buf); - sprintf(buf, "/engines/engine[%d]/oil-temperature-degf", i); + snprintf(buf, 256, "/engines/engine[%d]/oil-temperature-degf", i); fgUntieIfDefined(buf); } } diff --git a/src/Instrumentation/navradio.cxx b/src/Instrumentation/navradio.cxx index 4f56e390c..17a247993 100644 --- a/src/Instrumentation/navradio.cxx +++ b/src/Instrumentation/navradio.cxx @@ -378,9 +378,9 @@ void FGNavRadio::updateFormattedFrequencies() // Create "formatted" versions of the nav frequencies for // instrument displays. char tmp[16]; - sprintf( tmp, "%.2f", freq_node->getDoubleValue() ); + snprintf(tmp, 16, "%.2f", freq_node->getDoubleValue()); fmt_freq_node->setStringValue(tmp); - sprintf( tmp, "%.2f", alt_freq_node->getDoubleValue() ); + snprintf(tmp, 16, "%.2f", alt_freq_node->getDoubleValue()); fmt_alt_freq_node->setStringValue(tmp); is_loc_freq_node->setBoolValue( IsLocalizerFrequency( freq_node->getDoubleValue() )); } diff --git a/src/Main/fg_props.cxx b/src/Main/fg_props.cxx index c03994958..92c7d9188 100644 --- a/src/Main/fg_props.cxx +++ b/src/Main/fg_props.cxx @@ -259,9 +259,9 @@ getDateString () } struct tm * t = st->getGmt(); - sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d", - t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, - t->tm_hour, t->tm_min, t->tm_sec); + snprintf(buf, 64, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d", + t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, + t->tm_hour, t->tm_min, t->tm_sec); return buf; } diff --git a/src/Main/locale.cxx b/src/Main/locale.cxx index aac1722b0..28b0324eb 100644 --- a/src/Main/locale.cxx +++ b/src/Main/locale.cxx @@ -530,7 +530,7 @@ std::string FGLocale::vlocalizedPrintf(const char* id, const char* resource, va_ std::string format = getLocalizedString(id, resource); int len = ::vsnprintf(nullptr, 0, format.c_str(), args); char* buf = (char*) alloca(len); - ::vsprintf(buf, format.c_str(), args); + ::vsnprintf(buf, len, format.c_str(), args); return std::string(buf); } diff --git a/src/Network/rul.cxx b/src/Network/rul.cxx index 994516306..26936a9b2 100644 --- a/src/Network/rul.cxx +++ b/src/Network/rul.cxx @@ -21,9 +21,7 @@ // // $Id$ -#ifdef HAVE_CONFIG_H -# include -#endif +#include #include // sprintf() @@ -86,7 +84,7 @@ bool FGRUL::gen_message() { int roll = (int)( (roll_deg+180.0) * 255.0 / 360.0) + 1; int pitch = (int)( (pitch_deg+180.0) * 255.0 / 360.0) + 1; - sprintf( buf, "p%c%c\n", roll, pitch); + snprintf(buf, 10, "p%c%c\n", roll, pitch); length = 4; SG_LOG( SG_IO, SG_INFO, "p " << roll << " " << pitch ); diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx index cca57604b..1501aac22 100644 --- a/src/Scripting/NasalSys.cxx +++ b/src/Scripting/NasalSys.cxx @@ -397,7 +397,7 @@ FGNasalScript* FGNasalSys::parseScript(const char* src, const char* name) char buf[256]; if(!name) { - sprintf(buf, "FGNasalScript@%p", (void *)script); + snprintf(buf, 256, "FGNasalScript@%p", (void *)script); name = buf; }