Skip to content

Commit

Permalink
kernel: Compare pointers with NULL in while statements
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Flavio Ceolin authored and nashif committed Sep 18, 2018
1 parent b3d9202 commit c806ac3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions kernel/pipes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion kernel/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/sys_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions kernel/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c806ac3

Please sign in to comment.