diff --git a/sched/sched/sched_gettcb.c b/sched/sched/sched_gettcb.c index 43823ba849e..3e38f51b3fb 100644 --- a/sched/sched/sched_gettcb.c +++ b/sched/sched/sched_gettcb.c @@ -56,9 +56,13 @@ FAR struct tcb_s *nxsched_get_tcb(pid_t pid) irqstate_t flags; int hash_ndx; - /* Verify that the PID is within range */ + flags = enter_critical_section(); - if (pid >= 0) + /* Verify whether g_pidhash hash table has already been allocated and + * whether the PID is within range. + */ + + if (g_pidhash != NULL && pid >= 0) { /* The test and the return setup should be atomic. This still does * not provide proper protection if the recipient of the TCB does not @@ -66,24 +70,22 @@ FAR struct tcb_s *nxsched_get_tcb(pid_t pid) * terminating asynchronously. */ - flags = enter_critical_section(); - /* Get the hash_ndx associated with the pid */ hash_ndx = PIDHASH(pid); /* Verify that the correct TCB was found. */ - if (g_pidhash && pid == g_pidhash[hash_ndx]->pid) + if (g_pidhash[hash_ndx] != NULL && pid == g_pidhash[hash_ndx]->pid) { /* Return the TCB associated with this pid (if any) */ ret = g_pidhash[hash_ndx]; } - - leave_critical_section(flags); } + leave_critical_section(flags); + /* Return the TCB. */ return ret;