Skip to content

Commit eda2e0e

Browse files
slavendamfpistm
authored andcommitted
Fix subsecond calculation to prevent overflow
Signed-off-by: slavendam <slaven.damjanovic@tel.net.ba>
1 parent 60e7bb4 commit eda2e0e

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

src/rtc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,8 @@ void RTC_StartAlarm64(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes,
928928
*/
929929
if ((initMode == MODE_BINARY_ONLY) || (initMode == MODE_BINARY_MIX)) {
930930
/* the subsecond is the millisecond to be converted in a subsecond downcounter value */
931-
RTC_AlarmStructure.AlarmTime.SubSeconds = UINT32_MAX - ((uint32_t)subSeconds * (predivSync + 1)) / 1000;
931+
uint64_t tmp = (subSeconds * (uint64_t)(predivSync + 1)) / (uint64_t)1000;
932+
RTC_AlarmStructure.AlarmTime.SubSeconds = (uint32_t)UINT32_MAX - (uint32_t)tmp;
932933
} else {
933934
RTC_AlarmStructure.AlarmTime.SubSeconds = predivSync - ((uint32_t)subSeconds * (predivSync + 1)) / 1000;
934935
}

0 commit comments

Comments
 (0)