Skip to content

Commit f5be003

Browse files
authored
fix(timer): Add check to avoid crashing (espressif#10069)
1 parent 9e01ebd commit f5be003

File tree

1 file changed

+59
-11
lines changed

1 file changed

+59
-11
lines changed

cores/esp32/esp32-hal-timer.c

+59-11
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,28 @@ struct timer_struct_t {
3737
};
3838

3939
inline uint64_t timerRead(hw_timer_t *timer) {
40-
40+
if (timer == NULL) {
41+
log_e("Timer handle is NULL");
42+
return 0;
43+
}
4144
uint64_t value;
4245
gptimer_get_raw_count(timer->timer_handle, &value);
4346
return value;
4447
}
4548

4649
void timerWrite(hw_timer_t *timer, uint64_t val) {
50+
if (timer == NULL) {
51+
log_e("Timer handle is NULL");
52+
return;
53+
}
4754
gptimer_set_raw_count(timer->timer_handle, val);
4855
}
4956

5057
void timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count) {
58+
if (timer == NULL) {
59+
log_e("Timer handle is NULL");
60+
return;
61+
}
5162
esp_err_t err = ESP_OK;
5263
gptimer_alarm_config_t alarm_cfg = {
5364
.alarm_count = alarm_value,
@@ -61,22 +72,37 @@ void timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64
6172
}
6273

6374
uint32_t timerGetFrequency(hw_timer_t *timer) {
75+
if (timer == NULL) {
76+
return 0;
77+
}
6478
uint32_t frequency;
6579
gptimer_get_resolution(timer->timer_handle, &frequency);
6680
return frequency;
6781
}
6882

6983
void timerStart(hw_timer_t *timer) {
84+
if (timer == NULL) {
85+
log_e("Timer handle is NULL");
86+
return;
87+
}
7088
gptimer_start(timer->timer_handle);
7189
timer->timer_started = true;
7290
}
7391

7492
void timerStop(hw_timer_t *timer) {
93+
if (timer == NULL) {
94+
log_e("Timer handle is NULL");
95+
return;
96+
}
7597
gptimer_stop(timer->timer_handle);
7698
timer->timer_started = false;
7799
}
78100

79101
void timerRestart(hw_timer_t *timer) {
102+
if (timer == NULL) {
103+
log_e("Timer handle is NULL");
104+
return;
105+
}
80106
gptimer_set_raw_count(timer->timer_handle, 0);
81107
}
82108

@@ -129,17 +155,19 @@ hw_timer_t *timerBegin(uint32_t frequency) {
129155
}
130156

131157
void timerEnd(hw_timer_t *timer) {
132-
esp_err_t err = ESP_OK;
133-
if (timer->timer_started == true) {
134-
gptimer_stop(timer->timer_handle);
135-
}
136-
gptimer_disable(timer->timer_handle);
137-
err = gptimer_del_timer(timer->timer_handle);
138-
if (err != ESP_OK) {
139-
log_e("Failed to destroy GPTimer, error num=%d", err);
140-
return;
158+
if (timer != NULL) {
159+
esp_err_t err = ESP_OK;
160+
if (timer->timer_started == true) {
161+
gptimer_stop(timer->timer_handle);
162+
}
163+
gptimer_disable(timer->timer_handle);
164+
err = gptimer_del_timer(timer->timer_handle);
165+
if (err != ESP_OK) {
166+
log_e("Failed to destroy GPTimer, error num=%d", err);
167+
return;
168+
}
169+
free(timer);
141170
}
142-
free(timer);
143171
}
144172

145173
bool IRAM_ATTR timerFnWrapper(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *args) {
@@ -156,6 +184,10 @@ bool IRAM_ATTR timerFnWrapper(gptimer_handle_t timer, const gptimer_alarm_event_
156184
}
157185

158186
void timerAttachInterruptFunctionalArg(hw_timer_t *timer, void (*userFunc)(void *), void *arg) {
187+
if (timer == NULL) {
188+
log_e("Timer handle is NULL");
189+
return;
190+
}
159191
esp_err_t err = ESP_OK;
160192
gptimer_event_callbacks_t cbs = {
161193
.on_alarm = timerFnWrapper,
@@ -187,6 +219,10 @@ void timerAttachInterrupt(hw_timer_t *timer, voidFuncPtr userFunc) {
187219
}
188220

189221
void timerDetachInterrupt(hw_timer_t *timer) {
222+
if (timer == NULL) {
223+
log_e("Timer handle is NULL");
224+
return;
225+
}
190226
esp_err_t err = ESP_OK;
191227
err = gptimer_set_alarm_action(timer->timer_handle, NULL);
192228
timer->interrupt_handle.fn = NULL;
@@ -197,18 +233,30 @@ void timerDetachInterrupt(hw_timer_t *timer) {
197233
}
198234

199235
uint64_t timerReadMicros(hw_timer_t *timer) {
236+
if (timer == NULL) {
237+
log_e("Timer handle is NULL");
238+
return 0;
239+
}
200240
uint64_t timer_val = timerRead(timer);
201241
uint32_t frequency = timerGetFrequency(timer);
202242
return timer_val * 1000000 / frequency;
203243
}
204244

205245
uint64_t timerReadMilis(hw_timer_t *timer) {
246+
if (timer == NULL) {
247+
log_e("Timer handle is NULL");
248+
return 0;
249+
}
206250
uint64_t timer_val = timerRead(timer);
207251
uint32_t frequency = timerGetFrequency(timer);
208252
return timer_val * 1000 / frequency;
209253
}
210254

211255
double timerReadSeconds(hw_timer_t *timer) {
256+
if (timer == NULL) {
257+
log_e("Timer handle is NULL");
258+
return 0;
259+
}
212260
uint64_t timer_val = timerRead(timer);
213261
uint32_t frequency = timerGetFrequency(timer);
214262
return (double)timer_val / frequency;

0 commit comments

Comments
 (0)