drivers: counter: maxim_ds3231: Fix coverity errors

Casting 64-bit time_t values into 32-bit unsigned integers causes
CWE-197 violations. Explicitly truncate these values by masking
time_t to avoid these warnings.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
This commit is contained in:
Tahsin Mutlugun 2024-02-08 19:54:46 +03:00 committed by Henrik Brix Andersen
parent 0e532d8909
commit dde9e48394

View file

@ -25,6 +25,9 @@ LOG_MODULE_REGISTER(DS3231, CONFIG_COUNTER_LOG_LEVEL);
#define REG_DAYDATE_DOW 0x40
#define REG_ALARM_IGN 0x80
/* Return lower 32-bits of time as counter value */
#define COUNTER_GET(t) ((uint32_t) (t & UINT32_MAX))
enum {
SYNCSM_IDLE,
SYNCSM_PREP_READ,
@ -419,6 +422,7 @@ static uint32_t decode_rtc(struct ds3231_data *data)
{
struct tm tm = { 0 };
const struct register_map *rp = &data->registers;
time_t t;
decode_time(&tm, &rp->sec, true);
tm.tm_wday = (rp->dow & 0x07) - 1;
@ -430,7 +434,8 @@ static uint32_t decode_rtc(struct ds3231_data *data)
tm.tm_year += 100;
}
return timeutil_timegm(&tm);
t = timeutil_timegm(&tm);
return COUNTER_GET(t);
}
static int update_registers(const struct device *dev)
@ -760,7 +765,7 @@ static int ds3231_counter_get_value(const struct device *dev,
k_sem_give(&data->lock);
if (rc >= 0) {
*ticks = time;
*ticks = COUNTER_GET(time);
}
return rc;
@ -862,7 +867,7 @@ static void sync_prep_write(const struct device *dev)
data->sync_state = SYNCSM_FINISH_WRITE;
k_timer_start(&data->sync_timer, K_MSEC(rem_ms), K_NO_WAIT);
LOG_INF("sync %u in %u ms after %u", (uint32_t)when, rem_ms, syncclock);
LOG_INF("sync %u in %u ms after %u", COUNTER_GET(when), rem_ms, syncclock);
}
static void sync_finish_write(const struct device *dev)
@ -910,7 +915,7 @@ static void sync_finish_write(const struct device *dev)
data->syncpoint.rtc.tv_sec = when;
data->syncpoint.rtc.tv_nsec = 0;
data->syncpoint.syncclock = syncclock;
LOG_INF("sync %u at %u", (uint32_t)when, syncclock);
LOG_INF("sync %u at %u", COUNTER_GET(when), syncclock);
}
sync_finish(dev, rc);
}
@ -1220,7 +1225,7 @@ int ds3231_counter_set_alarm(const struct device *dev,
}
struct maxim_ds3231_alarm alarm = {
.time = (uint32_t)when,
.time = COUNTER_GET(when),
.handler = counter_alarm_forwarder,
.user_data = alarm_cfg->user_data,
.flags = MAXIM_DS3231_ALARM_FLAGS_AUTODISABLE,
@ -1228,7 +1233,7 @@ int ds3231_counter_set_alarm(const struct device *dev,
if (rc >= 0) {
data->counter_handler[id] = alarm_cfg->callback;
data->counter_ticks[id] = alarm.time;
data->counter_ticks[id] = COUNTER_GET(alarm.time);
rc = set_alarm(dev, id, &alarm);
}