-
Couldn't load subscription status.
- Fork 928
Replace sprintf with snprintf #13468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
01f60b8
fa2d6e0
bfee6d7
296713c
2fbeaa7
67ccb23
f298795
c9ba524
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -325,9 +325,9 @@ abbreviate_list_into_string(char *str, int max, int *list, int nlist) | |||||
| strcpy(&str[strlen(str)], ", "); | ||||||
| } | ||||||
| if (lo != hi) { | ||||||
| sprintf(&str[strlen(str)], "%d - %d", lo, hi); | ||||||
| snprintf(&str[strlen(str)], max - strlen(str), "%d - %d", lo, hi); | ||||||
| } else { | ||||||
| sprintf(&str[strlen(str)], "%d", lo); | ||||||
| snprintf(&str[strlen(str)], max - strlen(str), "%d", lo); | ||||||
| } | ||||||
| } | ||||||
| /* | ||||||
|
|
@@ -352,9 +352,9 @@ abbreviate_list_into_string(char *str, int max, int *list, int nlist) | |||||
| strcpy(&str[strlen(str)], ", "); | ||||||
| } | ||||||
| if (lo != hi) { | ||||||
| sprintf(&str[strlen(str)], "%d - %d", lo, hi); | ||||||
| snprintf(&str[strlen(str)], max - strlen(str), "%d - %d", lo, hi); | ||||||
| } else { | ||||||
| sprintf(&str[strlen(str)], "%d", lo); | ||||||
| snprintf(&str[strlen(str)], max - strlen(str), "%d", lo); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -460,7 +460,7 @@ ompi_report_comm_methods(int called_from_location) | |||||
|
|
||||||
| len = strlen(opal_process_info.nodename) + 100; | ||||||
| hoststring = malloc(len + 1); | ||||||
| sprintf(hoststring, "Host %d [%s] ranks ", | ||||||
| snprintf(hoststring, len + 1, "Host %d [%s] ranks ", | ||||||
| myleaderrank, opal_process_info.nodename); | ||||||
|
|
||||||
| abbreviate_list_into_string(&hoststring[strlen(hoststring)], | ||||||
|
|
@@ -642,7 +642,7 @@ ompi_report_comm_methods(int called_from_location) | |||||
| // 2: 2d table | ||||||
| if (nleaderranks <= max2Dprottable) { | ||||||
| char *str, *p; | ||||||
| int tmp, per, has_ucx_transport; | ||||||
| int tmp, per, has_ucx_transport, bufsize; | ||||||
| int strlens[NUM_COMM_METHODS]; | ||||||
|
|
||||||
| // characters per entry in the 2d table, must be large enough | ||||||
|
|
@@ -668,11 +668,11 @@ ompi_report_comm_methods(int called_from_location) | |||||
| if (tmp+1 > per) { per = tmp+1; } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| str = malloc(nleaderranks * per + 1); | ||||||
| bufsize = nleaderranks * per + 1; | ||||||
| str = malloc(bufsize); | ||||||
| p = str; | ||||||
| for (i=0; i<nleaderranks; ++i) { | ||||||
| sprintf(p, "%d", i); | ||||||
| snprintf(p, bufsize - (p - str), "%d", i); | ||||||
| for (j=(int)strlen(p); j<per; ++j) { | ||||||
| p[j] = ' '; | ||||||
| } | ||||||
|
|
@@ -698,12 +698,12 @@ ompi_report_comm_methods(int called_from_location) | |||||
| for (k=0; k<nleaderranks; ++k) { | ||||||
| char *method_string; | ||||||
| char ucx_label[20]; | ||||||
|
|
||||||
| method_string = comm_method_to_string(method[i * nleaderranks + k]); | ||||||
| if (0 == strncmp(method_string, UCX_TAG, strlen(UCX_TAG))) { | ||||||
| n = lookup_string_in_conversion_struct(&comm_method_string_conversion, | ||||||
| method_string); | ||||||
| sprintf(ucx_label, "ucx[%3d]", n); | ||||||
| snprintf(ucx_label, sizeof(ucx_label), "ucx[%3d]", n); | ||||||
| strcat(p, ucx_label); | ||||||
| methods_used[n / 8] |= (1 << (n % 8)); | ||||||
| has_ucx_transport = 1; | ||||||
|
|
@@ -755,7 +755,7 @@ ompi_report_comm_methods(int called_from_location) | |||||
| } | ||||||
| else if (nleaderranks <= max2D1Cprottable) { | ||||||
| char *str, *p; | ||||||
| int tmp, per, done; | ||||||
| int tmp, per, done, bufsize; | ||||||
| char char_code[NUM_COMM_METHODS], next_char; | ||||||
| int method_count[NUM_COMM_METHODS]; | ||||||
|
|
||||||
|
|
@@ -798,12 +798,13 @@ ompi_report_comm_methods(int called_from_location) | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| str = malloc(per + 32 + nleaderranks * 2 + 1); | ||||||
| bufsize = nleaderranks * (per + 1) + 1; | ||||||
|
||||||
| bufsize = nleaderranks * (per + 1) + 1; | |
| bufsize = 8 + (nleaderranks * (per + 1)) + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is incorrect. It accounts for the initial string by starting the loop below from 4 instead of 0. What I don't understand in this code is why it assumes there are always at least 4 leaderranks ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're in a branch where we have a minimum number of leaderranks: https://github.com/open-mpi/ompi/pull/13468/files#diff-4bcdf923dce180dbcc719b3472dd10527a4c285021962e918f5365a75eac81f6L756
I think copilot is right that I messed up the size computation (technically, that was copilot and I didn't pay enough attention ^^)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two usages of the allocated string. The first one requires8 + ((nleaderranks - 4) / 4) * 8 + 1 chars and the second one 2 * nleaderranks + 1 chars. You are allocating 8 + (nleaderranks * (per + 1)) + 1 with per being set to the number of digits in nleaderranks or 2 whichever is largest).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to understand the memory allocation above I got totally confused by this piece of code. Why is j looping up to 8 instead of up to per ? A similar code exists around line 650 and it clear the string up to per for each leaderrank.
Uh oh!
There was an error while loading. Please reload this page.