From 0d825629c8c722dc7d9ede5ccd73ece2b67d2130 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 14 Aug 2026 23:25:39 -0400 Subject: [PATCH 1/2] BUG: Give the statement macros a do/while(0) body A macro whose body is a bare { ... } block cannot be used as a statement: if( cond ) FSLIOERR(...); /* the ; ends the if, the block runs */ else /* ...and this is a syntax error */ No current call site is written that way -- FSLIOERR has no `else` after it anywhere, and none of the nifti_*_test macros appears in an if or else arm -- so the change is prophylactic and nothing observable moves. The next person to write one, though, gets a syntax error a long way from the cause. clang reports the call sites as -Wextra-semi-stmt, because the trailing semicolon at each is an empty statement. Wrapped: FSLIOERR in fslio.c and the seven nifti_*_test macros in nifti_tester001.c. The NT_FILL and NT_MAT* families in the tool headers already have do/while bodies. Two genuine empty statements go too: a doubled semicolon inside unescape_string() in both io files, and a stray semicolon after the closing brace of an if block in FslClose(). FSLIOERR is worth calling out: it expands to fprintf plus exit(EXIT_FAILURE), and of its roughly fifty call sites several are the whole body of a braceless if. Those are correct today only because the macro always exits. --- fsliolib/fslio.c | 4 ++-- nifti2/nifti2_io.c | 2 +- niftilib/nifti1_io.c | 2 +- niftilib/nifti_tester001.c | 28 ++++++++++++++-------------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/fsliolib/fslio.c b/fsliolib/fslio.c index 44053fcf..27af6839 100644 --- a/fsliolib/fslio.c +++ b/fsliolib/fslio.c @@ -43,7 +43,7 @@ static int check_for_multiple_filenames(const char* filename); static int FslIgnoreMFQ=0; static int FslOverrideOutputType=-1; -#define FSLIOERR(x) { fprintf(stderr,"Error:: %s\n",(x)); fflush(stderr); exit(EXIT_FAILURE); } +#define FSLIOERR(x) do { fprintf(stderr,"Error:: %s\n",(x)); fflush(stderr); exit(EXIT_FAILURE); } while(0) /************************************************************ @@ -1992,7 +1992,7 @@ int FslClose(FSLIO *fslio) fprintf(stderr,"Error:: Could not write origin data to header file %s.\n", fslio->niftiptr->fname); return -1; - }; + } znzwrite(hdr,1,sizeof(struct dsr),hptr); znzclose(hptr); diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 70c136e8..2b943f2a 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -8412,7 +8412,7 @@ static int unescape_string( char *str ) } else if( str[ii] == CR ) { /* is a carriage return */ if( str[ii+1] == LF ){ str[jj] = LF ; ii++ ; nn++ ; } /* CR LF */ - else { str[jj] = LF ; ; nn++ ; } /* CR only */ + else { str[jj] = LF ; nn++ ; } /* CR only */ } else { /* is a normal character, just copy to output */ diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index c9142554..2f17e91f 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -6276,7 +6276,7 @@ static int unescape_string( char *str ) } else if( str[ii] == CR ) { /* is a carriage return */ if( str[ii+1] == LF ){ str[jj] = LF ; ii++ ; nn++ ; } /* CR LF */ - else { str[jj] = LF ; ; nn++ ; } /* CR only */ + else { str[jj] = LF ; nn++ ; } /* CR only */ } else { /* is a normal character, just copy to output */ diff --git a/niftilib/nifti_tester001.c b/niftilib/nifti_tester001.c index 126e6bd6..77c61156 100644 --- a/niftilib/nifti_tester001.c +++ b/niftilib/nifti_tester001.c @@ -460,7 +460,7 @@ int main (int argc, const char *argv[]) * call nifti_datatype_string with all possible values */ #define nifti_datatype_test(constant,string) \ - { \ + do { \ char buf[64]; \ snprintf(buf,sizeof(buf),"nifti_datatype_string %s",string); \ PrintTest( \ @@ -468,7 +468,7 @@ int main (int argc, const char *argv[]) strcmp(nifti_datatype_string(constant),string) != 0, \ true, \ &Errors); \ - } + } while(0) nifti_datatype_test(DT_UNKNOWN,"UNKNOWN"); nifti_datatype_test(DT_BINARY, "BINARY"); nifti_datatype_test(DT_INT8, "INT8"); @@ -487,7 +487,7 @@ int main (int argc, const char *argv[]) nifti_datatype_test(DT_COMPLEX256, "COMPLEX256"); nifti_datatype_test(DT_RGB24, "RGB24"); #define nifti_is_inttype_test(constant,rval) \ - { \ + do { \ char buf[64]; \ snprintf(buf,sizeof(buf),"nifti_datatype_string %d",constant); \ PrintTest( \ @@ -495,7 +495,7 @@ int main (int argc, const char *argv[]) nifti_is_inttype(constant) != (rval), \ true, \ &Errors); \ - } + } while(0) nifti_is_inttype_test(DT_UNKNOWN,0); nifti_is_inttype_test(DT_BINARY,0); nifti_is_inttype_test(DT_INT8,1); @@ -514,7 +514,7 @@ int main (int argc, const char *argv[]) nifti_is_inttype_test(DT_COMPLEX256,0); nifti_is_inttype_test(DT_RGB24,1); #define nifti_units_string_test(constant,string) \ - { \ + do { \ char buf[64]; \ snprintf(buf,sizeof(buf),"nifti_units_string_test %s",string); \ PrintTest( \ @@ -522,7 +522,7 @@ int main (int argc, const char *argv[]) strcmp(nifti_units_string(constant),string) != 0, \ true, \ &Errors); \ - } + } while(0) nifti_units_string_test(NIFTI_UNITS_METER,"m"); nifti_units_string_test(NIFTI_UNITS_MM,"mm"); nifti_units_string_test(NIFTI_UNITS_MICRON,"um"); @@ -533,7 +533,7 @@ int main (int argc, const char *argv[]) nifti_units_string_test(NIFTI_UNITS_PPM,"ppm"); nifti_units_string_test(NIFTI_UNITS_RADS,"rad/s"); #define nifti_intent_string_test(constant,string) \ - { \ + do { \ char buf[64]; \ snprintf(buf,sizeof(buf),"nifti_intent_string %s",string); \ PrintTest( \ @@ -541,7 +541,7 @@ int main (int argc, const char *argv[]) strcmp(nifti_intent_string(constant),string) != 0, \ true, \ &Errors); \ - } + } while(0) nifti_intent_string_test(NIFTI_INTENT_CORREL,"Correlation statistic"); nifti_intent_string_test(NIFTI_INTENT_TTEST,"T-statistic"); nifti_intent_string_test(NIFTI_INTENT_FTEST,"F-statistic"); @@ -579,7 +579,7 @@ int main (int argc, const char *argv[]) nifti_intent_string_test(-200,"Unknown"); #define nifti_slice_string_test(constant,string) \ - { \ + do { \ char buf[64]; \ snprintf(buf,sizeof(buf),"nifti_slice_string_test %s",string); \ PrintTest( \ @@ -587,7 +587,7 @@ int main (int argc, const char *argv[]) strcmp(nifti_slice_string(constant),string) != 0, \ true, \ &Errors); \ - } + } while(0) nifti_slice_string_test(NIFTI_SLICE_SEQ_INC,"sequential_increasing"); nifti_slice_string_test(NIFTI_SLICE_SEQ_DEC,"sequential_decreasing"); nifti_slice_string_test(NIFTI_SLICE_ALT_INC,"alternating_increasing"); @@ -595,7 +595,7 @@ int main (int argc, const char *argv[]) nifti_slice_string_test(NIFTI_SLICE_ALT_INC2,"alternating_increasing_2"); nifti_slice_string_test(NIFTI_SLICE_ALT_DEC2,"alternating_decreasing_2"); #define nifti_orientation_string_test(constant,string) \ - { \ + do { \ char buf[64]; \ snprintf(buf,sizeof(buf),"nifti_orientation_string_test %s",string); \ PrintTest( \ @@ -603,7 +603,7 @@ int main (int argc, const char *argv[]) strcmp(nifti_orientation_string(constant),string) != 0, \ true, \ &Errors); \ - } + } while(0) nifti_orientation_string_test(NIFTI_L2R,"Left-to-Right"); nifti_orientation_string_test(NIFTI_R2L,"Right-to-Left"); nifti_orientation_string_test(NIFTI_P2A,"Posterior-to-Anterior"); @@ -612,7 +612,7 @@ int main (int argc, const char *argv[]) nifti_orientation_string_test(NIFTI_S2I,"Superior-to-Inferior"); #define nifti_datatype_sizes_test(constant,Nbyper,Swapsize) \ - { \ + do { \ int nbyper; \ int swapsize; \ char buf[64]; \ @@ -623,7 +623,7 @@ int main (int argc, const char *argv[]) nbyper != (Nbyper) || swapsize != (Swapsize), \ true, \ &Errors); \ - } + } while(0) nifti_datatype_sizes_test(DT_UINT8,1,0); nifti_datatype_sizes_test(DT_UINT16,2,2); From a91aa16b61e134182004b5ffc1c049e104359c2b Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 12:58:20 -0500 Subject: [PATCH 2/2] STYLE: Align the continuations in the macros this branch rewrites The backslashes sat at four different columns within a single macro, one of them past 80. Each macro is now aligned to its own width, one space past its longest line, so a block stays as narrow as its content allows rather than being widened by one long line. Only the seven macros the preceding commit gives a do/while(0) body are touched. --- niftilib/nifti_tester001.c | 132 ++++++++++++++++++------------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/niftilib/nifti_tester001.c b/niftilib/nifti_tester001.c index 77c61156..ed86a87c 100644 --- a/niftilib/nifti_tester001.c +++ b/niftilib/nifti_tester001.c @@ -459,15 +459,15 @@ int main (int argc, const char *argv[]) /* * call nifti_datatype_string with all possible values */ -#define nifti_datatype_test(constant,string) \ - do { \ - char buf[64]; \ - snprintf(buf,sizeof(buf),"nifti_datatype_string %s",string); \ - PrintTest( \ - buf, \ - strcmp(nifti_datatype_string(constant),string) != 0, \ - true, \ - &Errors); \ +#define nifti_datatype_test(constant,string) \ + do { \ + char buf[64]; \ + snprintf(buf,sizeof(buf),"nifti_datatype_string %s",string); \ + PrintTest( \ + buf, \ + strcmp(nifti_datatype_string(constant),string) != 0, \ + true, \ + &Errors); \ } while(0) nifti_datatype_test(DT_UNKNOWN,"UNKNOWN"); nifti_datatype_test(DT_BINARY, "BINARY"); @@ -486,15 +486,15 @@ int main (int argc, const char *argv[]) nifti_datatype_test(DT_COMPLEX128, "COMPLEX128"); nifti_datatype_test(DT_COMPLEX256, "COMPLEX256"); nifti_datatype_test(DT_RGB24, "RGB24"); -#define nifti_is_inttype_test(constant,rval) \ - do { \ - char buf[64]; \ - snprintf(buf,sizeof(buf),"nifti_datatype_string %d",constant); \ - PrintTest( \ - buf, \ - nifti_is_inttype(constant) != (rval), \ - true, \ - &Errors); \ +#define nifti_is_inttype_test(constant,rval) \ + do { \ + char buf[64]; \ + snprintf(buf,sizeof(buf),"nifti_datatype_string %d",constant); \ + PrintTest( \ + buf, \ + nifti_is_inttype(constant) != (rval), \ + true, \ + &Errors); \ } while(0) nifti_is_inttype_test(DT_UNKNOWN,0); nifti_is_inttype_test(DT_BINARY,0); @@ -513,15 +513,15 @@ int main (int argc, const char *argv[]) nifti_is_inttype_test(DT_COMPLEX128,0); nifti_is_inttype_test(DT_COMPLEX256,0); nifti_is_inttype_test(DT_RGB24,1); -#define nifti_units_string_test(constant,string) \ - do { \ - char buf[64]; \ - snprintf(buf,sizeof(buf),"nifti_units_string_test %s",string); \ - PrintTest( \ - buf, \ - strcmp(nifti_units_string(constant),string) != 0, \ - true, \ - &Errors); \ +#define nifti_units_string_test(constant,string) \ + do { \ + char buf[64]; \ + snprintf(buf,sizeof(buf),"nifti_units_string_test %s",string); \ + PrintTest( \ + buf, \ + strcmp(nifti_units_string(constant),string) != 0, \ + true, \ + &Errors); \ } while(0) nifti_units_string_test(NIFTI_UNITS_METER,"m"); nifti_units_string_test(NIFTI_UNITS_MM,"mm"); @@ -532,15 +532,15 @@ int main (int argc, const char *argv[]) nifti_units_string_test(NIFTI_UNITS_HZ,"Hz"); nifti_units_string_test(NIFTI_UNITS_PPM,"ppm"); nifti_units_string_test(NIFTI_UNITS_RADS,"rad/s"); -#define nifti_intent_string_test(constant,string) \ - do { \ - char buf[64]; \ - snprintf(buf,sizeof(buf),"nifti_intent_string %s",string); \ - PrintTest( \ - buf, \ - strcmp(nifti_intent_string(constant),string) != 0, \ - true, \ - &Errors); \ +#define nifti_intent_string_test(constant,string) \ + do { \ + char buf[64]; \ + snprintf(buf,sizeof(buf),"nifti_intent_string %s",string); \ + PrintTest( \ + buf, \ + strcmp(nifti_intent_string(constant),string) != 0, \ + true, \ + &Errors); \ } while(0) nifti_intent_string_test(NIFTI_INTENT_CORREL,"Correlation statistic"); nifti_intent_string_test(NIFTI_INTENT_TTEST,"T-statistic"); @@ -578,15 +578,15 @@ int main (int argc, const char *argv[]) nifti_intent_string_test(NIFTI_INTENT_DIMLESS,"Dimensionless number"); nifti_intent_string_test(-200,"Unknown"); -#define nifti_slice_string_test(constant,string) \ - do { \ - char buf[64]; \ - snprintf(buf,sizeof(buf),"nifti_slice_string_test %s",string); \ - PrintTest( \ - buf, \ - strcmp(nifti_slice_string(constant),string) != 0, \ - true, \ - &Errors); \ +#define nifti_slice_string_test(constant,string) \ + do { \ + char buf[64]; \ + snprintf(buf,sizeof(buf),"nifti_slice_string_test %s",string); \ + PrintTest( \ + buf, \ + strcmp(nifti_slice_string(constant),string) != 0, \ + true, \ + &Errors); \ } while(0) nifti_slice_string_test(NIFTI_SLICE_SEQ_INC,"sequential_increasing"); nifti_slice_string_test(NIFTI_SLICE_SEQ_DEC,"sequential_decreasing"); @@ -594,15 +594,15 @@ int main (int argc, const char *argv[]) nifti_slice_string_test(NIFTI_SLICE_ALT_DEC,"alternating_decreasing"); nifti_slice_string_test(NIFTI_SLICE_ALT_INC2,"alternating_increasing_2"); nifti_slice_string_test(NIFTI_SLICE_ALT_DEC2,"alternating_decreasing_2"); -#define nifti_orientation_string_test(constant,string) \ - do { \ - char buf[64]; \ - snprintf(buf,sizeof(buf),"nifti_orientation_string_test %s",string); \ - PrintTest( \ - buf, \ - strcmp(nifti_orientation_string(constant),string) != 0, \ - true, \ - &Errors); \ +#define nifti_orientation_string_test(constant,string) \ + do { \ + char buf[64]; \ + snprintf(buf,sizeof(buf),"nifti_orientation_string_test %s",string); \ + PrintTest( \ + buf, \ + strcmp(nifti_orientation_string(constant),string) != 0, \ + true, \ + &Errors); \ } while(0) nifti_orientation_string_test(NIFTI_L2R,"Left-to-Right"); nifti_orientation_string_test(NIFTI_R2L,"Right-to-Left"); @@ -611,18 +611,18 @@ int main (int argc, const char *argv[]) nifti_orientation_string_test(NIFTI_I2S,"Inferior-to-Superior"); nifti_orientation_string_test(NIFTI_S2I,"Superior-to-Inferior"); -#define nifti_datatype_sizes_test(constant,Nbyper,Swapsize) \ - do { \ - int nbyper; \ - int swapsize; \ - char buf[64]; \ - snprintf(buf,sizeof(buf),"nifti_datatype_sizes_test %d",constant); \ - nifti_datatype_sizes(constant,&nbyper,&swapsize); \ - PrintTest( \ - buf, \ - nbyper != (Nbyper) || swapsize != (Swapsize), \ - true, \ - &Errors); \ +#define nifti_datatype_sizes_test(constant,Nbyper,Swapsize) \ + do { \ + int nbyper; \ + int swapsize; \ + char buf[64]; \ + snprintf(buf,sizeof(buf),"nifti_datatype_sizes_test %d",constant); \ + nifti_datatype_sizes(constant,&nbyper,&swapsize); \ + PrintTest( \ + buf, \ + nbyper != (Nbyper) || swapsize != (Swapsize), \ + true, \ + &Errors); \ } while(0) nifti_datatype_sizes_test(DT_UINT8,1,0);