From c806ac3d36691fcae8c7af87dda2a43621efcb35 Mon Sep 17 00:00:00 2001 From: Flavio Ceolin Date: Mon, 17 Sep 2018 16:03:52 -0700 Subject: [PATCH] kernel: Compare pointers with NULL in while statements Make while statement using pointers explicitly check whether the value is NULL or not. The C standard does not say that the null pointer is the same as the pointer to memory address 0 and because of this is a good practice always compare with the macro NULL. Signed-off-by: Flavio Ceolin --- kernel/pipes.c | 6 +++--- kernel/queue.c | 3 ++- kernel/sys_clock.c | 2 +- kernel/thread.c | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/kernel/pipes.c b/kernel/pipes.c index f56fc335b90..c30e80e0dcc 100644 --- a/kernel/pipes.c +++ b/kernel/pipes.c @@ -477,7 +477,7 @@ int _k_pipe_put_internal(struct k_pipe *pipe, struct k_pipe_async *async_desc, struct k_thread *thread = (struct k_thread *) sys_dlist_get(&xfer_list); - while (thread) { + while (thread != NULL) { desc = (struct k_pipe_desc *)thread->base.swap_data; bytes_copied = pipe_xfer(desc->buffer, desc->bytes_to_xfer, data + num_bytes_written, @@ -625,7 +625,7 @@ int _impl_k_pipe_get(struct k_pipe *pipe, void *data, size_t bytes_to_read, struct k_thread *thread = (struct k_thread *) sys_dlist_get(&xfer_list); - while (thread && (num_bytes_read < bytes_to_read)) { + while ((thread != NULL) && (num_bytes_read < bytes_to_read)) { desc = (struct k_pipe_desc *)thread->base.swap_data; bytes_copied = pipe_xfer(data + num_bytes_read, bytes_to_read - num_bytes_read, @@ -665,7 +665,7 @@ int _impl_k_pipe_get(struct k_pipe *pipe, void *data, size_t bytes_to_read, * into the pipe's circular buffer. */ - while (thread) { + while (thread != NULL) { desc = (struct k_pipe_desc *)thread->base.swap_data; bytes_copied = pipe_buffer_put(pipe, desc->buffer, desc->bytes_to_xfer); diff --git a/kernel/queue.c b/kernel/queue.c index a493a6bd2e2..0bfd19aff01 100644 --- a/kernel/queue.c +++ b/kernel/queue.c @@ -237,7 +237,8 @@ void k_queue_append_list(struct k_queue *queue, void *head, void *tail) #if !defined(CONFIG_POLL) struct k_thread *thread; - while (head && ((thread = _unpend_first_thread(&queue->wait_q)))) { + while ((head != NULL) && + (thread = _unpend_first_thread(&queue->wait_q))) { prepare_thread_to_run(thread, head); head = *(void **)head; } diff --git a/kernel/sys_clock.c b/kernel/sys_clock.c index 471a3a84989..ef46b2915e0 100644 --- a/kernel/sys_clock.c +++ b/kernel/sys_clock.c @@ -196,7 +196,7 @@ static inline void handle_timeouts(s32_t ticks) * prohibited. */ - while (next) { + while (next != NULL) { /* * In the case where ticks number is greater than the first diff --git a/kernel/thread.c b/kernel/thread.c index 012234e3ee6..7c6a34ee74a 100644 --- a/kernel/thread.c +++ b/kernel/thread.c @@ -162,8 +162,8 @@ void _thread_monitor_exit(struct k_thread *thread) struct k_thread *prev_thread; prev_thread = _kernel.threads; - while (prev_thread != NULL && - thread != prev_thread->next_thread) { + while ((prev_thread != NULL) && + (thread != prev_thread->next_thread)) { prev_thread = prev_thread->next_thread; } if (prev_thread != NULL) {