diff mbox series

[libgpiod,1/7] tools: Clean up scandir memory allocations

Message ID 20220203042134.68425-2-joel@jms.id.au
State New
Headers show
Series tools: Add by-name support | expand

Commit Message

Joel Stanley Feb. 3, 2022, 4:21 a.m. UTC
The code copied from gpiofind didn't free the memory from scandir.

Signed-off-by: Joel Stanley <joel@jms.id.au>
---
 tools/tools-common.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Bartosz Golaszewski Feb. 8, 2022, 11:21 a.m. UTC | #1
On Thu, Feb 3, 2022 at 5:22 AM Joel Stanley <joel@jms.id.au> wrote:
>
> The code copied from gpiofind didn't free the memory from scandir.
>
> Signed-off-by: Joel Stanley <joel@jms.id.au>
> ---
>  tools/tools-common.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/tools/tools-common.c b/tools/tools-common.c
> index 0dc3d52668d7..c83e68a2c1e4 100644
> --- a/tools/tools-common.c
> +++ b/tools/tools-common.c
> @@ -190,12 +190,17 @@ struct gpiod_chip *chip_by_line_name(const char *name)
>
>                         die_perror("unable to open %s", entries[i]->d_name);
>                 }
> +               free(entries[i]);
>
>                 offset = gpiod_chip_find_line(chip, name);
> -               if (offset >= 0)
> +               if (offset >= 0) {
> +                       free(entries);
>                         return chip;
> +               }
> +               gpiod_chip_unref(chip);
>         }
>
> +       free(entries);
>         return NULL;
>  }
>
> --
> 2.34.1
>

Good catch! It doesn't come up with gpiodetect which is what I tested
with valgrind but does show up for gpiofind and gpioinfo. This patch
however doesn't apply on the current master. Also: I don't really want
new features for the v1.6.x series and we're heavily reworking the
interface. Current version is in the next/libgpiod-2.0 branch but I
should be posting a new version with reworked test suite later this
week. How about working on this feature for the new API right away and
make it part of libgpiod starting with v2.0?

Bart
diff mbox series

Patch

diff --git a/tools/tools-common.c b/tools/tools-common.c
index 0dc3d52668d7..c83e68a2c1e4 100644
--- a/tools/tools-common.c
+++ b/tools/tools-common.c
@@ -190,12 +190,17 @@  struct gpiod_chip *chip_by_line_name(const char *name)
 
 			die_perror("unable to open %s", entries[i]->d_name);
 		}
+		free(entries[i]);
 
 		offset = gpiod_chip_find_line(chip, name);
-		if (offset >= 0)
+		if (offset >= 0) {
+			free(entries);
 			return chip;
+		}
+		gpiod_chip_unref(chip);
 	}
 
+	free(entries);
 	return NULL;
 }