Skip to content
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

Fix sprintf issues #298

Merged
merged 1 commit into from
Sep 19, 2024
Merged

Fix sprintf issues #298

merged 1 commit into from
Sep 19, 2024

Conversation

billsacks
Copy link
Member

Fixes uses of sprintf where the output string is also used in the input, which can result in garbled time strings.

Also changes these sprintf calls to snprintf to avoid possible buffer overflows, and checks these calls to ensure that the output variable is large enough to hold the relevant string, returning an error code if not.

Resolves #284

Fixes uses of sprintf where the output string is also used in the input,
which can result in garbled time strings.

Also changes these sprintf calls to snprintf to avoid possible buffer
overflows, and checks these calls to ensure that the output variable is
large enough to hold the relevant string, returning an error code if
not.

Resolves #284
@billsacks billsacks requested a review from oehmke September 17, 2024 22:51
@billsacks
Copy link
Member Author

@oehmke - I'd be happy for a review of this if you'd like to look it over - but I feel good enough about these changes that I don't think a review is necessary if you don't have the time.

Comment on lines +1181 to +1186
std::stringstream msg;
msg << " - there was a problem (e.g. repeated points, clockwise poly, etc.) with the triangulation of the element:\n";
msg << "elem Id: " << elem.get_id() << " clip_elem Id: " << clip_elem.get_id() << "\n";
{
cd_sph = new double[num_p*2]; cart2sph(num_p, pts, cd_sph);
for(int npt=0; npt<num_p*2; npt++) sprintf(msg,"%s %g", msg, cd_sph[npt]);
cd_sph = new double[num_p*2]; cart2sph(num_p, pts, cd_sph);
for(int npt=0; npt<num_p*2; npt++) msg << " " << cd_sph[npt];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this change is untested because it is inside an #if 0 block.

I tested it by:

(1) Testing the build when I changed #if 0 to #if 1 in the surrounding block

(2) Writing the following test program that checks the msg string before and after this change, ensuring that the resulting string is identical before and after:

#include <iostream>
#include <sstream>

using namespace std;

int main() {
   int num_p = 4;
   double *cd_sph = new double[num_p * 2];
   for (int npt = 0; npt < num_p * 2; npt++) {
      cd_sph[npt] = 24 + 36.003*npt;
   }

   char msg[1024];
   sprintf(msg, " - there was a problem (e.g. repeated points, clockwise poly, etc.) with the triangulation of the element:\n");
   sprintf(msg, "%selem Id: %d clip_elem Id: %d\n", msg, 1005, 123456);
   for (int npt = 0; npt < num_p * 2; npt++)
      sprintf(msg, "%s %g", msg, cd_sph[npt]);

   std::stringstream msg2;
   msg2 << " - there was a problem (e.g. repeated points, clockwise poly, etc.) with the triangulation of the element:\n";
   msg2 << "elem Id: " << 1005 << " clip_elem Id: " << 123456 << "\n";
   for (int npt = 0; npt < num_p * 2; npt++)
      msg2 << " " << cd_sph[npt];

   cout << msg << "\n";
   cout << msg2.str() << "\n";
}

@billsacks
Copy link
Member Author

It looks like the test failure here is just in the ESMF_CompTunnelUTest, which I think @danrosen25 has seen failing in these automated runs periodically.

@danrosen25
Copy link
Member

It looks like the test failure here is just in the ESMF_CompTunnelUTest, which I think @danrosen25 has seen failing in these automated runs periodically.

I checked and it's the same error about Finalize finishing in less than 3 seconds even though there's a 3 second delay. The issue for it is here: #295

@danrosen25
Copy link
Member

@billsacks
I reran the failed mac os + openmpi test after rolling back the openmpi version. This time it passed. It could be a bug in openmpi or it could just be that openmpi v5+ calls to MPI_Wtime execute faster.

Copy link
Contributor

@oehmke oehmke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I appreciate your careful testing, especially of the edge cases. It's nice to get this in. It'll make the library safer. Thanks!

@billsacks billsacks merged commit 46dfc47 into develop Sep 19, 2024
6 checks passed
@billsacks billsacks deleted the fix_sprintf branch September 19, 2024 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Timemgr using sprintf with the output string also used in the input
3 participants