Skip to contentSkip to conversation

tools: Change asprintf return code check

PATCH In review Catalin Petrescu 2 participants 4 messages Sep 8, 2022 – Sep 14, 2022 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: Change asprintf return code check

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.

From e92b71df3c9f8304a91e0dbe0094614df3784941 Mon Sep 17 00:00:00 2001
From: Catalin Petrescu <[email protected]>
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(-)
tools/tools-common.c +1 -1
index 80087ee..8521c93 100644
@@ -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);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 8 lines · Catalin Petrescu

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.

Quoted 5 lines

I hope this helps.

Thanks,

Catalin.

Hey Catalin,

Please next time send the patch inline using git's send-email command.

You're right about the error code check but it should actually be ret < 0 as per asprintf's documentation.

Bart

#2 lore raw
Quoted 18 lines · Bartosz Golaszewski
Quoted 8 lines · Catalin Petrescu

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.

Quoted 5 lines

I hope this helps.

Thanks,

Catalin.

Hey Catalin,

Quoted 7 lines

Please next time send the patch inline using git's send-email command.

You're right about the error code check but it should actually be ret < 0 as per asprintf's documentation.

Bart

Hi Bart,

I'll use git send-email next time. I was not aware of its existence. And I still need to figure out how to pass the gmail SMTP authentication.

You're right, when asprintf returns zero, technically, that's not an error.
My thought was that if asprintf ever returns zero, that means that the path is a zero-length string.
So, the next call to gpiod_chip_open(path) will likely fail. But I guess, the right thing to do is to let gpiod_chip_open() deal with it.

Thanks,

Catalin.

#3 lore raw
Quoted 41 lines · Catalin Petrescu
Quoted 18 lines · Bartosz Golaszewski
Quoted 8 lines · Catalin Petrescu

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.

Quoted 5 lines

I hope this helps.

Thanks,

Catalin.

Hey Catalin,

Quoted 7 lines

Please next time send the patch inline using git's send-email command.

You're right about the error code check but it should actually be ret < 0 as per asprintf's documentation.

Bart

Hi Bart,

I'll use git send-email next time. I was not aware of its existence. And I still need to figure out how to pass the gmail SMTP authentication.

You're right, when asprintf returns zero, technically, that's not an error.
My thought was that if asprintf ever returns zero, that means that the path is a zero-length string.
So, the next call to gpiod_chip_open(path) will likely fail. But I guess, the right thing to do is to let gpiod_chip_open() deal with it.

Quoted 5 lines

Thanks,

Catalin.

Yes and also next thing we do is use strerror() which will return "Success" for 0.

Bart