diff mbox series

tools: Change asprintf return code check

Message ID e8bf314dd0edfd0709d71e85fb8d877c3722e33e.camel@gmail.com
State New
Headers show
Series tools: Change asprintf return code check | expand

Commit Message

Catalin Petrescu Sept. 8, 2022, 2:03 p.m. UTC
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.

Comments

Bartosz Golaszewski Sept. 13, 2022, 2:47 p.m. UTC | #1
On Thu, Sep 8, 2022 at 4:03 PM Catalin Petrescu
<catalin.petrescu@gmail.com> wrote:
>
> 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.

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
Catalin Petrescu Sept. 13, 2022, 8:30 p.m. UTC | #2
On Tue, 2022-09-13 at 16:47 +0200, Bartosz Golaszewski wrote:
> On Thu, Sep 8, 2022 at 4:03 PM Catalin Petrescu
> <catalin.petrescu@gmail.com> wrote:
> > 
> > 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.
> 
> 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

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.
Bartosz Golaszewski Sept. 14, 2022, 9:11 a.m. UTC | #3
On Tue, Sep 13, 2022 at 10:30 PM Catalin Petrescu
<catalin.petrescu@gmail.com> wrote:
>
> On Tue, 2022-09-13 at 16:47 +0200, Bartosz Golaszewski wrote:
> > On Thu, Sep 8, 2022 at 4:03 PM Catalin Petrescu
> > <catalin.petrescu@gmail.com> wrote:
> > >
> > > 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.
> >
> > 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
>
> 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.
>
>

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

Bart
diff mbox series

Patch

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