diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 43a26f71..2602a42f 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -4744,7 +4744,10 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname) } if( doswap ) { if ( g_opts.debug > 3 ) disp_nifti_1_header("-d ni1 pre-swap: ", &nhdr); - swap_nifti_header( &nhdr , ni_ver ) ; + /* nhdr is a nifti_1_header, so swap it as one. ni_ver comes from the + magic string, and a magic of "n+2" would otherwise have + swap_nifti_header() treat these 348 bytes as a 540 byte header. */ + swap_nifti_header( &nhdr , ni_ver ? 1 : 0 ) ; } if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr2nim : ", &nhdr); @@ -5367,7 +5370,9 @@ nifti_1_header * nifti_read_n1_hdr(const char * hname, int *swapped, int check) if( lswap ) { if ( g_opts.debug > 3 ) disp_nifti_1_header("-d nhdr pre-swap: ", &nhdr); - swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ) ; + /* only sizeof(nifti_1_header) bytes were read, so swap as that; see + the same guard in nifti_convert_n1hdr2nim() */ + swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ? 1 : 0 ) ; } if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr post-swap: ", &nhdr); diff --git a/nifti2/nifti_tool.c b/nifti2/nifti_tool.c index 7dfa3272..85bc3f54 100644 --- a/nifti2/nifti_tool.c +++ b/nifti2/nifti_tool.c @@ -3391,7 +3391,11 @@ int act_mod_hdrs( nt_opts * opts ) nifti_image_free(nim); } else if ( swap ) - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* nhdr is a nifti_1_header, so swap it as one. NIFTI_VERSION() + reads the magic string, and a magic of "n+2" would otherwise + have swap_nifti_header() treat these 348 bytes as a 540 byte + header. ni_ver 0 and 1 are both 348 byte layouts. */ + swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr) ? 1 : 0); /* if all is well, overwrite header in fname dataset */ (void)write_hdr_to_file(nhdr, fname); /* errors printed in function */ @@ -3512,7 +3516,9 @@ int act_mod_hdr2s( nt_opts * opts ) nifti_image_free(nim); } else if ( swap ) - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* nhdr is a nifti_2_header; use the explicit version rather than + the magic, which could claim "n+1" and swap 540 bytes as 348 */ + swap_nifti_header(nhdr, 2); /* if all is well, overwrite header in fname dataset */ (void)write_hdr2_to_file(nhdr, fname); /* errors printed in function */ @@ -3613,8 +3619,12 @@ int act_swap_hdrs( nt_opts * opts ) swap_nifti_header(nhdr, 0); /* undo ANALYZE */ swap_nifti_header(nhdr, 1); /* swap NIFTI */ } else if ( opts->swap_old ) { - /* undo whichever was done and apply the old way */ - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* undo whichever was done and apply the old way. As above, + nhdr is a nifti_1_header, so it must not be swapped as a + 540 byte NIFTI-2 header just because its magic says "n+2". + old_swap_nifti_header() takes a nifti_1_header and a + boolean, so it needs no such guard. */ + swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr) ? 1 : 0); old_swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); }