linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tools: Change asprintf return code check
@ 2022-09-08 14:03 Catalin Petrescu
  2022-09-13 14:47 ` Bartosz Golaszewski
  0 siblings, 1 reply; 4+ messages in thread
From: Catalin Petrescu @ 2022-09-08 14:03 UTC (permalink / raw)
  To: linux-gpio; +Cc: Bartosz Golaszewski

[-- 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-09-14  9:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-08 14:03 [PATCH] tools: Change asprintf return code check Catalin Petrescu
2022-09-13 14:47 ` Bartosz Golaszewski
2022-09-13 20:30   ` Catalin Petrescu
2022-09-14  9:11     ` Bartosz Golaszewski

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).