1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2025-06-14 04:22:28 -04:00

Replace sprintf by snprintf wherever possible

This commit is contained in:
f4exb 2025-04-21 13:31:12 +02:00
parent 2e1411311b
commit 77b2b815b8
9 changed files with 43 additions and 42 deletions

View File

@ -82,7 +82,7 @@ std::string Packing::unpackcall(int x)
if (x <= 1002) if (x <= 1002)
{ {
sprintf(tmp, "CQ %d", x - 3); snprintf(tmp, sizeof(tmp), "CQ %d", x - 3);
return std::string(tmp); return std::string(tmp);
} }
@ -96,7 +96,7 @@ std::string Packing::unpackcall(int x)
int ci3 = x / 27; int ci3 = x / 27;
x %= 27; x %= 27;
int ci4 = x; int ci4 = x;
sprintf(tmp, "CQ %c%c%c%c", c4[ci1], c4[ci2], c4[ci3], c4[ci4]); snprintf(tmp, sizeof(tmp), "CQ %c%c%c%c", c4[ci1], c4[ci2], c4[ci3], c4[ci4]);
return std::string(tmp); return std::string(tmp);
} }
@ -193,9 +193,9 @@ std::string Packing::unpackgrid15(int ng, int ir)
char tmp[16]; char tmp[16];
if (db >= 0) { if (db >= 0) {
sprintf(tmp, "%s+%02d", ir ? "R" : "", db); snprintf(tmp, sizeof(tmp), "%s+%02d", ir ? "R" : "", db);
} else { } else {
sprintf(tmp, "%s-%02d", ir ? "R" : "", 0 - db); snprintf(tmp, sizeof(tmp), "%s-%02d", ir ? "R" : "", 0 - db);
} }
return std::string(tmp); return std::string(tmp);
@ -396,7 +396,7 @@ std::string Packing::unpack_5(int a77[], std::string& call1str, std::string& cal
i += 3; i += 3;
int qsonb = un64(a77, i, 11); int qsonb = un64(a77, i, 11);
char report[16]; char report[16];
sprintf(report, "%d%04d", rst, qsonb); snprintf(report, sizeof(report), "%d%04d", rst, qsonb);
i += 11; i += 11;
// g25 // g25
int ng = un64(a77, i, 25); int ng = un64(a77, i, 25);
@ -467,9 +467,9 @@ std::string Packing::unpack_0_1(int a77[], std::string& call1str, std::string& c
char tmp[32]; char tmp[32];
if (r >= 0) { if (r >= 0) {
sprintf(tmp, "+%02d", r); snprintf(tmp, sizeof(tmp), "+%02d", r);
} else { } else {
sprintf(tmp, "-%02d", -r); snprintf(tmp, sizeof(tmp), "-%02d", -r);
} }
locstr = std::string(tmp); locstr = std::string(tmp);
@ -553,7 +553,7 @@ std::string Packing::unpack_3(int a77[], std::string& call1str, std::string& cal
else else
{ {
char tmp[32]; char tmp[32];
sprintf(tmp, "%04d", serial); snprintf(tmp, sizeof(tmp), "%04d", serial);
serialstr = std::string(tmp); serialstr = std::string(tmp);
} }
@ -571,7 +571,7 @@ std::string Packing::unpack_3(int a77[], std::string& call1str, std::string& cal
} }
{ {
char tmp[16]; char tmp[16];
sprintf(tmp, "%d ", rst); snprintf(tmp, sizeof(tmp), "%d ", rst);
msg += std::string(tmp); msg += std::string(tmp);
} }
@ -634,7 +634,7 @@ std::string Packing::unpack_0_3(int a77[], int n3, std::string& call1str, std::s
{ {
char tmp[16]; char tmp[16];
sprintf(tmp, "%d%c ", n_transmitters + 1, clss + 'A'); snprintf(tmp, sizeof(tmp), "%d%c ", n_transmitters + 1, clss + 'A');
msg += std::string(tmp); msg += std::string(tmp);
} }
@ -658,9 +658,9 @@ std::string Packing::unpack(int a77[], std::string& call1, std::string& call2, s
char tmp[64]; char tmp[64];
if (i3 == 0) { if (i3 == 0) {
sprintf(tmp, "%d.%d", i3, n3); snprintf(tmp, sizeof(tmp), "%d.%d", i3, n3);
} else { } else {
sprintf(tmp, "%d", i3); snprintf(tmp, sizeof(tmp), "%d", i3);
} }
type = std::string(tmp); type = std::string(tmp);
@ -714,7 +714,7 @@ std::string Packing::unpack(int a77[], std::string& call1, std::string& call2, s
} }
call1 = "UNK"; call1 = "UNK";
sprintf(tmp, "UNK i3=%d n3=%d", i3, n3); snprintf(tmp, sizeof(tmp), "UNK i3=%d n3=%d", i3, n3);
return std::string(tmp); return std::string(tmp);
} }

View File

@ -700,8 +700,9 @@ struct s2_frame_receiver : runnable
char *format() { char *format() {
static char buf[256]; static char buf[256];
sprintf( snprintf(
buf, buf,
sizeof(buf),
"%9.2lf %+6.0f ppm %+3.0f ° %f", "%9.2lf %+6.0f ppm %+3.0f ° %f",
(double)((p-(std::complex<T>*)NULL)&262143)+mu, // Arbitrary wrap (double)((p-(std::complex<T>*)NULL)&262143)+mu, // Arbitrary wrap
fw16*1e6/65536, fw16*1e6/65536,
@ -3516,11 +3517,11 @@ struct s2_fecdec_helper : runnable
close(rx[0]); close(rx[0]);
dup2(rx[1], 1); dup2(rx[1], 1);
char max_trials_arg[16]; char max_trials_arg[16];
sprintf(max_trials_arg, "%d", max_trials); snprintf(max_trials_arg, sizeof(max_trial_args), "%d", max_trials);
char batch_size_arg[16]; char batch_size_arg[16];
sprintf(batch_size_arg, "%d", batch_size); snprintf(batch_size_arg, sizeof(batch_size_args), "%d", batch_size);
char mc_arg[16]; char mc_arg[16];
sprintf(mc_arg, "%d", pls->modcod); snprintf(mc_arg, sizeof(mc_arg), "%d", pls->modcod);
const char *sf_arg = pls->sf ? "--shortframes" : nullptr; const char *sf_arg = pls->sf ? "--shortframes" : nullptr;
const char *argv[] = { const char *argv[] = {
command, command,

View File

@ -368,7 +368,7 @@ struct slowmultiscope : runnable
channel *c = &chans[i]; channel *c = &chans[i];
g.setfg(c->spec.rgb[0], c->spec.rgb[1], c->spec.rgb[2]); g.setfg(c->spec.rgb[0], c->spec.rgb[1], c->spec.rgb[2]);
char text[256]; char text[256];
sprintf(text, c->spec.format, c->print_val); snprintf(text, sizeof(text), c->spec.format, c->print_val);
g.transient_text(5, 20 + 16 * i, text); g.transient_text(5, 20 + 16 * i, text);
} }
run_gui(); run_gui();
@ -445,7 +445,7 @@ struct slowmultiscope : runnable
float ct = g.mx * samples_per_pixel / sample_freq; float ct = g.mx * samples_per_pixel / sample_freq;
float tt = total_samples / sample_freq; float tt = total_samples / sample_freq;
char text[256]; char text[256];
sprintf(text, "%.3f / %.3f s", ct, tt); snprintf(text, sizeof(text), "%.3f / %.3f s", ct, tt);
g.setfg(255, 255, 255); g.setfg(255, 255, 255);
g.transient_text(g.w * 3 / 4, 20, text); g.transient_text(g.w * 3 / 4, 20, text);
} }
@ -529,7 +529,7 @@ struct spectrumscope : runnable
{ {
char s[256]; char s[256];
float f = 2.4e6 * (g.mx - g.w / 2) / g.w; float f = 2.4e6 * (g.mx - g.w / 2) / g.w;
sprintf(s, "%f", f); snprintf(s, sizeof(s), "%f", f);
g.text(16, 16, s); g.text(16, 16, s);
} }
g.show(); g.show();
@ -637,7 +637,7 @@ struct rfscope : runnable
char s[256]; char s[256];
float freq = Fc + Fs * (g.mx - g.w / 2) / g.w / hzoom; float freq = Fc + Fs * (g.mx - g.w / 2) / g.w / hzoom;
float val = db0 + (float)((g.h - 1) - g.my) * dbrange / g.h; float val = db0 + (float)((g.h - 1) - g.my) * dbrange / g.h;
sprintf(s, "%f.3 Hz %f.2 dB", freq, val); snprintf(s, sizeof(s), "%f.3 Hz %f.2 dB", freq, val);
g.setfg(255, 255, 255); g.setfg(255, 255, 255);
g.text(16, 16, s); g.text(16, 16, s);
} }
@ -726,7 +726,7 @@ struct genscope : runnable
g.line(pc->spec.r.x - dx, pc->spec.r.y - dy, g.line(pc->spec.r.x - dx, pc->spec.r.y - dy,
pc->spec.r.x + dx, pc->spec.r.y + dy); pc->spec.r.x + dx, pc->spec.r.y + dy);
char txt[16]; char txt[16];
sprintf(txt, "%d", (int)last); snprintf(txt, sizeof(txt), "%d", (int)last);
g.text(pc->spec.r.x + 5, pc->spec.r.y - 2, txt); g.text(pc->spec.r.x + 5, pc->spec.r.y - 2, txt);
} }
struct timeval newtv; struct timeval newtv;

