Timer overflow problem

Please post support and help requests here.
Posts: 4
Joined: 03 Jul 2012, 08:07

Timer overflow problem

Postby DmitryBogdanov » 14 Jul 2012, 12:39


We used POCO library in our project on Linux and found 3 issues which forced us to make changes in POCO sources. One of them is clearly a bug, and I want to start from it.

Let's look at Timer::start (Foundation/src/Timer.cpp):

nextInvocation += _startInterval*1000;

Then in Timer::run twice:

_nextInvocation += interval*1000;

Note that _startInterval and interval are declared as long, and 1000 is int.

What will happen on 32-bit OS when you want to wait for initial timer run or have interval between runs as quite a long time? Hours or days? When converted to microseconds, that interval will overflow long data type which is 4-byte on 32-bit OS. Let's check - 24 hours = 86,400 seconds = 86,400,000,000 microseconds (86 billion). 4-byte integer can hold only 2 billion (OK, 2,147,483,647).

As you see, it is quite easy to overflow long on 32-bit OS when using microseconds. However, fix is easy - when multiplying 2 numbers and none of them in 64-bit, just convert one of them to Int64. For example:

nextInvocation += Int64(_startInterval)*1000;

This will make compiler use Int64 when doing multiplication, and thus 64-bit integer math rather than 32-bit. We have applied and verified this fix with long initial and periodic intervals on 32-bit Linux and it worked fine.

Posts: 1375
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Timer overflow problem

Postby alex » 14 Jul 2012, 17:09

Thanks, it will be fixed in the next release.

Return to “Support”

Who is online

Users browsing this forum: No registered users and 4 guests