Skip to content

Commit 0709610

Browse files
authored
Merge pull request #4864 from computezrmle/replace_sprintf
[client] Replace sprintf with snprintf
2 parents 0f470cf + 821ef46 commit 0709610

33 files changed

+194
-194
lines changed

api/boinc_api.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ static int setup_shared_mem() {
306306
app_client_shm = new APP_CLIENT_SHM;
307307

308308
#ifdef _WIN32
309-
sprintf(buf, "%s%s", SHM_PREFIX, aid.shmem_seg_name);
309+
snprintf(buf, sizeof(buf), "%s%s", SHM_PREFIX, aid.shmem_seg_name);
310310
hSharedMem = attach_shmem(buf, (void**)&app_client_shm->shm);
311311
if (hSharedMem == NULL) {
312312
delete app_client_shm;
@@ -412,7 +412,7 @@ static bool update_app_progress(double cpu_t, double cp_cpu_t) {
412412

413413
if (standalone) return true;
414414

415-
sprintf(msg_buf,
415+
snprintf(msg_buf, sizeof(msg_buf),
416416
"<current_cpu_time>%e</current_cpu_time>\n"
417417
"<checkpoint_cpu_time>%e</checkpoint_cpu_time>\n",
418418
cpu_t, cp_cpu_t
@@ -423,15 +423,15 @@ static bool update_app_progress(double cpu_t, double cp_cpu_t) {
423423
if (fraction_done >= 0) {
424424
double range = aid.fraction_done_end - aid.fraction_done_start;
425425
double fdone = aid.fraction_done_start + fraction_done*range;
426-
sprintf(buf, "<fraction_done>%e</fraction_done>\n", fdone);
426+
snprintf(buf, sizeof(buf), "<fraction_done>%e</fraction_done>\n", fdone);
427427
strlcat(msg_buf, buf, sizeof(msg_buf));
428428
}
429429
if (bytes_sent) {
430-
sprintf(buf, "<bytes_sent>%f</bytes_sent>\n", bytes_sent);
430+
snprintf(buf, sizeof(buf), "<bytes_sent>%f</bytes_sent>\n", bytes_sent);
431431
strlcat(msg_buf, buf, sizeof(msg_buf));
432432
}
433433
if (bytes_received) {
434-
sprintf(buf, "<bytes_received>%f</bytes_received>\n", bytes_received);
434+
snprintf(buf, sizeof(buf), "<bytes_received>%f</bytes_received>\n", bytes_received);
435435
strlcat(msg_buf, buf, sizeof(msg_buf));
436436
}
437437
#ifdef MSGS_FROM_FILE
@@ -962,7 +962,7 @@ int boinc_report_app_status_aux(
962962
char msg_buf[MSG_CHANNEL_SIZE], buf[1024];
963963
if (standalone) return 0;
964964

965-
sprintf(msg_buf,
965+
snprintf(msg_buf, sizeof(msg_buf),
966966
"<current_cpu_time>%e</current_cpu_time>\n"
967967
"<checkpoint_cpu_time>%e</checkpoint_cpu_time>\n"
968968
"<fraction_done>%e</fraction_done>\n",
@@ -971,15 +971,15 @@ int boinc_report_app_status_aux(
971971
_fraction_done
972972
);
973973
if (other_pid) {
974-
sprintf(buf, "<other_pid>%d</other_pid>\n", other_pid);
974+
snprintf(buf, sizeof(buf), "<other_pid>%d</other_pid>\n", other_pid);
975975
safe_strcat(msg_buf, buf);
976976
}
977977
if (_bytes_sent) {
978-
sprintf(buf, "<bytes_sent>%f</bytes_sent>\n", _bytes_sent);
978+
snprintf(buf, sizeof(buf), "<bytes_sent>%f</bytes_sent>\n", _bytes_sent);
979979
safe_strcat(msg_buf, buf);
980980
}
981981
if (_bytes_received) {
982-
sprintf(buf, "<bytes_received>%f</bytes_received>\n", _bytes_received);
982+
snprintf(buf, sizeof(buf), "<bytes_received>%f</bytes_received>\n", _bytes_received);
983983
safe_strcat(msg_buf, buf);
984984
}
985985
#ifdef MSGS_FROM_FILE
@@ -1338,15 +1338,15 @@ static void timer_handler() {
13381338
// send graphics-related messages
13391339
//
13401340
if (send_web_graphics_url && !app_client_shm->shm->graphics_reply.has_msg()) {
1341-
sprintf(buf,
1341+
snprintf(buf, sizeof(buf),
13421342
"<web_graphics_url>%s</web_graphics_url>",
13431343
web_graphics_url
13441344
);
13451345
app_client_shm->shm->graphics_reply.send_msg(buf);
13461346
send_web_graphics_url = false;
13471347
}
13481348
if (send_remote_desktop_addr && !app_client_shm->shm->graphics_reply.has_msg()) {
1349-
sprintf(buf,
1349+
snprintf(buf, sizeof(buf),
13501350
"<remote_desktop_addr>%s</remote_desktop_addr>",
13511351
remote_desktop_addr
13521352
);
@@ -1625,7 +1625,7 @@ int boinc_upload_file(std::string& name) {
16251625

16261626
retval = boinc_resolve_filename_s(name.c_str(), pname);
16271627
if (retval) return retval;
1628-
sprintf(buf, "%s%s", UPLOAD_FILE_REQ_PREFIX, name.c_str());
1628+
snprintf(buf, sizeof(buf), "%s%s", UPLOAD_FILE_REQ_PREFIX, name.c_str());
16291629
FILE* f = boinc_fopen(buf, "w");
16301630
if (!f) return ERR_FOPEN;
16311631
have_new_upload_file = true;

api/graphics_lib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int boinc_init_options_graphics_lib(
9898
// if it's not a symlink, put "./" in front of it
9999
//
100100
if (!strcmp(graphics_lib, resolved_name)) {
101-
sprintf(resolved_name, "./%s", graphics_lib);
101+
snprintf(resolved_name, sizeof(resolved_name), "./%s", graphics_lib);
102102
}
103103

104104
// get handle for shared library.

api/mac_icon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void getPathToThisApp(char* pathBuf, size_t bufSize) {
172172
// (or the soft-link to it.) So all we need for the path to this
173173
// application is the file name. We use the -c option so ps strips off
174174
// any command-line arguments for us.
175-
sprintf(buf, "ps -wcp %d -o command=", myPID);
175+
snprintf(buf, sizeof(buf), "ps -wcp %d -o command=", myPID);
176176
f = popen(buf, "r");
177177
if (!f)
178178
return;

api/ttfont.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void ttf_load_fonts(
9898
char vpath[MAXPATHLEN];
9999
g_iFont = -1;
100100
for (unsigned int i=0 ; i < NUM_FONT; i++){
101-
sprintf(vpath, "%s/%s", dir, g_cstrFont[i]);
101+
snprintf(vpath, sizeof(vpath), "%s/%s", dir, g_cstrFont[i]);
102102
if (boinc_file_exists(vpath)) {
103103
//g_font[i] = new FTBitmapFont(vpath);
104104
//g_font[i] = new FTPixmapFont(vpath);

client/acct_setup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ int GET_PROJECT_LIST_OP::do_rpc() {
213213
int retval;
214214
char buf[256];
215215

216-
sprintf(buf, "https://boinc.berkeley.edu/project_list.php");
216+
snprintf(buf, sizeof(buf), "https://boinc.berkeley.edu/project_list.php");
217217
retval = gui_http->do_rpc(
218218
this, buf, ALL_PROJECTS_LIST_FILENAME_TEMP, true
219219
);

client/app_control.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ bool ACTIVE_TASK::finish_file_present(int &exit_code) {
630630

631631
exit_code = 0;
632632

633-
sprintf(path, "%s/%s", slot_dir, BOINC_FINISH_CALLED_FILE);
633+
snprintf(path, sizeof(path), "%s/%s", slot_dir, BOINC_FINISH_CALLED_FILE);
634634
FILE* f = boinc_fopen(path, "r");
635635
if (!f) return false;
636636
char* p = fgets(buf, sizeof(buf), f);
@@ -657,7 +657,7 @@ bool ACTIVE_TASK::temporary_exit_file_present(
657657
double& x, char* buf, bool& is_notice
658658
) {
659659
char path[MAXPATHLEN], buf2[256];
660-
sprintf(path, "%s/%s", slot_dir, TEMPORARY_EXIT_FILE);
660+
snprintf(path, sizeof(path), "%s/%s", slot_dir, TEMPORARY_EXIT_FILE);
661661
FILE* f = boinc_fopen(path, "r");
662662
if (!f) return false;
663663
strcpy(buf, "");
@@ -888,7 +888,7 @@ bool ACTIVE_TASK_SET::check_rsc_limits_exceeded() {
888888
atp = active_tasks[i];
889889
if (atp->task_state() != PROCESS_EXECUTING) continue;
890890
if (!atp->result->non_cpu_intensive() && (atp->elapsed_time > atp->max_elapsed_time)) {
891-
sprintf(buf, "exceeded elapsed time limit %.2f (%.2fG/%.2fG)",
891+
snprintf(buf, sizeof(buf), "exceeded elapsed time limit %.2f (%.2fG/%.2fG)",
892892
atp->max_elapsed_time,
893893
atp->result->wup->rsc_fpops_bound/1e9,
894894
atp->result->avp->flops/1e9
@@ -908,7 +908,7 @@ bool ACTIVE_TASK_SET::check_rsc_limits_exceeded() {
908908
// accurate bounds.
909909
//
910910
if (atp->procinfo.working_set_size_smoothed > atp->max_mem_usage) {
911-
sprintf(buf, "working set size > workunit.rsc_memory_bound: %.2fMB > %.2fMB",
911+
snprintf(buf, sizeof(buf), "working set size > workunit.rsc_memory_bound: %.2fMB > %.2fMB",
912912
atp->procinfo.working_set_size_smoothed/MEGA, atp->max_mem_usage/MEGA
913913
);
914914
msg_printf(atp->result->project, MSG_INFO,
@@ -921,7 +921,7 @@ bool ACTIVE_TASK_SET::check_rsc_limits_exceeded() {
921921
}
922922
#endif
923923
if (atp->procinfo.working_set_size_smoothed > max_ram) {
924-
sprintf(buf, "working set size > client RAM limit: %.2fMB > %.2fMB",
924+
snprintf(buf, sizeof(buf), "working set size > client RAM limit: %.2fMB > %.2fMB",
925925
atp->procinfo.working_set_size_smoothed/MEGA, max_ram/MEGA
926926
);
927927
msg_printf(atp->result->project, MSG_INFO,
@@ -987,7 +987,7 @@ int ACTIVE_TASK::read_stderr_file() {
987987
// it's unlikely that more than that will be useful
988988
//
989989
int max_len = 63*1024;
990-
sprintf(path, "%s/%s", slot_dir, STDERR_FILE);
990+
snprintf(path, sizeof(path), "%s/%s", slot_dir, STDERR_FILE);
991991
if (!boinc_file_exists(path)) return 0;
992992
int retval = read_file_malloc(
993993
path, buf1, max_len, !cc_config.stderr_head
@@ -1560,7 +1560,7 @@ void ACTIVE_TASK_SET::get_msgs() {
15601560
if (atp->get_app_status_msg()) {
15611561
if (old_time != atp->checkpoint_cpu_time) {
15621562
char buf[512];
1563-
sprintf(buf, "%s checkpointed", atp->result->name);
1563+
snprintf(buf, sizeof(buf), "%s checkpointed", atp->result->name);
15641564
if (atp->overdue_checkpoint) {
15651565
gstate.request_schedule_cpus(buf);
15661566
}
@@ -1589,7 +1589,7 @@ void ACTIVE_TASK_SET::get_msgs() {
15891589
//
15901590
void ACTIVE_TASK::write_task_state_file() {
15911591
char path[MAXPATHLEN];
1592-
sprintf(path, "%s/%s", slot_dir, TASK_STATE_FILENAME);
1592+
snprintf(path, sizeof(path), "%s/%s", slot_dir, TASK_STATE_FILENAME);
15931593
FILE* f = boinc_fopen(path, "w");
15941594
if (!f) return;
15951595
fprintf(f,
@@ -1620,7 +1620,7 @@ void ACTIVE_TASK::write_task_state_file() {
16201620
//
16211621
void ACTIVE_TASK::read_task_state_file() {
16221622
char buf[4096], path[MAXPATHLEN], s[1024];
1623-
sprintf(path, "%s/%s", slot_dir, TASK_STATE_FILENAME);
1623+
snprintf(path, sizeof(path), "%s/%s", slot_dir, TASK_STATE_FILENAME);
16241624
FILE* f = boinc_fopen(path, "r");
16251625
if (!f) return;
16261626
buf[0] = 0;

client/app_graphics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ void ACTIVE_TASK::request_graphics_mode(GRAPHICS_MSG& m) {
6060

6161
safe_strcpy(buf, xml_graphics_modes[m.mode]);
6262
if (strlen(m.window_station)) {
63-
sprintf(buf2, "<window_station>%s</window_station>", m.window_station);
63+
snprintf(buf2, sizeof(buf2), "<window_station>%s</window_station>", m.window_station);
6464
strcat(buf, buf2);
6565
}
6666
if (strlen(m.desktop)) {
67-
sprintf(buf2, "<desktop>%s</desktop>", m.desktop);
67+
snprintf(buf2, sizeof(buf2), "<desktop>%s</desktop>", m.desktop);
6868
strcat(buf, buf2);
6969
}
7070
if (strlen(m.display)) {
71-
sprintf(buf2, "<display>%s</display>", m.display);
71+
snprintf(buf2, sizeof(buf2), "<display>%s</display>", m.display);
7272
strcat(buf, buf2);
7373
}
7474

client/app_start.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static void coproc_cmdline(
122122
);
123123
k = 0;
124124
}
125-
sprintf(buf, " --device %d", coproc->device_nums[k]);
125+
snprintf(buf, sizeof(buf), " --device %d", coproc->device_nums[k]);
126126
strlcat(cmdline, buf, cmdline_len);
127127
}
128128
}
@@ -137,15 +137,15 @@ int ACTIVE_TASK::get_shmem_seg_name() {
137137

138138
bool try_global = (sandbox_account_service_token != NULL);
139139
for (i=0; i<1024; i++) {
140-
sprintf(seg_name, "%sboinc_%d", SHM_PREFIX, i);
140+
snprintf(seg_name, sizeof(seg_name), "%sboinc_%d", SHM_PREFIX, i);
141141
shm_handle = create_shmem(
142142
seg_name, sizeof(SHARED_MEM), (void**)&app_client_shm.shm,
143143
try_global
144144
);
145145
if (shm_handle) break;
146146
}
147147
if (!shm_handle) return ERR_SHMGET;
148-
sprintf(shmem_seg_name, "boinc_%d", i);
148+
snprintf(shmem_seg_name, sizeof(shmem_seg_name), "boinc_%d", i);
149149
#else
150150
char init_data_path[MAXPATHLEN];
151151
#ifndef __EMX__
@@ -155,7 +155,7 @@ int ACTIVE_TASK::get_shmem_seg_name() {
155155
return 0;
156156
}
157157
#endif
158-
sprintf(init_data_path, "%s/%s", slot_dir, INIT_DATA_FILE);
158+
snprintf(init_data_path, sizeof(init_data_path), "%s/%s", slot_dir, INIT_DATA_FILE);
159159

160160
// ftok() only works if there's a file at the given location
161161
//
@@ -293,7 +293,7 @@ int ACTIVE_TASK::write_app_init_file(APP_INIT_DATA& aid) {
293293
);
294294
#endif
295295

296-
sprintf(init_data_path, "%s/%s", slot_dir, INIT_DATA_FILE);
296+
snprintf(init_data_path, sizeof(init_data_path), "%s/%s", slot_dir, INIT_DATA_FILE);
297297

298298
// delete the file using the switcher (Unix)
299299
// in case it's owned by another user and we don't have write access
@@ -869,7 +869,7 @@ int ACTIVE_TASK::start(bool test) {
869869
//
870870
retval = chdir(slot_dir);
871871
if (retval) {
872-
sprintf(buf, "Can't change directory to %s: %s", slot_dir, boincerror(retval));
872+
snprintf(buf, sizeof(buf), "Can't change directory to %s: %s", slot_dir, boincerror(retval));
873873
goto error;
874874
}
875875

client/check_security.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ int use_sandbox, int isManager, char* path_to_error, int len
261261
if (use_sandbox) {
262262
for (int i=0; i<NUMBRANDS; i++) {
263263
// Does gfx_switcher exist in screensaver bundle?
264-
sprintf(full_path, "/Library/Screen Savers/%s.saver/Contents/Resources/gfx_switcher", saverName[i]);
264+
snprintf(full_path, sizeof(full_path), "/Library/Screen Savers/%s.saver/Contents/Resources/gfx_switcher", saverName[i]);
265265
retval = stat(full_path, &sbuf);
266266
if (! retval) {
267267
#ifdef _DEBUG
@@ -631,7 +631,7 @@ static void GetPathToThisProcess(char* outbuf, size_t maxLen) {
631631

632632
*outbuf = '\0';
633633

634-
sprintf(buf, "ps -xwo command -p %d", (int)aPID);
634+
snprintf(buf, sizeof(buf), "ps -xwo command -p %d", (int)aPID);
635635
f = popen(buf, "r");
636636
if (f == NULL)
637637
return;

client/client_state.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,7 @@ int CLIENT_STATE::report_result_error(RESULT& res, const char* err_msg) {
19101910
res.set_ready_to_report();
19111911
res.completed_time = now;
19121912

1913-
sprintf(buf, "Unrecoverable error for task %s", res.name);
1913+
snprintf(buf, sizeof(buf), "Unrecoverable error for task %s", res.name);
19141914
#ifndef SIM
19151915
scheduler_op->project_rpc_backoff(res.project, buf);
19161916
#endif
@@ -1952,7 +1952,7 @@ int CLIENT_STATE::report_result_error(RESULT& res, const char* err_msg) {
19521952
//
19531953
for (i=0; i<res.output_files.size(); i++) {
19541954
if (res.output_files[i].file_info->had_failure(failnum)) {
1955-
sprintf(buf,
1955+
snprintf(buf, sizeof(buf),
19561956
"<upload_error>\n"
19571957
" <file_name>%s</file_name>\n"
19581958
" <error_code>%d</error_code>\n"
@@ -2297,7 +2297,7 @@ void CLIENT_STATE::log_show_projects() {
22972297
for (unsigned int i=0; i<projects.size(); i++) {
22982298
PROJECT* p = projects[i];
22992299
if (p->hostid) {
2300-
sprintf(buf, "%d", p->hostid);
2300+
snprintf(buf, sizeof(buf), "%d", p->hostid);
23012301
} else {
23022302
safe_strcpy(buf, "not assigned yet");
23032303
}

0 commit comments

Comments
 (0)