task_queue_put - prevent dereference of null pointer

This commit is contained in:
twinaphex 2020-06-29 19:57:52 +02:00
parent bc61ceb338
commit 28399641da
1 changed files with 21 additions and 15 deletions

View File

@ -111,31 +111,37 @@ static void task_queue_push_progress(retro_task_t *task)
static void task_queue_put(task_queue_t *queue, retro_task_t *task) static void task_queue_put(task_queue_t *queue, retro_task_t *task)
{ {
task->next = NULL; task->next = NULL;
if (queue->front) if (queue->front)
{ {
/* Make sure to insert in order - the queue is sorted by 'when' so items that aren't scheduled /* Make sure to insert in order - the queue is
* to run immediately are at the back of the queue. Items with the same 'when' are inserted after * sorted by 'when' so items that aren't scheduled
* all the other items with the same 'when'. This primarily affects items with a 'when' of 0. * to run immediately are at the back of the queue.
* Items with the same 'when' are inserted after
* all the other items with the same 'when'.
* This primarily affects items with a 'when' of 0.
*/ */
if (queue->back->when > task->when) if (queue->back)
{ {
retro_task_t** prev = &queue->front; if (queue->back->when > task->when)
while (*prev && (*prev)->when <= task->when) {
prev = &((*prev)->next); retro_task_t** prev = &queue->front;
while (*prev && (*prev)->when <= task->when)
prev = &((*prev)->next);
task->next = *prev; task->next = *prev;
*prev = task; *prev = task;
return; return;
}
queue->back->next = task;
} }
queue->back->next = task;
} }
else else
queue->front = task; queue->front = task;
queue->back = task; queue->back = task;
} }
static retro_task_t *task_queue_get(task_queue_t *queue) static retro_task_t *task_queue_get(task_queue_t *queue)