View File

@ -258,8 +258,8 @@ void PacketModGUI::on_insertPosition_clicked()
longFrac = round(longitude); longFrac = round(longitude);
// Insert position with house symbol (-) in to data field // Insert position with house symbol (-) in to data field
sprintf(latBuf, "%02d%02d.%02d%c", latDeg, latMin, latFrac, latNorth ? 'N' : 'S'); snprintf(latBuf, sizeof(latBuf), "%02d%02d.%02d%c", latDeg, latMin, latFrac, latNorth ? 'N' : 'S');
sprintf(longBuf, "%03d%02d.%02d%c", longDeg, longMin, longFrac, longEast ? 'E' : 'W'); snprintf(longBuf, sizeof(longBuf), "%03d%02d.%02d%c", longDeg, longMin, longFrac, longEast ? 'E' : 'W');
QString packet = QString("%1/%2-").arg(latBuf).arg(longBuf); QString packet = QString("%1/%2-").arg(latBuf).arg(longBuf);
ui->packet->insert(packet); ui->packet->insert(packet);
} }

View File

@ -75,7 +75,7 @@ void AMBEEngine::getComList()
// Arbitrarily set the list to the 20 first COM ports // Arbitrarily set the list to the 20 first COM ports
for (int i = 1; i <= 20; i++) for (int i = 1; i <= 20; i++)
{ {
sprintf(comCStr, "COM%d", i); snprintf(comCStr, sizeof(comCStr), "COM%d", i);
m_comList.push_back(std::string(comCStr)); m_comList.push_back(std::string(comCStr));
} }
} }

