Skip to content

Commit 33aa905

Browse files
lpereiraMaureenHelm
authored andcommitted
tests: kernel: fifo_timeout: Do not potentially dereference NULL ptrs
The return value from k_fifo_get() might be NULL in some situations, so protect against that. Coverity-ID: 186190 Coverity-ID: 186058 Signed-off-by: Leandro Pereira <[email protected]>
1 parent a37e037 commit 33aa905

File tree

1 file changed

+22
-16
lines changed
  • tests/kernel/fifo/fifo_timeout/src

1 file changed

+22
-16
lines changed

tests/kernel/fifo/fifo_timeout/src/main.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,35 +194,41 @@ static int test_multiple_threads_get_data(struct timeout_order_data *test_data,
194194
K_PRIO_PREEMPT(0), K_INHERIT_PERMS, 0);
195195

196196
for (ii = 0; ii < test_data_size-1; ii++) {
197-
198197
k_fifo_put(test_data[ii].fifo, get_scratch_packet());
199198

200199
data = k_fifo_get(&timeout_order_fifo, K_FOREVER);
200+
if (!data) {
201+
TC_ERROR("thread %d got NULL value from fifo\n", ii);
202+
return TC_FAIL;
203+
}
204+
205+
if (data->q_order != ii) {
206+
TC_ERROR(" *** thread %d woke up, expected %d\n",
207+
data->q_order, ii);
208+
return TC_FAIL;
209+
}
201210

202-
if (data && data->q_order == ii) {
211+
if (data->q_order == ii) {
203212
TC_PRINT(" thread (q order: %d, t/o: %d, fifo %p)\n",
204213
data->q_order, data->timeout, data->fifo);
205-
} else {
206-
if (data) {
207-
TC_ERROR(" *** thread %d woke up, expected %d\n",
208-
data->q_order, ii);
209-
}
210-
return TC_FAIL;
211214
}
212215
}
213216

214217
data = k_fifo_get(&timeout_order_fifo, K_FOREVER);
215-
if (data && data->q_order == ii) {
216-
TC_PRINT(" thread (q order: %d, t/o: %d, fifo %p)\n",
217-
data->q_order, data->timeout, data->fifo);
218-
} else {
219-
if (data) {
220-
TC_ERROR(" *** thread %d woke up, expected %d\n",
221-
data->q_order, ii);
222-
}
218+
if (!data) {
219+
TC_ERROR("thread %d got NULL value from fifo\n", ii);
223220
return TC_FAIL;
224221
}
225222

223+
if (data->q_order != ii) {
224+
TC_ERROR(" *** thread %d woke up, expected %d\n",
225+
data->q_order, ii);
226+
return TC_FAIL;
227+
}
228+
229+
TC_PRINT(" thread (q order: %d, t/o: %d, fifo %p)\n",
230+
data->q_order, data->timeout, data->fifo);
231+
226232
return TC_PASS;
227233
}
228234

0 commit comments

Comments
 (0)