My code suddenly not working anymore today, what could be the issue? Here is the part of the code.
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
int32_t tnow = tp.tv_sec * 1000 tp.tv_nsec / 1000000;
if (tnow - decoder->last_dumped >= 300) {
decoder->last_dumped = tnow;
// it was working before
...
}
CodePudding user response:
tv_sec is a signed integer holding seconds, so 1000 * tv_sec will overflow an int32_t. Use int64_t.
Overflow will occur after (1 << 31) milliseconds, which is about 24.8 days.