View File

@ -203,7 +203,7 @@ void RigCtlServerWorker::getCommand()
// Set frequency // Set frequency
double targetFrequency = atof(cmd[0] == 'F' ? &cmd[2] : &cmd[9]); double targetFrequency = atof(cmd[0] == 'F' ? &cmd[2] : &cmd[9]);
setFrequency(targetFrequency, rigCtlRC); setFrequency(targetFrequency, rigCtlRC);
sprintf(response, "RPRT %d\n", rigCtlRC); snprintf(response, sizeof(response), "RPRT %d\n", rigCtlRC);
} }
else if (!strncmp(cmd, "f", 1) || !strncmp(cmd, "get_freq", 8)) else if (!strncmp(cmd, "f", 1) || !strncmp(cmd, "get_freq", 8))
{ {
@ -211,9 +211,9 @@ void RigCtlServerWorker::getCommand()
double frequency; double frequency;
if (getFrequency(frequency, rigCtlRC)) { if (getFrequency(frequency, rigCtlRC)) {
sprintf(response, "%u\n", (unsigned int) frequency); snprintf(response, sizeof(response), "%u\n", (unsigned int) frequency);
} else { } else {
sprintf(response, "RPRT %d\n", rigCtlRC); snprintf(response, sizeof(response), "RPRT %d\n", rigCtlRC);
} }
} }
else if (!strncmp(cmd, "M ?", 3) || !(strncmp(cmd, "set_mode ?", 10))) else if (!strncmp(cmd, "M ?", 3) || !(strncmp(cmd, "set_mode ?", 10)))
@ -263,11 +263,11 @@ void RigCtlServerWorker::getCommand()
if (m_modeMap[i].modem != nullptr) if (m_modeMap[i].modem != nullptr)
{ {
changeModem(targetMode, targetModem, targetBW, rigCtlRC); changeModem(targetMode, targetModem, targetBW, rigCtlRC);
sprintf(response, "RPRT %d\n", rigCtlRC); snprintf(response, sizeof(response), "RPRT %d\n", rigCtlRC);
} }
else else
{ {
sprintf(response, "RPRT %d\n", RIG_EINVAL); snprintf(response, sizeof(response), "RPRT %d\n", RIG_EINVAL);
m_clientConnection->write(response, strlen(response)); m_clientConnection->write(response, strlen(response));
} }
} }
@ -280,27 +280,27 @@ void RigCtlServerWorker::getCommand()
if (getMode(&mode, passband, rigCtlRC)) if (getMode(&mode, passband, rigCtlRC))
{ {
if (passband < 0) { if (passband < 0) {
sprintf(response, "%s\n", mode); snprintf(response, sizeof(response), "%s\n", mode);
} else { } else {
sprintf(response, "%s %u\n", mode, (unsigned int) passband); snprintf(response, sizeof(response), "%s %u\n", mode, (unsigned int) passband);
} }
} }
else else
{ {
sprintf(response, "RPRT %d\n", rigCtlRC); snprintf(response, sizeof(response), "RPRT %d\n", rigCtlRC);
} }
} }
else if (!strncmp(cmd, "set_powerstat 0", 15)) else if (!strncmp(cmd, "set_powerstat 0", 15))
{ {
// Power off radio // Power off radio
setPowerOff(rigCtlRC); setPowerOff(rigCtlRC);
sprintf(response, "RPRT %d\n", rigCtlRC); snprintf(response, sizeof(response), "RPRT %d\n", rigCtlRC);
} }
else if (!strncmp(cmd, "set_powerstat 1", 15)) else if (!strncmp(cmd, "set_powerstat 1", 15))
{ {
// Power on radio // Power on radio
setPowerOn(rigCtlRC); setPowerOn(rigCtlRC);
sprintf(response, "RPRT %d\n", rigCtlRC); snprintf(response, sizeof(response), "RPRT %d\n", rigCtlRC);
} }
else if (!strncmp(cmd, "get_powerstat", 13)) else if (!strncmp(cmd, "get_powerstat", 13))
{ {
@ -309,20 +309,20 @@ void RigCtlServerWorker::getCommand()
if (getPower(power, rigCtlRC)) if (getPower(power, rigCtlRC))
{ {
if (power) { if (power) {
sprintf(response, "1\n"); snprintf(response, sizeof(response), "1\n");
} else { } else {
sprintf(response, "0\n"); snprintf(response, sizeof(response), "0\n");
} }
} }
else else
{ {
sprintf(response, "RPRT %d\n", rigCtlRC); snprintf(response, sizeof(response), "RPRT %d\n", rigCtlRC);
} }
} }
else else
{ {
// Unimplemented command // Unimplemented command
sprintf(response, "RPRT %d\n", RIG_ENIMPL); snprintf(response, sizeof(response), "RPRT %d\n", RIG_ENIMPL);
m_clientConnection->write(response, strlen(response)); m_clientConnection->write(response, strlen(response));
} }
} }

