linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [libgpiod] Integer overflow in C++ binding on 32bit systems causes wrong event timestamps
@ 2020-09-06 16:52 Florian Evers
  2020-09-09  8:05 ` Bartosz Golaszewski
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Evers @ 2020-09-06 16:52 UTC (permalink / raw)
  To: linux-gpio

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

Hello,

I found a bug in the c++ bindings of libgpiod that affects 32bit systems.

Observation:

Only the c++ bindings lib is affected. For input events, the time stamp
delivered in the event object is wrong. Instead of providing a complete
"system time", it delivers time stamps of of a range "-2.1"...+2.1 seconds
(which relates to the time stamp range of a signed 32bit value, in
nanoseconds).

Cause:

The bug is in line 219 of file "/tree/bindings/cxx/line.cpp":

event.ts.tv_nsec + (event.ts.tv_sec * 1000000000));

"event.ts.tv_sec" is of type signed long, which is 32 bit on my device. Thus,
that multiplication to convert total seconds of a system time to nanoseconds
causes an overflow.

Fix:

There should be a cast to uint64_t before the multiplication.

Thank you for fixing this!

Regards,
Florian

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [libgpiod] Integer overflow in C++ binding on 32bit systems causes wrong event timestamps
  2020-09-06 16:52 [libgpiod] Integer overflow in C++ binding on 32bit systems causes wrong event timestamps Florian Evers
@ 2020-09-09  8:05 ` Bartosz Golaszewski
  2020-09-11 10:52   ` Florian Evers
  0 siblings, 1 reply; 3+ messages in thread
From: Bartosz Golaszewski @ 2020-09-09  8:05 UTC (permalink / raw)
  To: Florian Evers; +Cc: open list:GPIO SUBSYSTEM, Kent Gibson

On Sun, Sep 6, 2020 at 6:52 PM Florian Evers <florian-evers@gmx.de> wrote:
>
> Hello,
>
> I found a bug in the c++ bindings of libgpiod that affects 32bit systems.
>
> Observation:
>
> Only the c++ bindings lib is affected. For input events, the time stamp
> delivered in the event object is wrong. Instead of providing a complete
> "system time", it delivers time stamps of of a range "-2.1"...+2.1 seconds
> (which relates to the time stamp range of a signed 32bit value, in
> nanoseconds).
>
> Cause:
>
> The bug is in line 219 of file "/tree/bindings/cxx/line.cpp":
>
> event.ts.tv_nsec + (event.ts.tv_sec * 1000000000));
>
> "event.ts.tv_sec" is of type signed long, which is 32 bit on my device. Thus,
> that multiplication to convert total seconds of a system time to nanoseconds
> causes an overflow.
>
> Fix:
>
> There should be a cast to uint64_t before the multiplication.
>
> Thank you for fixing this!
>
> Regards,
> Florian

Hi Florian,

thanks for the report! I see Kent already sent a fix - could you test
it on your setup?

Thanks in advance,
Bartosz Golaszewski

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

* Re: [libgpiod] Integer overflow in C++ binding on 32bit systems causes wrong event timestamps
  2020-09-09  8:05 ` Bartosz Golaszewski
@ 2020-09-11 10:52   ` Florian Evers
  0 siblings, 0 replies; 3+ messages in thread
From: Florian Evers @ 2020-09-11 10:52 UTC (permalink / raw)
  To: linux-gpio; +Cc: Kent Gibson, Bartosz Golaszewski

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

Hi,

thank you very much for the patch. Your approach to use std::chrono directly
is far better than my proposal to use a simple cast! I tested it on my
platform and the use of std::chrono does not cause the overflow anymore. Great!

Regards,
Florian

Am Mittwoch, 9. September 2020, 10:05:14 CEST schrieb Bartosz Golaszewski:
> On Sun, Sep 6, 2020 at 6:52 PM Florian Evers <florian-evers@gmx.de> wrote:
> > Hello,
> >
> > I found a bug in the c++ bindings of libgpiod that affects 32bit systems.
> >
> > Observation:
> >
> > Only the c++ bindings lib is affected. For input events, the time stamp
> > delivered in the event object is wrong. Instead of providing a complete
> > "system time", it delivers time stamps of of a range "-2.1"...+2.1 seconds
> > (which relates to the time stamp range of a signed 32bit value, in
> > nanoseconds).
> >
> > Cause:
> >
> > The bug is in line 219 of file "/tree/bindings/cxx/line.cpp":
> >
> > event.ts.tv_nsec + (event.ts.tv_sec * 1000000000));
> >
> > "event.ts.tv_sec" is of type signed long, which is 32 bit on my device.
> > Thus, that multiplication to convert total seconds of a system time to
> > nanoseconds causes an overflow.
> >
> > Fix:
> >
> > There should be a cast to uint64_t before the multiplication.
> >
> > Thank you for fixing this!
> >
> > Regards,
> > Florian
>
> Hi Florian,
>
> thanks for the report! I see Kent already sent a fix - could you test
> it on your setup?
>
> Thanks in advance,
> Bartosz Golaszewski


[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

end of thread, other threads:[~2020-09-11 10:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-06 16:52 [libgpiod] Integer overflow in C++ binding on 32bit systems causes wrong event timestamps Florian Evers
2020-09-09  8:05 ` Bartosz Golaszewski
2020-09-11 10:52   ` Florian Evers

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