From: Martin Quinson Date: Sat, 28 May 2016 21:09:57 +0000 (+0200) Subject: change some sprintf into snprintf X-Git-Tag: v3_14~1127 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/74ffd4ad21972c63bc21b07b22df673420e0bb47?hp=847ed634dcf26a99c8fb7751fe72720c391e2c4d change some sprintf into snprintf --- diff --git a/examples/msg/actions-storage/actions-storage.c b/examples/msg/actions-storage/actions-storage.c index 2be369d52e..76d399ae69 100644 --- a/examples/msg/actions-storage/actions-storage.c +++ b/examples/msg/actions-storage/actions-storage.c @@ -30,7 +30,7 @@ static msg_file_t get_file_descriptor(const char *file_name){ char full_name[1024]; msg_file_t file = NULL; - sprintf(full_name, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name); + snprintf(full_name,1023, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name); file = (msg_file_t) xbt_dict_get_or_null(opened_files, full_name); return file; @@ -52,7 +52,7 @@ static void action_open(const char *const *action) { msg_file_t file = NULL; double clock = MSG_get_clock(); - sprintf(full_name, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name); + snprintf(full_name,1023, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name); ACT_DEBUG("Entering Open: %s (filename: %s)", NAME, file_name); file = MSG_file_open(file_name, NULL); diff --git a/examples/msg/app-bittorrent/peer.c b/examples/msg/app-bittorrent/peer.c index 0ef1d35574..9469fab43a 100644 --- a/examples/msg/app-bittorrent/peer.c +++ b/examples/msg/app-bittorrent/peer.c @@ -204,8 +204,8 @@ int get_peers_data(peer_t peer) void peer_init(peer_t peer, int id, int seed) { peer->id = id; - sprintf(peer->mailbox, "%d", id); - sprintf(peer->mailbox_tracker, "tracker_%d", id); + snprintf(peer->mailbox,MAILBOX_SIZE-1, "%d", id); + snprintf(peer->mailbox_tracker,MAILBOX_SIZE-1, "tracker_%d", id); peer->peers = xbt_dict_new(); peer->active_peers = xbt_dict_new(); peer->hostname = MSG_host_get_name(MSG_host_self()); diff --git a/examples/msg/app-masterworker/app-masterworker.c b/examples/msg/app-masterworker/app-masterworker.c index 06406336d5..899253db98 100644 --- a/examples/msg/app-masterworker/app-masterworker.c +++ b/examples/msg/app-masterworker/app-masterworker.c @@ -24,8 +24,8 @@ static int master(int argc, char *argv[]) char mailbox[256]; char task_name[256]; - sprintf(mailbox, "worker-%ld", i % workers_count); /* - Select a @ref worker in a round-robin way */ - sprintf(task_name, "Task_%d", i); + snprintf(mailbox,255, "worker-%ld", i % workers_count); /* - Select a @ref worker in a round-robin way */ + snprintf(task_name,255, "Task_%d", i); msg_task_t task = MSG_task_create(task_name, comp_size, comm_size, NULL); /* - Create a task */ if (number_of_tasks < 10000 || i % 10000 == 0) XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox); @@ -37,7 +37,7 @@ static int master(int argc, char *argv[]) for (i = 0; i < workers_count; i++) { /* - Eventually tell all the workers to stop by sending a "finalize" task */ char mailbox[80]; - sprintf(mailbox, "worker-%ld", i % workers_count); + snprintf(mailbox,79, "worker-%ld", i % workers_count); msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0); MSG_task_send(finalize, mailbox); } @@ -53,7 +53,7 @@ static int worker(int argc, char *argv[]) long id= xbt_str_parse_int(argv[1], "Invalid argument %s"); - sprintf(mailbox, "worker-%ld", id); + snprintf(mailbox,79, "worker-%ld", id); while (1) { /* The worker wait in an infinite loop for tasks sent by the \ref master */ int res = MSG_task_receive(&(task), mailbox); diff --git a/examples/msg/app-token-ring/app-token-ring.c b/examples/msg/app-token-ring/app-token-ring.c index abe169117c..858889e986 100644 --- a/examples/msg/app-token-ring/app-token-ring.c +++ b/examples/msg/app-token-ring/app-token-ring.c @@ -16,7 +16,7 @@ static int foo(int argc, char *argv[]) msg_task_t task = NULL; XBT_ATTRIB_UNUSED int res; if (rank == 0){ /* - The root (rank 0) first sends the token then waits to receive it back */ - sprintf(mailbox, "%d", rank+1); + snprintf(mailbox,255, "%d", rank+1); task = MSG_task_create("Token", 0, task_comm_size, NULL); XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"", rank, task->name,mailbox); MSG_task_send(task, mailbox); @@ -31,9 +31,9 @@ static int foo(int argc, char *argv[]) XBT_INFO("Host \"%d\" received \"%s\"",rank, MSG_task_get_name(task)); if(rank+1 == MSG_get_host_number()) /* - Except for the last one which sends the token back to rank 0 */ - sprintf(mailbox, "0"); + snprintf(mailbox,255, "0"); else - sprintf(mailbox, "%d", rank+1); + snprintf(mailbox,255, "%d", rank+1); XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",rank,task->name,mailbox); MSG_task_send(task, mailbox); } diff --git a/examples/msg/async-wait/async-wait.c b/examples/msg/async-wait/async-wait.c index da753e836b..814411a868 100644 --- a/examples/msg/async-wait/async-wait.c +++ b/examples/msg/async-wait/async-wait.c @@ -25,13 +25,13 @@ static int sender(int argc, char *argv[]) MSG_process_sleep(sleep_start_time); for (i = 0; i < number_of_tasks; i++) { char mailbox[256]; - char sprintf_buffer[256]; + char snprintf_buffer[256]; - sprintf(mailbox, "receiver-%ld", i % receivers_count); - sprintf(sprintf_buffer, "Task_%d", i); + snprintf(mailbox,255, "receiver-%ld", i % receivers_count); + snprintf(snprintf_buffer,255, "Task_%d", i); /* This process first creates a task and send it asynchronously with @ref MSG_task_isend. Then, if: */ - task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL); + task = MSG_task_create(snprintf_buffer, task_comp_size, task_comm_size, NULL); comm = MSG_task_isend(task, mailbox); XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i); @@ -47,7 +47,7 @@ static int sender(int argc, char *argv[]) for (i = 0; i < receivers_count; i++) { char mailbox[80]; - sprintf(mailbox, "receiver-%ld", i % receivers_count); + snprintf(mailbox,79, "receiver-%ld", i % receivers_count); task = MSG_task_create("finalize", 0, 0, 0); comm = MSG_task_isend(task, mailbox); XBT_INFO("Send to receiver-%ld finalize", i % receivers_count); @@ -79,7 +79,7 @@ static int receiver(int argc, char *argv[]) MSG_process_sleep(sleep_start_time); /* This process first sleeps for "start time" seconds. */ - sprintf(mailbox, "receiver-%d", id); + snprintf(mailbox,79, "receiver-%d", id); while (1) { res_irecv = MSG_task_irecv(&(task), mailbox); /* Then it posts asynchronous receives (@ref MSG_task_irecv) and*/ XBT_INFO("Wait to receive a task"); diff --git a/examples/msg/async-waitall/async-waitall.c b/examples/msg/async-waitall/async-waitall.c index 6c3848e706..bdef25d6d3 100644 --- a/examples/msg/async-waitall/async-waitall.c +++ b/examples/msg/async-waitall/async-waitall.c @@ -19,16 +19,16 @@ static int sender(int argc, char *argv[]) msg_task_t task = NULL; for (i = 0; i < number_of_tasks; i++) { char mailbox[256]; - char sprintf_buffer[256]; - sprintf(mailbox, "receiver-%ld", i % receivers_count); - sprintf(sprintf_buffer, "Task_%d", i); - task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL); + char snprintf_buffer[256]; + snprintf(mailbox,255, "receiver-%ld", i % receivers_count); + snprintf(snprintf_buffer,255, "Task_%d", i); + task = MSG_task_create(snprintf_buffer, task_comp_size, task_comm_size, NULL); comm[i] = MSG_task_isend(task, mailbox); XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i); } for (i = 0; i < receivers_count; i++) { char mailbox[80]; - sprintf(mailbox, "receiver-%ld", i % receivers_count); + snprintf(mailbox,79, "receiver-%ld", i % receivers_count); task = MSG_task_create("finalize", 0, 0, 0); comm[i + number_of_tasks] = MSG_task_isend(task, mailbox); XBT_INFO("Send to receiver-%ld finalize", i % receivers_count); @@ -55,7 +55,7 @@ static int receiver(int argc, char *argv[]) read = sscanf(argv[1], "%d", &id); xbt_assert(read, "Invalid argument %s\n", argv[1]); MSG_process_sleep(10); - sprintf(mailbox, "receiver-%d", id); + snprintf(mailbox,79, "receiver-%d", id); while (1) { res_irecv = MSG_task_irecv(&(task), mailbox); XBT_INFO("Wait to receive a task"); diff --git a/examples/msg/async-waitany/async-waitany.c b/examples/msg/async-waitany/async-waitany.c index a1340c1167..f5d76315a0 100644 --- a/examples/msg/async-waitany/async-waitany.c +++ b/examples/msg/async-waitany/async-waitany.c @@ -19,7 +19,7 @@ static int sender(int argc, char *argv[]) int i; msg_task_t task; char mailbox[256]; - char sprintf_buffer[256]; + char snprintf_buffer[256]; msg_comm_t comm; for (i = 0; i < number_of_tasks; i++) { @@ -28,12 +28,12 @@ static int sender(int argc, char *argv[]) else coef = (i + 1); - sprintf(mailbox, "receiver-%ld", (i % receivers_count)); - sprintf(sprintf_buffer, "Task_%d", i); - task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size / coef, NULL); + snprintf(mailbox,255, "receiver-%ld", (i % receivers_count)); + snprintf(snprintf_buffer,255, "Task_%d", i); + task = MSG_task_create(snprintf_buffer, task_comp_size, task_comm_size / coef, NULL); comm = MSG_task_isend(task, mailbox); xbt_dynar_push_as(d, msg_comm_t, comm); - XBT_INFO("Send to receiver-%ld %s comm_size %f", i % receivers_count, sprintf_buffer, task_comm_size / coef); + XBT_INFO("Send to receiver-%ld %s comm_size %f", i % receivers_count, snprintf_buffer, task_comm_size / coef); } /* Here we are waiting for the completion of all communications */ @@ -70,7 +70,7 @@ static int receiver(int argc, char *argv[]) XBT_ATTRIB_UNUSED int read; read = sscanf(argv[1], "%d", &id); xbt_assert(read, "Invalid argument %s\n", argv[1]); - sprintf(mailbox, "receiver-%d", id); + snprintf(mailbox,79, "receiver-%d", id); MSG_process_sleep(10); msg_comm_t res_irecv; for (int i = 0; i < tasks; i++) { diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index 006ea0d932..cb8ef294cd 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -1029,27 +1029,27 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") d = xbt_dynar_new(sizeof(char *), &xbt_free_ref); /* 1. Populate the dynar */ for (cpt = 0; cpt < NB_ELEM; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); s1 = xbt_strdup(buf); xbt_dynar_push(d, &s1); } for (cpt = 0; cpt < NB_ELEM; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); s1 = xbt_strdup(buf); xbt_dynar_replace(d, cpt, &s1); } for (cpt = 0; cpt < NB_ELEM; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); s1 = xbt_strdup(buf); xbt_dynar_replace(d, cpt, &s1); } for (cpt = 0; cpt < NB_ELEM; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); s1 = xbt_strdup(buf); xbt_dynar_replace(d, cpt, &s1); } for (cpt = 0; cpt < NB_ELEM; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); xbt_dynar_shift(d, &s2); xbt_test_assert(!strcmp(buf, s2), "The retrieved value is not the same than the injected one (%s!=%s)", buf, s2); free(s2); @@ -1061,18 +1061,18 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") xbt_test_add("==== Unshift, traverse and pop %d strings", NB_ELEM); d = xbt_dynar_new(sizeof(char **), &xbt_free_ref); for (cpt = 0; cpt < NB_ELEM; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); s1 = xbt_strdup(buf); xbt_dynar_unshift(d, &s1); } /* 2. Traverse the dynar with the macro */ xbt_dynar_foreach(d, iter, s1) { - sprintf(buf, "%u", NB_ELEM - iter - 1); + snprintf(buf,1023, "%u", NB_ELEM - iter - 1); xbt_test_assert(!strcmp(buf, s1), "The retrieved value is not the same than the injected one (%s!=%s)", buf, s1); } /* 3. Traverse the dynar with the macro */ for (cpt = 0; cpt < NB_ELEM; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); xbt_dynar_pop(d, &s2); xbt_test_assert(!strcmp(buf, s2), "The retrieved value is not the same than the injected one (%s!=%s)", buf, s2); free(s2); @@ -1085,32 +1085,32 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") xbt_test_add("==== Push %d strings, insert %d strings in the middle, shift everything", NB_ELEM, NB_ELEM / 5); d = xbt_dynar_new(sizeof(char *), &xbt_free_ref); for (cpt = 0; cpt < NB_ELEM; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); s1 = xbt_strdup(buf); xbt_dynar_push(d, &s1); } for (cpt = 0; cpt < NB_ELEM / 5; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); s1 = xbt_strdup(buf); xbt_dynar_insert_at(d, NB_ELEM / 2, &s1); } for (cpt = 0; cpt < NB_ELEM / 2; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); xbt_dynar_shift(d, &s2); xbt_test_assert(!strcmp(buf, s2), "The retrieved value is not the same than the injected one at the begining (%s!=%s)", buf, s2); free(s2); } for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); xbt_dynar_shift(d, &s2); xbt_test_assert(!strcmp(buf, s2), "The retrieved value is not the same than the injected one in the middle (%s!=%s)", buf, s2); free(s2); } for (cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); xbt_dynar_shift(d, &s2); xbt_test_assert(!strcmp(buf, s2), "The retrieved value is not the same than the injected one at the end (%s!=%s)", buf, s2); @@ -1123,12 +1123,12 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") xbt_test_add("==== Push %d strings, remove %d-%d. free the rest", NB_ELEM, 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5)); d = xbt_dynar_new(sizeof(char *), &xbt_free_ref); for (cpt = 0; cpt < NB_ELEM; cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); s1 = xbt_strdup(buf); xbt_dynar_push(d, &s1); } for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) { - sprintf(buf, "%d", cpt); + snprintf(buf,1023, "%d", cpt); xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2); xbt_test_assert(!strcmp(buf, s2), "Remove a bad value. Got %s, expected %s", s2, buf); free(s2);