View File

@ -90,7 +90,7 @@ LimeSDROutputGUI::LimeSDROutputGUI(DeviceUISet *deviceUISet, QWidget* parent) :
displaySettings(); displaySettings();
char recFileNameCStr[30]; char recFileNameCStr[30];
sprintf(recFileNameCStr, "test_%d.sdriq", m_deviceUISet->m_deviceAPI->getDeviceUID()); snprintf(recFileNameCStr, sizeof(recFileNameCStr), "test_%d.sdriq", m_deviceUISet->m_deviceAPI->getDeviceUID());
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection); connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection);
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(openDeviceSettingsDialog(const QPoint &))); connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(openDeviceSettingsDialog(const QPoint &)));

View File

@ -82,7 +82,7 @@ USRPOutputGUI::USRPOutputGUI(DeviceUISet *deviceUISet, QWidget* parent) :
displaySettings(); displaySettings();
char recFileNameCStr[30]; char recFileNameCStr[30];
sprintf(recFileNameCStr, "test_%d.sdriq", m_deviceUISet->m_deviceAPI->getDeviceUID()); snprintf(recFileNameCStr, sizeof(recFileNameCStr), "test_%d.sdriq", m_deviceUISet->m_deviceAPI->getDeviceUID());
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection); connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection);
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(openDeviceSettingsDialog(const QPoint &))); connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(openDeviceSettingsDialog(const QPoint &)));

View File

@ -44,7 +44,7 @@ void SerialUtil::getComPorts(std::vector<std::string>& comPorts, const std::stri
for (int i = 0; i<255; i++) // checking ports from COM0 to COM255 for (int i = 0; i<255; i++) // checking ports from COM0 to COM255
{ {
sprintf(portName, "COM%d", i); snprintf(portName, sizeof(portName), "COM%d", i);
test = QueryDosDeviceA((LPCSTR)portName, (LPSTR)lpTargetPath, 5000); test = QueryDosDeviceA((LPCSTR)portName, (LPSTR)lpTargetPath, 5000);