Skip to content

Commit

Permalink
Fixed clang-tidy clang-analyzer-optin.portability.UnixAPI warnings
Browse files Browse the repository at this point in the history
There were indeed code paths where 0 is passed to the calloc computation:

calloc(sizeof(char),slen+1) ;

slen could be -1.  Then -1 + 1 = 0, and passing zero size to calloc is implementation-defined.
  • Loading branch information
seanm authored and hjmjohnson committed Dec 20, 2024
1 parent d84af56 commit 04ac6b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions nifti2/nifti2_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -6107,6 +6107,11 @@ nifti_image * nifti_read_ascii_image(znzFile fp, const char *fname, int flen,
slen = flen; /* slen will be our buffer length */
if( slen <= 0 ) slen = nifti_get_filesize(fname);

if( slen < 0 ){
LNI_FERR(lfunc,"nifti_get_filesize failed giving %" PRId64, fname);
return NULL;
}

if( g_opts.debug > 1 )
fprintf(stderr,"-d %s: have ASCII NIFTI file of size %" PRId64 "\n",
fname, slen);
Expand Down
6 changes: 6 additions & 0 deletions niftilib/nifti1_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -4348,6 +4348,12 @@ nifti_image * nifti_read_ascii_image(znzFile fp, char *fname, int flen,
fname);
return NULL;
}

if( flen < 0 ){
LNI_FERR(lfunc,"negative length not allowed", fname);
return NULL;
}

slen = flen; /* slen will be our buffer length */

if( g_opts.debug > 1 )
Expand Down

0 comments on commit 04ac6b1

Please sign in to comment.