Bug 105427

Summary: Memory leak in lib: core.c
Product: libfprint Reporter: Mark Harfouche <mark.harfouche>
Component: libfprintAssignee: libfprint-bugs
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: medium CC: bugzilla
Version: unspecified   
Hardware: Other   
OS: All   
Whiteboard:
i915 platform: i915 features:
Attachments: memory patch leka
Memory leak patch in lib
Memory leak patch in lib
lib: Fix memory leak patch in device discovery
examples: Fix memory leaked by device discovery

Description Mark Harfouche 2018-03-10 09:14:40 UTC
Created attachment 137959 [details] [review]
memory patch leka

I ran img_capture with valgrind to find memory leaks in my driver.

I ended up finding one in core.

Here is the patch for it.
Comment 1 Mark Harfouche 2018-03-10 09:25:30 UTC
Note, these changes are included in #99462
https://bugs.freedesktop.org/show_bug.cgi?id=99462

So if you end up merging 99462 (which I think you should), then this isn't necessary.
Comment 2 Bastien Nocera 2018-03-10 17:13:56 UTC
Comment on attachment 137959 [details] [review]
memory patch leka

Review of attachment 137959 [details] [review]:
-----------------------------------------------------------------

> memory leak

Please prefix with "lib: " and explain where the memory leak happens, and what is being leaked.

::: libfprint/core.c
@@ +564,4 @@
>  		struct fp_dscv_dev *ddev = discover_dev(udev);
>  		if (!ddev)
>  			continue;
> +    libusb_ref_device(udev);

Please fix the indentation to match the existing one.
Comment 3 Mark Harfouche 2018-03-10 18:04:45 UTC
Created attachment 137968 [details] [review]
Memory leak patch in lib

Fixed the indentation to follow the coding style
Comment 4 Mark Harfouche 2018-03-10 18:06:31 UTC
Comment on attachment 137968 [details] [review]
Memory leak patch in lib

Incomplete patch
Comment 5 Mark Harfouche 2018-03-10 18:14:42 UTC
Created attachment 137969 [details] [review]
Memory leak patch in lib

the library needs to call 	

libusb_free_device_list(devs, 1);

after 

libusb_get_device_list

To do so safely, I think you have to increment the reference to the device you want to use first, then just decrement the reference too the whole list.

In finding this bug, I also found a small bug in `img_capture` but that one isn't as important since it isn't part of the library.
Comment 6 Bastien Nocera 2018-03-11 12:45:40 UTC
Created attachment 137986 [details] [review]
lib: Fix memory leak patch in device discovery

libusb_free_device_list() needs to be called on the list of USB devices
obtained through libusb_get_device_list() or the list and its elements
will be leaked.
Comment 7 Bastien Nocera 2018-03-11 12:46:17 UTC
Created attachment 137988 [details] [review]
examples: Fix memory leaked by device discovery
Comment 8 Bastien Nocera 2018-03-11 12:48:10 UTC
Comment on attachment 137969 [details] [review]
Memory leak patch in lib

In the future, please try to provide git-formatted patches, and remove white space changes using "git checkout -p" before committing. The patches should also be split into individual cohesive commits as much as possible.

I've done all this for the 2 changes you provided.
Comment 9 Bastien Nocera 2018-03-11 12:52:27 UTC
Attachment 137986 [details] pushed as 58ba9b0 - lib: Fix memory leak patch in device discovery
Attachment 137988 [details] pushed as 7ff667f - examples: Fix memory leaked by device discovery

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.