Skip to content
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

Memory leaks in nifti_image_from_ascii #154

Open
zer1t0 opened this issue Aug 3, 2022 · 0 comments
Open

Memory leaks in nifti_image_from_ascii #154

zer1t0 opened this issue Aug 3, 2022 · 0 comments

Comments

@zer1t0
Copy link

zer1t0 commented Aug 3, 2022

There is (at least) two memory leaks in nifti_image_from_ascii. The nim->iname member will be overwritten, with a new allocated string, as many times as the image_filename attribute is repeated in the file. Thus, the previous iname reference will be lost. Same happens with fname and header_filename.

Here are the affected lines:

nim->fname = nifti_strdup(rhs) ;

nim->iname = nifti_strdup(rhs) ;

Here is the test with the included nifti1_test and the backtrace produced by ASAN:

$ ./nifti1_test /tmp/test-many-image-filename.nii 
** ERROR (nifti_read_ascii_image): failed nifti_image_from_ascii() '/tmp/test-many-image-filename.nii'

=================================================================
==21838==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 22 byte(s) in 1 object(s) allocated from:
    #0 0x493b2d in malloc (/home/user/investigations/nifti/build-nifti_clib-libfuzzer/niftilib/nifti1_test+0x493b2d)
    #1 0x4d2161 in nifti_strdup /home/user/nifti/nifti_clib/niftilib/nifti1_io.c:1174:17
    #2 0x4faf7d in nifti_image_from_ascii /home/user/nifti/nifti_clib/niftilib/nifti1_io.c:6568:21
    #3 0x4f7359 in nifti_read_ascii_image /home/user/nifti/nifti_clib/niftilib/nifti1_io.c:4348:10
    #4 0x4c51f0 in nifti_image_read /home/user/nifti/nifti_clib/niftilib/nifti1_io.c:4223:13
    #5 0x4c3969 in main /home/user/nifti/nifti_clib/niftilib/nifti1_test.c:72:10
    #6 0x7fa416a2f082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16

SUMMARY: AddressSanitizer: 22 byte(s) leaked in 1 allocation(s).

Here are the samples to reproduce the behaviour:
tests.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant