Skip to content

Commit 76dffd0

Browse files
authored
Merge pull request #4866 from computezrmle/vboxwrapper_replace_sprintf
[vboxwrapper] Replace sprintf with snprintf
2 parents 0709610 + 35b4d7b commit 76dffd0

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

samples/vboxwrapper/vbox_vboxmanage.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ namespace vboxmanage {
224224
// Tweak the VM's Memory Size
225225
//
226226
vboxlog_msg("Setting Memory Size for VM. (%dMB)", (int)memory_size_mb);
227-
sprintf(buf, "%d", (int)memory_size_mb);
227+
snprintf(buf, sizeof(buf), "%d", (int)memory_size_mb);
228228

229229
command = "modifyvm \"" + vm_name + "\" ";
230230
command += "--memory " + string(buf) + " ";
@@ -259,7 +259,7 @@ namespace vboxmanage {
259259
// Tweak the VM's Graphics Controller Options
260260
//
261261
vboxlog_msg("Setting Graphics Controller Options for VM.");
262-
sprintf(buf, "%d", (int)vram_size_mb);
262+
snprintf(buf, sizeof(buf), "%d", (int)vram_size_mb);
263263

264264
command = "modifyvm \"" + vm_name + "\" ";
265265
command += "--vram " + string(buf) + " ";
@@ -768,7 +768,7 @@ namespace vboxmanage {
768768

769769
// Add new firewall rule
770770
//
771-
sprintf(buf, ",tcp,%s,%d,,%d",
771+
snprintf(buf, sizeof(buf), ",tcp,%s,%d,,%d",
772772
pf.is_remote?"":"127.0.0.1",
773773
pf.host_port, pf.guest_port
774774
);
@@ -790,7 +790,7 @@ namespace vboxmanage {
790790
retval = boinc_get_port(false, rd_host_port);
791791
if (retval) return retval;
792792

793-
sprintf(buf, "%d", rd_host_port);
793+
snprintf(buf, sizeof(buf), "%d", rd_host_port);
794794
command = "modifyvm \"" + vm_name + "\" ";
795795
command += "--vrde on ";
796796
command += "--vrdeextpack default ";
@@ -1533,7 +1533,7 @@ namespace vboxmanage {
15331533
pause();
15341534

15351535
// Create new snapshot
1536-
sprintf(buf, "%d", (int)elapsed_time);
1536+
snprintf(buf, sizeof(buf), "%d", (int)elapsed_time);
15371537
command = "snapshot \"" + vm_name + "\" ";
15381538
command += "take boinc_";
15391539
command += buf;
@@ -2161,7 +2161,7 @@ namespace vboxmanage {
21612161
// the arg to controlvm is percentage
21622162
//
21632163
vboxlog_msg("Setting CPU throttle for VM. (%d%%)", percentage);
2164-
sprintf(buf, "%d", percentage);
2164+
snprintf(buf, sizeof(buf), "%d", percentage);
21652165
command = "controlvm \"" + vm_name + "\" ";
21662166
command += "cpuexecutioncap ";
21672167
command += buf;
@@ -2198,7 +2198,7 @@ namespace vboxmanage {
21982198
retval = vbm_popen(command, output, "network throttle (set default value)");
21992199
if (retval) return retval;
22002200
} else {
2201-
sprintf(buf, "%d", kilobytes);
2201+
snprintf(buf, sizeof(buf), "%d", kilobytes);
22022202
command = "bandwidthctl \"" + vm_name + "\" ";
22032203
command += "set \"" + vm_name + "_net\" ";
22042204
command += "--limit ";
@@ -2211,7 +2211,7 @@ namespace vboxmanage {
22112211

22122212
} else {
22132213

2214-
sprintf(buf, "%d", kilobytes);
2214+
snprintf(buf, sizeof(buf), "%d", kilobytes);
22152215
command = "modifyvm \"" + vm_name + "\" ";
22162216
command += "--nicspeed1 ";
22172217
command += buf;

samples/vboxwrapper/vboxwrapper.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,20 @@ using std::string;
9292

9393
bool shared_file_exists(std::string& filename) {
9494
char path[MAXPATHLEN];
95-
sprintf(path, "shared/%s", filename.c_str());
95+
snprintf(path, sizeof(path), "shared/%s", filename.c_str());
9696
if (filename.size() && boinc_file_exists(path)) return true;
9797
return false;
9898
}
9999

100100
void shared_delete_file(std::string& filename) {
101101
char path[MAXPATHLEN];
102-
sprintf(path, "shared/%s", filename.c_str());
102+
snprintf(path, sizeof(path), "shared/%s", filename.c_str());
103103
boinc_delete_file(path);
104104
}
105105

106106
int shared_stat(std::string& filename, struct stat* stat_file) {
107107
char path[MAXPATHLEN];
108-
sprintf(path, "shared/%s", filename.c_str());
108+
snprintf(path, sizeof(path), "shared/%s", filename.c_str());
109109
return stat(path, stat_file);
110110
}
111111

@@ -114,7 +114,7 @@ bool read_fraction_done(double& frac_done, VBOX_VM& vm) {
114114
char buf[256];
115115
double temp, frac = 0;
116116

117-
sprintf(path, "shared/%s", vm.fraction_done_filename.c_str());
117+
snprintf(path, sizeof(path), "shared/%s", vm.fraction_done_filename.c_str());
118118
FILE* f = fopen(path, "r");
119119
if (!f) return false;
120120

@@ -147,7 +147,7 @@ void read_completion_file_info(unsigned long& exit_code, bool& is_notice, string
147147
exit_code = 0;
148148
message = "";
149149

150-
sprintf(path, "shared/%s", vm.completion_trigger_file.c_str());
150+
snprintf(path, sizeof(path), "shared/%s", vm.completion_trigger_file.c_str());
151151
FILE* f = fopen(path, "r");
152152
if (f) {
153153
if (fgets(buf, 1024, f) != NULL) {
@@ -170,7 +170,7 @@ void read_temporary_exit_file_info(int& temp_delay, bool& is_notice, string& mes
170170
temp_delay = 0;
171171
message = "";
172172

173-
sprintf(path, "shared/%s", vm.temporary_exit_trigger_file.c_str());
173+
snprintf(path, sizeof(path), "shared/%s", vm.temporary_exit_trigger_file.c_str());
174174
FILE* f = fopen(path, "r");
175175
if (f) {
176176
if (fgets(buf, 1024, f) != NULL) {
@@ -249,16 +249,16 @@ void set_floppy_image(APP_INIT_DATA& aid, VBOX_VM& vm) {
249249
scratch = "BOINC_USERNAME=" + string(aid.user_name) + "\n";
250250
scratch += "BOINC_AUTHENTICATOR=" + string(aid.authenticator) + "\n";
251251

252-
sprintf(buf, "%d", aid.userid);
252+
snprintf(buf, sizeof(buf), "%d", aid.userid);
253253
scratch += "BOINC_USERID=" + string(buf) + "\n";
254254

255-
sprintf(buf, "%d", aid.hostid);
255+
snprintf(buf, sizeof(buf), "%d", aid.hostid);
256256
scratch += "BOINC_HOSTID=" + string(buf) + "\n";
257257

258-
sprintf(buf, "%.17g", aid.user_total_credit);
258+
snprintf(buf, sizeof(buf), "%.17g", aid.user_total_credit);
259259
scratch += "BOINC_USER_TOTAL_CREDIT=" + string(buf) + "\n";
260260

261-
sprintf(buf, "%.17g", aid.host_total_credit);
261+
snprintf(buf, sizeof(buf), "%.17g", aid.host_total_credit);
262262
scratch += "BOINC_HOST_TOTAL_CREDIT=" + string(buf) + "\n";
263263
}
264264
vm.write_floppy(scratch);
@@ -270,7 +270,7 @@ void set_floppy_image(APP_INIT_DATA& aid, VBOX_VM& vm) {
270270
void report_web_graphics_url(VBOX_VM& vm) {
271271
char buf[256];
272272
if (vm.pf_host_port && !boinc_file_exists("graphics_app")) {
273-
sprintf(buf, "http://localhost:%d", vm.pf_host_port);
273+
snprintf(buf, sizeof(buf), "http://localhost:%d", vm.pf_host_port);
274274
vboxlog_msg("Detected: Web Application Enabled (%s)", buf);
275275
boinc_web_graphics_url(buf);
276276
}
@@ -281,7 +281,7 @@ void report_web_graphics_url(VBOX_VM& vm) {
281281
void report_remote_desktop_info(VBOX_VM& vm) {
282282
char buf[256];
283283
if (vm.rd_host_port) {
284-
sprintf(buf, "localhost:%d", vm.rd_host_port);
284+
snprintf(buf, sizeof(buf), "localhost:%d", vm.rd_host_port);
285285
vboxlog_msg("Detected: Remote Desktop Enabled (%s)", buf);
286286
boinc_remote_desktop_addr(buf);
287287
}
@@ -295,7 +295,7 @@ void check_trickle_triggers(VBOX_VM& vm) {
295295
std::string text;
296296
for (unsigned int i=0; i<vm.trickle_trigger_files.size(); i++) {
297297
strcpy(filename, vm.trickle_trigger_files[i].c_str());
298-
sprintf(path, "shared/%s", filename);
298+
snprintf(path, sizeof(path), "shared/%s", filename);
299299
if (!boinc_file_exists(path)) continue;
300300
vboxlog_msg("Reporting a trickle. (%s)", filename);
301301
retval = read_file_string(path, text);
@@ -322,7 +322,7 @@ void check_intermediate_uploads(VBOX_VM& vm) {
322322
char filename[256], path[MAXPATHLEN];
323323
for (unsigned int i=0; i<vm.intermediate_upload_files.size(); i++) {
324324
strcpy(filename, vm.intermediate_upload_files[i].file.c_str());
325-
sprintf(path, "shared/%s", filename);
325+
snprintf(path, sizeof(path), "shared/%s", filename);
326326
if (!boinc_file_exists(path)) continue;
327327
if (!vm.intermediate_upload_files[i].reported && !vm.intermediate_upload_files[i].ignore) {
328328
vboxlog_msg("Reporting an intermediate file. (%s)", vm.intermediate_upload_files[i].file.c_str());
@@ -354,7 +354,7 @@ void check_trickle_period(double& elapsed_time, double& trickle_period) {
354354
}
355355
last_trickle_report_time = elapsed_time;
356356
vboxlog_msg("Status Report: Trickle-Up Event.");
357-
sprintf(buf,
357+
snprintf(buf, sizeof(buf),
358358
"<cpu_time>%f</cpu_time>", last_trickle_report_time
359359
);
360360
int retval = boinc_send_trickle_up(
@@ -629,7 +629,7 @@ int main(int argc, char** argv) {
629629
pVM->vm_master_name += "standalone";
630630
pVM->vm_master_description = "standalone";
631631
if (pVM->enable_floppyio) {
632-
sprintf(buf, "%s.%s",
632+
snprintf(buf, sizeof(buf), "%s.%s",
633633
FLOPPY_IMAGE_FILENAME, FLOPPY_IMAGE_FILENAME_EXTENSION
634634
);
635635
pVM->floppy_image_filename = buf;
@@ -638,13 +638,13 @@ int main(int argc, char** argv) {
638638
pVM->vm_master_name += md5_string(std::string(aid.result_name)).substr(0, 16);
639639
pVM->vm_master_description = aid.result_name;
640640
if (vm_image) {
641-
sprintf(buf, "%s_%d.%s",
641+
snprintf(buf, sizeof(buf), "%s_%d.%s",
642642
IMAGE_FILENAME, vm_image, IMAGE_FILENAME_EXTENSION
643643
);
644644
pVM->image_filename = buf;
645645
}
646646
if (pVM->enable_floppyio) {
647-
sprintf(buf, "%s_%d.%s",
647+
snprintf(buf, sizeof(buf), "%s_%d.%s",
648648
FLOPPY_IMAGE_FILENAME, aid.slot,
649649
FLOPPY_IMAGE_FILENAME_EXTENSION
650650
);
@@ -666,9 +666,9 @@ int main(int argc, char** argv) {
666666
ncpus = 32.0;
667667
}
668668
if (ncpus) {
669-
sprintf(buf, "%d", (int)ceil(ncpus));
669+
snprintf(buf, sizeof(buf), "%d", (int)ceil(ncpus));
670670
} else {
671-
sprintf(buf, "%d", (int)ceil(aid.ncpus));
671+
snprintf(buf, sizeof(buf), "%d", (int)ceil(aid.ncpus));
672672
}
673673
pVM->vm_cpu_count = buf;
674674
} else {
@@ -1065,7 +1065,7 @@ int main(int argc, char** argv) {
10651065
if ((unsigned)retval == VBOX_E_INVALID_OBJECT_STATE) {
10661066
vboxlog_msg("ERROR: VM task failed to pause, rescheduling task for a later time.");
10671067
pVM->poweroff();
1068-
sprintf(buf,
1068+
snprintf(buf, sizeof(buf),
10691069
"VM suspend failed. Will exit and restart in %d sec.",
10701070
RESTART_DELAY
10711071
);
@@ -1078,7 +1078,7 @@ int main(int argc, char** argv) {
10781078
if ((unsigned)retval == VBOX_E_INVALID_OBJECT_STATE) {
10791079
vboxlog_msg("ERROR: VM task failed to resume, rescheduling task for a later time.");
10801080
pVM->poweroff();
1081-
sprintf(buf,
1081+
snprintf(buf, sizeof(buf),
10821082
"VM resume failed. Will exit and restart in %d sec.",
10831083
RESTART_DELAY
10841084
);
@@ -1157,7 +1157,7 @@ int main(int argc, char** argv) {
11571157
//
11581158
vboxlog_msg("ERROR: Checkpoint maintenance failed, rescheduling task for a later time. (%d)", retval);
11591159
pVM->poweroff();
1160-
sprintf(buf,
1160+
snprintf(buf, sizeof(buf),
11611161
"VM snapshot failed. Will exit and restart in %d sec.",
11621162
RESTART_DELAY
11631163
);

0 commit comments

Comments
 (0)