Skip to content

Commit

Permalink
Fix int overflow for 0 deadline timers
Browse files Browse the repository at this point in the history
Turns out, HAPPlatformTimerRegister may be invoked with deadline of 0 sometimes.
This translates into a negative duration for mgos_set_timer, which is fine
for the first 24.85 days but then duration rolls over and things go south.

Instead, clamp negative duration to 0 while it's still a 64-bit int.

apple/HomeKitADK#102

Fixes mongoose-os-apps/shelly-homekit#129
  • Loading branch information
rojer committed Apr 10, 2021
1 parent 13a22a0 commit 36ba0d2
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions src/PAL/HAPPlatformTimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ HAPError HAPPlatformTimerRegister(
return kHAPError_OutOfResources;
}
int64_t duration = ((int64_t) deadline - (int64_t) HAPPlatformClockGetCurrent());
if (duration < 0) duration = 0;
ctx->timer_id = mgos_set_timer((int) duration, 0, hap_timer_cb, ctx);
if (ctx->timer_id == MGOS_INVALID_TIMER_ID) {
return kHAPError_OutOfResources;
Expand Down

0 comments on commit 36ba0d2

Please sign in to comment.