[input] Make sure marker list is always ordered by pos

Otherwise the read size in input_read() becomes invalid and we segfault.
This commit is contained in:
ejurgensen 2019-05-01 23:11:39 +02:00
parent f317c887b4
commit bc4b5275f3

View File

@ -258,7 +258,8 @@ marker_free(struct marker *marker)
static void static void
marker_add(size_t pos, short flag, void *flagdata) marker_add(size_t pos, short flag, void *flagdata)
{ {
struct marker *head; struct marker *insert;
struct marker *compare;
struct marker *marker; struct marker *marker;
CHECK_NULL(L_PLAYER, marker = calloc(1, sizeof(struct marker))); CHECK_NULL(L_PLAYER, marker = calloc(1, sizeof(struct marker)));
@ -267,13 +268,29 @@ marker_add(size_t pos, short flag, void *flagdata)
marker->flag = flag; marker->flag = flag;
marker->data = flagdata; marker->data = flagdata;
for (head = input_buffer.marker_tail; head && head->prev; head = head->prev) // We want the list to be ordered by pos, so we reverse through it and compare
; // Fast forward to the head // each element with pos. Only if the element's pos is less than or equal to
// pos do we keep reversing. If no reversing is possible then we insert as new
// tail.
insert = NULL;
compare = input_buffer.marker_tail;
if (!head) while (compare && compare->pos <= pos)
input_buffer.marker_tail = marker; {
insert = compare;
compare = compare->prev;
}
if (insert)
{
marker->prev = insert->prev;
insert->prev = marker;
}
else else
head->prev = marker; {
marker->prev = input_buffer.marker_tail;
input_buffer.marker_tail = marker;
}
} }
static void static void