* [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
* Re: [PATCH] tools: Change asprintf return code check
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
0 siblings, 1 reply; 4+ messages in thread
From: Bartosz Golaszewski @ 2022-09-13 14:47 UTC (permalink / raw)
To: Catalin Petrescu; +Cc: linux-gpio
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] tools: Change asprintf return code check
2022-09-13 14:47 ` Bartosz Golaszewski
@ 2022-09-13 20:30 ` Catalin Petrescu
2022-09-14 9:11 ` Bartosz Golaszewski
0 siblings, 1 reply; 4+ messages in thread
From: Catalin Petrescu @ 2022-09-13 20:30 UTC (permalink / raw)
To: Bartosz Golaszewski; +Cc: linux-gpio
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.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] tools: Change asprintf return code check
2022-09-13 20:30 ` Catalin Petrescu
@ 2022-09-14 9:11 ` Bartosz Golaszewski
0 siblings, 0 replies; 4+ messages in thread
From: Bartosz Golaszewski @ 2022-09-14 9:11 UTC (permalink / raw)
To: Catalin Petrescu; +Cc: linux-gpio
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
^ permalink raw reply [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).