From fca13e87208e010e387a30f659a0ab0f4d146aa5 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 14 Aug 2026 23:08:32 -0400 Subject: [PATCH] BUG: Fix signed/unsigned comparisons in the nifti tools Three -Wsign-compare warnings, each comparing a signed value against an unsigned one, where the signed operand is silently converted and a negative value would compare as enormous. nifti1_tool.c, nifti_tool.c, fill_cmd_string() `len < 0 || len >= remain`, len an int, remain a size_t. The `len < 0` test short-circuits first, so the conversion could not actually misfire, but the comparison relies on that ordering to be correct. Made explicit, matching the idiom used a few lines above. nifti_tool.c, read_file_text() `bytes != len64`, size_t against int64_t. len64 is validated as > 0 and <= INT_MAX immediately above, so the cast is lossless. (cherry picked from commit 7b37e321171234b12f8b993d5d5d7c3f83d81977) --- nifti2/nifti_tool.c | 4 ++-- niftilib/nifti1_tool.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nifti2/nifti_tool.c b/nifti2/nifti_tool.c index ac249bf8..3512b4ba 100644 --- a/nifti2/nifti_tool.c +++ b/nifti2/nifti_tool.c @@ -885,7 +885,7 @@ int fill_cmd_string( nt_opts * opts, int argc, const char * argv[]) if( has_space ) len = snprintf(cp, remain, " '%s'", argv[ac]); else len = snprintf(cp, remain, " %s", argv[ac]); - if( len < 0 || len >= remain ) { + if( len < 0 || (size_t)len >= remain ) { fprintf(stderr,"FCS: error parsing command, continuing...\n"); return 1; } @@ -2367,7 +2367,7 @@ static char * read_file_text(const char * filename, int * length) bytes = fread(text, sizeof(char), len64, fp); fclose(fp); /* in any case */ - if( bytes != len64 ) { + if( bytes != (size_t)len64 ) { fprintf(stderr,"** RFT: read only %zu of %" PRId64 " bytes from %s\n", bytes, len64, filename); free(text); diff --git a/niftilib/nifti1_tool.c b/niftilib/nifti1_tool.c index 15bb165a..f2efdc4e 100644 --- a/niftilib/nifti1_tool.c +++ b/niftilib/nifti1_tool.c @@ -723,7 +723,7 @@ int fill_cmd_string( nt_opts * opts, int argc, const char * argv[]) if( has_space ) len = snprintf(cp, remain, " '%s'", argv[ac]); else len = snprintf(cp, remain, " %s", argv[ac]); - if( len < 0 || len >= remain ) { + if( len < 0 || (size_t)len >= remain ) { fprintf(stderr,"FCS: error parsing command, continuing...\n"); return 1; }