-
Notifications
You must be signed in to change notification settings - Fork 693
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
Quiet compiler warnings for registry.exe #1858
base: develop
Are you sure you want to change the base?
Conversation
@DWesl It looks like the compilation is failed for all of the tests. Our regression tests are run using gfortran compiler. Here is a particular error (also see the attached file): make[2]: Entering directory '/wrf/WRF/tools' |
Interesting. My own tests all used GFortran, so it's likely a Cygwin/Linux difference rather than a compiler one. I set up guards so the |
OK, now our regression tests have passed:
|
As have mine on Cygwin (with #1812) |
Am I missing anything for this PR? |
@islas Are any of the changes proposed here in conflict with anything you've been working on? |
@weiwangncar Nope, no conflicts should arise from our respective changes in registry |
@islas Great! Should we consider this in develop or for 4.5.2? |
I'd say this would be a nice-to-have if we can get it in, but not critical. |
@islas Thanks. We can leave it on the develop branch. |
tools/gen_irr_diag.c
Outdated
strcat( line,piece ); | ||
} | ||
strcat( line," /)\n" ); | ||
fprintf( fp_inc,line ); | ||
fprintf( fp_inc," \n"); | ||
|
||
for( i = 0; i < nChmOpts,rxt_cnt[i] > 0; i++ ) { | ||
for( i = 0; /*i < nChmOpts &&*/ rxt_cnt[i] > 0; i++ ) { |
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.
It seems like this is relying on rxt_cnt
to be default initialized to zero. I'm not too sure what it is used for, but given that all other loops use nChmOpts
to specify number of indices into rxt_cnt
, keeping this check seems prudent
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.
The old version was ignoring the result of the nChmOpts
comparison. Are you suggesting that check be included in the for-loop exit condition?
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.
Ah! You're correct, I forgot that this use of the ,
operator is nulling and not &&
I'd imagine that the original intent was to have it included via &&
given the other loops. Maybe @weiwangncar or someone else with historic knowledge on this section of the Registry can chime in. Otherwise, as long as tests agree, I think that check should be there
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.
Changed to the suggested form. I'd still like to get an opinion from someone who knows this bit of the code better than me.
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.
A few magic constants I didn't catch earlier, plus some possibly-premature optimizations.
|
||
if ( p->ntl > 1 ) { sprintf(tag,"_2") ; sprintf(tag2,"_%d", use_nest_time_level) ; } | ||
else { sprintf(tag,"") ; sprintf(tag2,"") ; } | ||
else { tag[0] = '\0'; tag2[0] = '\0'; } |
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.
sprintf
will likely be clearer, and the compiler may notice this optimization anyway.
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.
Alternately, strcpy
/strncpy
@@ -200,7 +200,7 @@ gen_nest_interp1 ( FILE * fp , node_t * node, char * fourdname, int down_path , | |||
if ( nest_mask & down_path ) | |||
{ | |||
if ( p->ntl > 1 ) { sprintf(tag,"_2") ; sprintf(tag2,"_%d", use_nest_time_level) ; } | |||
else { sprintf(tag,"") ; sprintf(tag2,"") ; } | |||
else { tag[0] = '\0'; tag2[0] = '\0'; } |
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.
sprintf
or strcpy
will likely be clearer, and the compiler might notice this optimization anyway.
@@ -130,7 +130,7 @@ else if ( down_path[ipath] == FORCE_DOWN ) { sprintf(halo_id,"HALO_FORCE_DOWN") | |||
else if ( down_path[ipath] == INTERP_UP ) { sprintf(halo_id,"HALO_INTERP_UP") ; } | |||
else if ( down_path[ipath] == SMOOTH_UP ) { sprintf(halo_id,"HALO_INTERP_SMOOTH") ; } | |||
sprintf(halo_define,"80:") ; | |||
sprintf(halo_use,"") ; | |||
halo_use[0] = '\0' ; |
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.
sprintf
may be clearer, and the compiler may still notice the optimization.
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.
Or strcpy
, since this isn't using any of the formatting features.
tools/reg_parse.c
Outdated
@@ -256,7 +275,7 @@ pre_parse( char * dir, FILE * infile, FILE * outfile ) | |||
if ( !strcmp( tokens[F_USE] , tracers[i] ) ) found = 1 ; | |||
} | |||
if ( found == 0 ) { | |||
sprintf(tracers[ntracers],tokens[F_USE]) ; | |||
snprintf(tracers[ntracers], 100, tokens[F_USE]) ; |
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.
Definition on line 89; should this be another magic constant?
#else | ||
#endif | ||
# include <sys/time.h> | ||
# include <sys/resource.h> |
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.
I suspect these two headers are still unavailable on Windows; the others might be.
@@ -1,6 +1,12 @@ | |||
#ifndef REGISTRY_H | |||
#include <stdlib.h> | |||
#include <ctype.h> | |||
#include <sys/unistd.h> |
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 might also be unavailable on Windows.
@@ -157,7 +159,8 @@ main( int argc, char *argv[], char *env[] ) | |||
sprintf( fname_wrk,"%s/Registry_irr_diag",dir ) ; | |||
} | |||
// fprintf(stderr,"Registry tmp file = %s\n",fname_wrk); | |||
sprintf(command,"/bin/cp %s %s\n",fname_in,fname_wrk); | |||
/* we should be able to implement this using posix_spawn */ | |||
sprintf(command,"/bin/cp \'%s\' \'%s\'\n",fname_in,fname_wrk); |
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 will still break if the filename includes apostrophes, but I think that's unlikely enough to not try re-implementing Python's shlex.quote
.
@DWesl The compilation failed in our regression test. If you need to see the output, let me know. |
The regression test results from the last commit:
|
@islas Is this ready for approval? |
@weiwangncar I think @DWesl has a lot of self-marked revisions they are still going through. Is that correct? |
I completed all the simple work I noticed on a second passthrough. There's a few places where I would like feedback on style. |
Something that should be noted before the final merge: I accidently added a Cygwin workflow in GitHub actions; I'm not sure if the maintainers want to keep that in or out of the main repository. EDIT: I added the entire "Fix Cygwin compile" PR to this one. Should I rebase the branch to drop those changes? Or just edit them out and let the merge sort out differences? |
I still see some Cygwin changes to configure and the workflow. I'm unsure if that is something we will want to add, though having a test would be incredibly helpful. That content & discussion might be more suited to a Cygwin-specific PR |
There might be more I wanted to add, but I don't remember. I'll have to add those the way I did the first time, compiling with -Werror until it works. This pass is almost exclusively focused on the registry. I'll have a few more for Fortran once I figure out how. Inspiration: registry was segfaulting, so I tried fixing the warnings in case it was one of those. It didn't work, but it's worth doing regardless.
Change compile flags back to what they were before.
Guard io.h #include to avoid Linux compilation failures.
Given how much space this provides at the moment, this had better not run over.
Most likely the better solution is to look up functions to copy or delete files directly. I'm not sure where to look for those, but I'm pretty sure they're not in the C standard.
I pushed it to another branch, so a PR version can get some eyes on it.
external/RSL_LITE implements these as int, so they need to be declared with int returns, not void.
Not quite all of them yet, but there's only one other, that one without an existing named constant. EDIT: There's about seventy left: I'll have to go through those again.
I think there's a way to pass this as a parameter to snprintf instead, but I don't remember what that is at the moment.
I looked up how to do this. Apparently you just stick a star for the width and put the variable with the width before the variable you want to have that width.
grep -E -e '\[[[:digit:]]{2,}\]' *.[CcHhFf]*
1d09961
to
4ba1aab
Compare
Lots of unused variables, but I should be able to leave those for a bit. Unused parameters would be a pain, so I'm leaving those.
I split off the remaining Cygwin-specific fixes to DWesl#5. This should be just the bits that address warnings, with comments on the bits I thought might be controversial. |
I wanted to make it a short to save space in the strings based on this, but given the push to optimize for clarity that's less relevant.
@DWesl This PR has code conflicts with what is in the latest repository. Can you resolve these conflicts while preserving the latest change in the repository? |
There's a number of places that change |
Apparently that's #1942, which also attempted to fix warnings, in this case a warning that The other option would be to just delete the conditional, but the comment implies there's a point to it. I just asked on that PR for clarification of what I should do with those cases. |
…warnings-registry Fix merge conflicts.
I made nChmOpts a short int to keep buffer size down. Given the change to named constants, this is likely irrelevant. On the other hand, it may currently max out at five.
Discussion resolved there, conflicts resolved here: I kept the bit-mask then logical-not I already had. |
Should I try to squash the changes into fewer commits? |
@islas Can you see if this PR can be accepted? |
Quiet the compiler warnings in registry.exe
TYPE: bug fix
KEYWORDS: compiler warnings, snprintf, initial values, string length
SOURCE: "Daniel Wesloh (Pennsylvania State University Department of Meteorology and Atmospheric Science)"
DESCRIPTION OF CHANGES:
Problem:
Generally or specifically, what was wrong and needed to be addressed?
A few years ago,
registry.exe
was segfaulting while compiling WRF. I enabled every compiler warning for that build process I could find, and tried to fix every one I saw. The problem ended up being insufficient stack space (fix merged a few years ago), but fixing the warnings probably won't hurt.Solution:
What was down algorithmically and in the source code to address the problem?
short
instead ofint
for numbers in the 300--16,000 range, so printf knows that five characters will be enough)snprintf
and friendsISSUE: None
LIST OF MODIFIED FILES: list of changed files
TESTS CONDUCTED:
RELEASE NOTE: Eliminate some compiler warnings when building registry.exe