mirror of
https://github.com/nfc-tools/libnfc.git
synced 2026-03-14 03:23:48 +00:00
Fix mem leak with libusb by introducing buses/usbbus.c
Now call only once usb_init(); usb_find_busses(); usb_find_devices() instead of multiple calls in several scan() then open() This fixes the following leaks: ==1159== 8 bytes in 1 blocks are definitely lost in loss record 9 of 102 ==1159== at 0x4C28BED: malloc (vg_replace_malloc.c:263) ==1159== by 0x53D9931: usb_parse_configuration (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x53DB8B1: usb_os_find_devices (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x53D8FDC: usb_find_devices (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x4E41D79: pn53x_usb_scan (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==1159== ==1159== 8 bytes in 1 blocks are definitely lost in loss record 10 of 102 ==1159== at 0x4C28BED: malloc (vg_replace_malloc.c:263) ==1159== by 0x53D9931: usb_parse_configuration (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x53DB8B1: usb_os_find_devices (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x53D8FDC: usb_find_devices (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x4E42CC7: pn53x_usb_open (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==1159== by 0x4E351E6: nfc_open (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0)
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
AM_CPPFLAGS = $(all_includes) $(LIBNFC_CFLAGS)
|
||||
|
||||
noinst_LTLIBRARIES = libnfcbuses.la
|
||||
libnfcbuses_la_SOURCES = uart.c uart.h
|
||||
libnfcbuses_la_SOURCES = uart.c uart.h usbbus.c usbbus.h
|
||||
libnfcbuses_la_CFLAGS = -I$(top_srcdir)/libnfc
|
||||
|
||||
EXTRA_DIST = uart_posix.c uart_win32.c
|
||||
|
||||
61
libnfc/buses/usbbus.c
Normal file
61
libnfc/buses/usbbus.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/*-
|
||||
* Public platform independent Near Field Communication (NFC) library
|
||||
*
|
||||
* Copyright (C) 2013, Romuald Conty
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file usbbus.c
|
||||
* @brief libusb 0.1 driver wrapper
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif // HAVE_CONFIG_H
|
||||
|
||||
#include "usbbus.h"
|
||||
#include "log.h"
|
||||
#define LOG_CATEGORY "libnfc.buses.usbbus"
|
||||
#define LOG_GROUP NFC_LOG_GROUP_DRIVER
|
||||
|
||||
// Global flag to know if usb_init() has already been called or not
|
||||
bool usb_initialized=false;
|
||||
|
||||
int usb_prepare(void) {
|
||||
if (usb_initialized)
|
||||
return 0;
|
||||
usb_init();
|
||||
usb_initialized = true;
|
||||
|
||||
int res;
|
||||
// usb_find_busses will find all of the busses on the system. Returns the
|
||||
// number of changes since previous call to this function (total of new
|
||||
// busses and busses removed).
|
||||
if ((res = usb_find_busses()) < 0) {
|
||||
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res));
|
||||
return -1;
|
||||
}
|
||||
// usb_find_devices will find all of the devices on each bus. This should be
|
||||
// called after usb_find_busses. Returns the number of changes since the
|
||||
// previous call to this function (total of new device and devices removed).
|
||||
if ((res = usb_find_devices()) < 0) {
|
||||
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
49
libnfc/buses/usbbus.h
Normal file
49
libnfc/buses/usbbus.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*-
|
||||
* Public platform independent Near Field Communication (NFC) library
|
||||
*
|
||||
* Copyright (C) 2013, Romuald Conty
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file usbbus.h
|
||||
* @brief libusb 0.1 driver header
|
||||
*/
|
||||
|
||||
#ifndef __NFC_BUS_USB_H__
|
||||
# define __NFC_BUS_USB_H__
|
||||
|
||||
#ifndef _WIN32
|
||||
// Under POSIX system, we use libusb (>= 0.1.12)
|
||||
#include <usb.h>
|
||||
#define USB_TIMEDOUT ETIMEDOUT
|
||||
#define _usb_strerror( X ) strerror(-X)
|
||||
#else
|
||||
// Under Windows we use libusb-win32 (>= 1.2.5)
|
||||
#include <lusb0_usb.h>
|
||||
#define USB_TIMEDOUT 116
|
||||
#define _usb_strerror( X ) usb_strerror()
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// Global flag to know if usb_init() has already been called or not
|
||||
extern bool usb_initialized;
|
||||
|
||||
int usb_prepare(void);
|
||||
|
||||
#endif // __NFC_BUS_USB_H__
|
||||
Reference in New Issue
Block a user