[sane-devel] Memory leak in backend/dll.c load()

Ariel S gsenoras at gmail.com
Sat Jan 4 03:41:58 UTC 2014


Hello, I found a small leak in SANE backend/dll.c, this is what I get
from valgrind:

        $ valgrind --leak-check=full scanimage -L
        ==19768== Memcheck, a memory error detector
        ==19768== Copyright (C) 2002-2013, and GNU GPL'd, by Julian
Seward et al.
        ==19768== Using Valgrind-3.9.0 and LibVEX; rerun with -h for
copyright info
        ==19768== Command: scanimage -L
        ==19768==
        ==19768== Warning: invalid file descriptor -1 in syscall close()

        No scanners were identified. If you were expecting something different,
        check that the scanner is plugged in, turned on and detected by the
        sane-find-scanner tool (if appropriate). Please read the documentation
        which came with this software (README, FAQ, manpages).
        ==19768==
        ==19768== HEAP SUMMARY:
        ==19768==     in use at exit: 289,018 bytes in 1,256 blocks
        ==19768==   total heap usage: 3,614 allocs, 2,358 frees,
694,460 bytes allocated
        ==19768==
        ==19768== 14 bytes in 1 blocks are definitely lost in loss
record 12 of 143
        ==19768==    at 0x4C27730: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
        ==19768==    by 0x50E1709: strdup (in /usr/lib/libc-2.18.so)
        ==19768==    by 0x4E4DC5B: load (dll.c:446)
        ==19768==    by 0x4E4DCB3: init (dll.c:608)
        ==19768==    by 0x4E4E5ED: sane_dll_get_devices (dll.c:1056)
        ==19768==    by 0x401BF1: main (scanimage.c:1809)
        ==19768==
        ==19768== LEAK SUMMARY:
        ==19768==    definitely lost: 14 bytes in 1 blocks
        ==19768==    indirectly lost: 0 bytes in 0 blocks
        ==19768==      possibly lost: 0 bytes in 0 blocks
        ==19768==    still reachable: 289,004 bytes in 1,255 blocks
        ==19768==         suppressed: 0 bytes in 0 blocks
        ==19768== Reachable blocks (those to which a pointer was
found) are not shown.
        ==19768== To see them, rerun with: --leak-check=full
--show-leak-kinds=all
        ==19768==
        ==19768== For counts of detected and suppressed errors, rerun with: -v
        ==19768== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

I look into the source code, and I wrote a one-line-patch to fix this:



diff --git a/backend/dll.c b/backend/dll.c
index 619ee55..70ddb41 100644
--- a/backend/dll.c
+++ b/backend/dll.c
@@ -449,6 +449,7 @@ load (struct backend *be)
          DBG (1, "load: strdup failed: %s\n", strerror (errno));
          return SANE_STATUS_NO_MEM;
        }
+      orig_src = src;
     }
   DBG (3, "load: searching backend `%s' in `%s'\n", be->name, src);



This change will make 'src', which implicitly malloc'd by strdup, to be
freed.

After:

        $ valgrind --leak-check=full scanimage -L
        ==26409== Memcheck, a memory error detector
        ==26409== Copyright (C) 2002-2013, and GNU GPL'd, by Julian
Seward et al.
        ==26409== Using Valgrind-3.9.0 and LibVEX; rerun with -h for
copyright info
        ==26409== Command: scanimage -L
        ==26409==
        ==26409== Warning: invalid file descriptor -1 in syscall close()

        No scanners were identified. If you were expecting something different,
        check that the scanner is plugged in, turned on and detected by the
        sane-find-scanner tool (if appropriate). Please read the documentation
        which came with this software (README, FAQ, manpages).
        ==26409==
        ==26409== HEAP SUMMARY:
        ==26409==     in use at exit: 289,004 bytes in 1,255 blocks
        ==26409==   total heap usage: 3,614 allocs, 2,359 frees,
694,460 bytes allocated
        ==26409==
        ==26409== LEAK SUMMARY:
        ==26409==    definitely lost: 0 bytes in 0 blocks
        ==26409==    indirectly lost: 0 bytes in 0 blocks
        ==26409==      possibly lost: 0 bytes in 0 blocks
        ==26409==    still reachable: 289,004 bytes in 1,255 blocks
        ==26409==         suppressed: 0 bytes in 0 blocks
        ==26409== Reachable blocks (those to which a pointer was
found) are not shown.
        ==26409== To see them, rerun with: --leak-check=full
--show-leak-kinds=all
        ==26409==
        ==26409== For counts of detected and suppressed errors, rerun with: -v
        ==26409== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)


According to git log the lines around my finding came from 2002-04-21
(ie. revision 41efdd), which is 12 years ago. Am I the only one having this
leak?

Thank you.



More information about the sane-devel mailing list