Package: coreutils
Version: 8.30-3+b1
Severity: normal
Dear Maintainer,
I was looking what I can do with valgrind, so I tested it with a simple
command: du.
It seem I have found two points in which allocated memory is not free.
For the first I have found a solution, but not for the second one (it is
a lot of time that I don't do C/C++ programming).
Those are the steps I have done:
1) installing debug symbols
# apt install coreutils-dbgsym
2) executing valgrind for memory leak
$ valgrind --leak-check=yes --leak-check=full --show-leak-kinds=all du -s .
==13844== Memcheck, a memory error detector
==13844== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13844== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==13844== Command: du -s .
==13844==
236735652 .
==13844==
==13844== HEAP SUMMARY:
==13844== in use at exit: 720 bytes in 2 blocks
==13844== total heap usage: 1,325 allocs, 1,323 frees, 638,869 bytes
allocated
==13844==
==13844== 16 bytes in 1 blocks are still reachable in loss record 1 of 2
==13844== at 0x483877F: malloc (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==13844== by 0x115558: xmalloc (xmalloc.c:41)
==13844== by 0x115708: xzalloc (xmalloc.c:86)
==13844== by 0x10B752: main (du.c:750)
==13844==
==13844== 704 bytes in 1 blocks are still reachable in loss record 2 of 2
==13844== at 0x483AB65: calloc (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==13844== by 0x11573E: xcalloc (xmalloc.c:101)
==13844== by 0x10C203: process_file (du.c:602)
==13844== by 0x10C203: du_files (du.c:708)
==13844== by 0x10C203: main (du.c:1122)
==13844==
==13844== LEAK SUMMARY:
==13844== definitely lost: 0 bytes in 0 blocks
==13844== indirectly lost: 0 bytes in 0 blocks
==13844== possibly lost: 0 bytes in 0 blocks
==13844== still reachable: 720 bytes in 2 blocks
==13844== suppressed: 0 bytes in 0 blocks
==13844==
==13844== For lists of detected and suppressed errors, rerun with: -s
==13844== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
3) installing du source
$ apt source coreutils
4) look what there are at the du error lines
$ cd coreutils-8.30/
$ head -n 750 src/du.c | tail -n 1
exclude = new_exclude ();
$ head -n 1122 src/du.c | tail -n 1
ok &= du_files (temp_argv, bit_flags);
5) analyze the 1st error: exclude = new_exclude ();
I see that in the exclude.h/exclude.c there is also a free_exclude()
that is not called
5.1) adding the missing line
$ sed '925 i free_exclude( exclude );' src/du.c > src/du1.c
$ mv src/du.c src/du.c.bak
$ mv src/du1.c src/du.c
5.2) compile
# apt build-dep coreutils
$ ./debian/rules build
5.3) test to see if the memory leak is already here
$ valgrind --leak-check=yes --leak-check=full --show-leak-kinds=all
/tmp/2/coreutils-8.30/src/du -s .
236735652 .
==62961==
==62961== HEAP SUMMARY:
==62961== in use at exit: 704 bytes in 1 blocks
==62961== total heap usage: 1,325 allocs, 1,324 frees, 638,869 bytes
allocated
==62961==
==62961== 704 bytes in 1 blocks are still reachable in loss record 1 of 1
==62961== at 0x483AB65: calloc (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==62961== by 0x118ADE: xcalloc (xmalloc.c:101)
==62961== by 0x10C47A: process_file (du.c:602)
==62961== by 0x10C47A: du_files (du.c:708)
==62961== by 0x10C47A: main (du.c:1123)
==62961==
==62961== LEAK SUMMARY:
==62961== definitely lost: 0 bytes in 0 blocks
==62961== indirectly lost: 0 bytes in 0 blocks
==62961== possibly lost: 0 bytes in 0 blocks
==62961== still reachable: 704 bytes in 1 blocks
==62961== suppressed: 0 bytes in 0 blocks
==62961==
==62961== For lists of detected and suppressed errors, rerun with: -s
==62961== ERROR SUMMARY: 1209 errors from 1 contexts (suppressed: 0 from 0)
6) analyze the 2nd error
$ head -n 1123 src/du.c | tail -n 1
ok &= du_files (temp_argv, bit_flags);
$ head -n 708 src/du.c | tail -n 1
ok &= process_file (fts, ent);
$ head -n 602 src/du.c | tail -n 1
dulvl = xcalloc (n_alloc, sizeof *dulvl);
6.1) I try to adding the missing free line, but it don't work, probably
for the simil cast used with xnrealloc
$ sed '668 i if( dulvl ) {free( dulvl );}' src/du.c > src/du1.c
$ mv src/du1.c src/du.c
6.2) compile
$ ./debian/rules clean
$ ./debian/rules build
6.3) test to see if the memory leak is already here, but I have a lot of
errors
$ valgrind --leak-check=yes --leak-check=full --show-leak-kinds=all
/tmp/2/coreutils-8.30/src/du -s .
==101211== Memcheck, a memory error detector
==101211== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==101211== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright
info
==101211== Command: /tmp/2/coreutils-8.30/src/du -s .
==101211==
==101211== Invalid read of size 8
==101211== at 0x10E82E: excluded_file_name (exclude.c:477)
==101211== by 0x10C109: process_file (du.c:517)
==101211== by 0x10C109: du_files (du.c:709)
==101211== by 0x10C109: main (du.c:1124)
==101211== Address 0x4a3a840 is 0 bytes inside a block of size 16 free'd
==101211== at 0x48399AB: free (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==101211== by 0x10BE0A: main (du.c:926)
==101211== Block was alloc'd at
==101211== at 0x483AB65: calloc (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==101211== by 0x118ABD: xmalloc (xmalloc.c:41)
==101211== by 0x118ABD: xzalloc (xmalloc.c:86)
==101211== by 0x10B9C3: main (du.c:751)
[...]
==101211== Invalid write of size 8
==101211== at 0x10C392: duinfo_add (du.c:113)
==101211== by 0x10C392: process_file (du.c:651)
==101211== by 0x10C392: du_files (du.c:709)
==101211== by 0x10C392: main (du.c:1124)
==101211== Address 0x4a48750 is 64 bytes inside a block of size 704 free'd
==101211== at 0x48399AB: free (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==101211== by 0x10C43F: process_file (du.c:668)
==101211== by 0x10C43F: du_files (du.c:709)
==101211== by 0x10C43F: main (du.c:1124)
==101211== Block was alloc'd at
==101211== at 0x483AB65: calloc (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==101211== by 0x118AEE: xcalloc (xmalloc.c:101)
==101211== by 0x10C53B: process_file (du.c:602)
==101211== by 0x10C53B: du_files (du.c:709)
==101211== by 0x10C53B: main (du.c:1124)
[...]
==101211== Invalid free() / delete / delete[] / realloc()
==101211== at 0x48399AB: free (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==101211== by 0x10C43F: process_file (du.c:668)
==101211== by 0x10C43F: du_files (du.c:709)
==101211== by 0x10C43F: main (du.c:1124)
==101211== Address 0x4a48710 is 0 bytes inside a block of size 704 free'd
==101211== at 0x48399AB: free (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==101211== by 0x10C43F: process_file (du.c:668)
==101211== by 0x10C43F: du_files (du.c:709)
==101211== by 0x10C43F: main (du.c:1124)
==101211== Block was alloc'd at
==101211== at 0x483AB65: calloc (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==101211== by 0x118AEE: xcalloc (xmalloc.c:101)
==101211== by 0x10C53B: process_file (du.c:602)
==101211== by 0x10C53B: du_files (du.c:709)
==101211== by 0x10C53B: main (du.c:1124)
[...]
==101211== HEAP SUMMARY:
==101211== in use at exit: 0 bytes in 0 blocks
==101211== total heap usage: 3,880 allocs, 7,485 frees, 3,485,123
bytes allocated
==101211==
==101211== All heap blocks were freed -- no leaks are possible
==101211==
==101211== For lists of detected and suppressed errors, rerun with: -s
==101211== ERROR SUMMARY: 28021 errors from 46 contexts (suppressed: 0
from 0)
Ciao
Davide
-- System Information:
Debian Release: bullseye/sid
APT prefers testing-debug
APT policy: (500, 'testing-debug'), (500, 'testing'), (500, 'stable')
Architecture: amd64 (x86_64)
Kernel: Linux 5.7.17-dp-20200831 (SMP w/4 CPU threads; PREEMPT)
Kernel taint flags: TAINT_UNSIGNED_MODULE
Locale: LANG=it_IT.utf8, LC_CTYPE=it_IT.utf8 (charmap=UTF-8), LANGUAGE
not set
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled
Versions of packages coreutils depends on:
ii libacl1 2.2.53-8
ii libattr1 1:2.4.48-5
ii libc6 2.31-3
ii libselinux1 3.1-2
coreutils recommends no packages.
coreutils suggests no packages.
-- debconf-show failed