linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Catalin Petrescu <catalin.petrescu@gmail.com>
To: linux-gpio@vger.kernel.org
Cc: Bartosz Golaszewski <brgl@bgdev.pl>
Subject: [PATCH] tools: Change asprintf return code check
Date: Thu, 08 Sep 2022 10:03:13 -0400	[thread overview]
Message-ID: <e8bf314dd0edfd0709d71e85fb8d877c3722e33e.camel@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 263 bytes --]

Hi there,

I found a small error (IMHO) in libgpiod.
The issue is that asprintf may return -1 if it fails to allocate
memory, and if that happens, chip_open_by_number will pass a NULL
pointer to gpiod_chip_open.

I hope this helps.

Thanks,

Catalin.

[-- Attachment #2: 0001-tools-Change-asprintf-return-code-check.patch --]
[-- Type: text/x-patch, Size: 804 bytes --]

From e92b71df3c9f8304a91e0dbe0094614df3784941 Mon Sep 17 00:00:00 2001
From: Catalin Petrescu <catalin.petrescu@gmail.com>
Date: Tue, 6 Sep 2022 19:07:25 -0400
Subject: [PATCH] tools: Change asprintf return code check

Asprintf may return negative numbers in some cases (failure to allocate
memory, for example).
Treat negative values as errors.
---
 tools/tools-common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/tools-common.c b/tools/tools-common.c
index 80087ee..8521c93 100644
--- a/tools/tools-common.c
+++ b/tools/tools-common.c
@@ -137,7 +137,7 @@ static struct gpiod_chip *chip_open_by_number(unsigned int num)
 	int ret;
 
 	ret = asprintf(&path, "/dev/gpiochip%u", num);
-	if (!ret)
+	if (ret <= 0)
 		return NULL;
 
 	chip = gpiod_chip_open(path);
-- 
2.34.1


             reply	other threads:[~2022-09-08 14:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-08 14:03 Catalin Petrescu [this message]
2022-09-13 14:47 ` [PATCH] tools: Change asprintf return code check Bartosz Golaszewski
2022-09-13 20:30   ` Catalin Petrescu
2022-09-14  9:11     ` Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e8bf314dd0edfd0709d71e85fb8d877c3722e33e.camel@gmail.com \
    --to=catalin.petrescu@gmail.com \
    --cc=brgl@bgdev.pl \
    --cc=linux-gpio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).