1
0
Fork 0

Sprintf to snprintf fixes

Reduce warning from Clang about use of sprintf.
This commit is contained in:
James Turner 2023-01-08 19:33:14 +00:00
parent 0fbe3547fb
commit 4c42600898
12 changed files with 64 additions and 70 deletions

View file

@ -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);

View file

@ -21,14 +21,9 @@
// $Id$
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cassert>
#include <cstdio> // sprintf()
#include <cstdlib> // atoi()
#include <simgear/compiler.h>
#include <simgear/props/props.hxx>

View file

@ -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;

View file

@ -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);

View file

@ -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 );

View file

@ -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;

View file

@ -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);
}
}

View file

@ -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() ));
}

View file

@ -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;
}

View file

@ -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);
}

View file

@ -21,9 +21,7 @@
//
// $Id$
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <config.h>
#include <stdio.h> // 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 );

View file

@ -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;
}