From 3d040d73f39f124b3d0a707f38d0acb642e3b641 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 19 Sep 2013 23:34:52 +0200 Subject: [PATCH] Fix memory leak Problems reported by Coverity: CID 1090335 (#1 of 1): Resource leak (RESOURCE_LEAK) 24. leaked_storage: Variable "acPorts" going out of scope leaks the storage it points to. CID 1090336 (#1 of 1): Resource leak (RESOURCE_LEAK) 10. leaked_storage: Variable "acPorts" going out of scope leaks the storage it points to. CID 1090337 (#1 of 1): Resource leak (RESOURCE_LEAK) 21. leaked_storage: Variable "i2cPorts" going out of scope leaks the storage it points to. CID 1090338 (#1 of 1): Resource leak (RESOURCE_LEAK) 21. leaked_storage: Variable "acPorts" going out of scope leaks the storage it points to. CID 1090339 (#1 of 1): Resource leak (RESOURCE_LEAK) 23. leaked_storage: Variable "acPorts" going out of scope leaks the storage it points to. --- libnfc/drivers/acr122s.c | 20 ++++++++++++++++++++ libnfc/drivers/arygon.c | 20 ++++++++++++++++++++ libnfc/drivers/pn532_i2c.c | 15 +++++++++++++++ libnfc/drivers/pn532_spi.c | 15 +++++++++++++++ libnfc/drivers/pn532_uart.c | 20 ++++++++++++++++++++ 5 files changed, 90 insertions(+) diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index 6dd7cab..e04f0f8 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -435,6 +435,11 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz if (!pnd) { perror("malloc"); uart_close(sp); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return -1; } @@ -444,6 +449,11 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz perror("malloc"); uart_close(sp); nfc_device_free(pnd); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return -1; } DRIVER_DATA(pnd)->port = sp; @@ -453,6 +463,11 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz if (pipe(DRIVER_DATA(pnd)->abort_fds) < 0) { uart_close(DRIVER_DATA(pnd)->port); nfc_device_free(pnd); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } #else @@ -463,6 +478,11 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz perror("malloc"); uart_close(DRIVER_DATA(pnd)->port); nfc_device_free(pnd); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } CHIP_DATA(pnd)->type = PN532; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 54781c1..d7229f8 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -122,6 +122,11 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size if (!pnd) { perror("malloc"); uart_close(sp); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } @@ -131,6 +136,11 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size perror("malloc"); uart_close(sp); nfc_device_free(pnd); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } DRIVER_DATA(pnd)->port = sp; @@ -140,6 +150,11 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size perror("malloc"); uart_close(DRIVER_DATA(pnd)->port); nfc_device_free(pnd); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } @@ -149,6 +164,11 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size uart_close(DRIVER_DATA(pnd)->port); pn53x_data_free(pnd); nfc_device_free(pnd); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } #else diff --git a/libnfc/drivers/pn532_i2c.c b/libnfc/drivers/pn532_i2c.c index 7fb7185..b62c7de 100644 --- a/libnfc/drivers/pn532_i2c.c +++ b/libnfc/drivers/pn532_i2c.c @@ -125,6 +125,11 @@ pn532_i2c_scan(const nfc_context *context, nfc_connstring connstrings[], const s if (!pnd) { perror("malloc"); i2c_close(id); + iDevice = 0; + while ((i2cPort = i2cPorts[iDevice++])) { + free((void *)i2cPort); + } + free(i2cPorts); return 0; } pnd->driver = &pn532_i2c_driver; @@ -133,6 +138,11 @@ pn532_i2c_scan(const nfc_context *context, nfc_connstring connstrings[], const s perror("malloc"); i2c_close(id); nfc_device_free(pnd); + iDevice = 0; + while ((i2cPort = i2cPorts[iDevice++])) { + free((void *)i2cPort); + } + free(i2cPorts); return 0; } DRIVER_DATA(pnd)->dev = id; @@ -142,6 +152,11 @@ pn532_i2c_scan(const nfc_context *context, nfc_connstring connstrings[], const s perror("malloc"); i2c_close(DRIVER_DATA(pnd)->dev); nfc_device_free(pnd); + iDevice = 0; + while ((i2cPort = i2cPorts[iDevice++])) { + free((void *)i2cPort); + } + free(i2cPorts); return 0; } diff --git a/libnfc/drivers/pn532_spi.c b/libnfc/drivers/pn532_spi.c index 08fc7d1..f9ed9db 100644 --- a/libnfc/drivers/pn532_spi.c +++ b/libnfc/drivers/pn532_spi.c @@ -111,6 +111,11 @@ pn532_spi_scan(const nfc_context *context, nfc_connstring connstrings[], const s if (!pnd) { perror("malloc"); spi_close(sp); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } pnd->driver = &pn532_spi_driver; @@ -119,6 +124,11 @@ pn532_spi_scan(const nfc_context *context, nfc_connstring connstrings[], const s perror("malloc"); spi_close(sp); nfc_device_free(pnd); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } DRIVER_DATA(pnd)->port = sp; @@ -128,6 +138,11 @@ pn532_spi_scan(const nfc_context *context, nfc_connstring connstrings[], const s perror("malloc"); spi_close(DRIVER_DATA(pnd)->port); nfc_device_free(pnd); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } // SAMConfiguration command if needed to wakeup the chip and pn53x_SAMConfiguration check if the chip is a PN532 diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 0e1c0d5..9561b89 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -96,6 +96,11 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const if (!pnd) { perror("malloc"); uart_close(sp); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } pnd->driver = &pn532_uart_driver; @@ -104,6 +109,11 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const perror("malloc"); uart_close(sp); nfc_device_free(pnd); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } DRIVER_DATA(pnd)->port = sp; @@ -113,6 +123,11 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const perror("malloc"); uart_close(DRIVER_DATA(pnd)->port); nfc_device_free(pnd); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } // SAMConfiguration command if needed to wakeup the chip and pn53x_SAMConfiguration check if the chip is a PN532 @@ -126,6 +141,11 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const uart_close(DRIVER_DATA(pnd)->port); pn53x_data_free(pnd); nfc_device_free(pnd); + iDevice = 0; + while ((acPort = acPorts[iDevice++])) { + free((void *)acPort); + } + free(acPorts); return 0; } #else