Skip to contentSkip to conversation

Integer overflow in C++ binding on 32bit systems causes wrong event timestamps

libgpiod Florian Evers 2 participants 3 messages Sep 6, 2020 – Sep 11, 2020 GPIO

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw
Quoted 23 lines · Florian Evers

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.

Quoted 8 lines

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

#2 lore raw

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:

Quoted 39 lines · Florian Evers
Quoted 22 lines

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.

Quoted 8 lines

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