Bug in pa_memblockq_push()
Maybe there is a bug in pa_memblockq_push:
In this if statment:
if (bq->write_index + (int64_t) chunk.length < q->index + (int64_t) q->chunk.length) {
/* We need to save the end of this memchunk */
struct list_item *p;
size_t d;
/* Create a new list entry for the end of the memchunk */
if (!(p = pa_flist_pop(PA_STATIC_FLIST_GET(list_items))))
p = pa_xnew(struct list_item, 1);
p->chunk = q->chunk;
pa_memblock_ref(p->chunk.memblock);
/* Calculate offset */
d = (size_t) (bq->write_index + (int64_t) chunk.length - q->index);
pa_assert(d > 0);
/* Drop it from the new entry */
p->index = q->index + (int64_t) d;
// I THINK THERE IS A BUG HERE
p->chunk.length -= d;
/* Add it to the list */
p->prev = q;
if ((p->next = q->next))
q->next->prev = p;
else
bq->blocks_tail = p;
q->next = p;
bq->n_blocks++;
}
We create a new list entry for the end of the memchunk. But we only adjust the p->index and p->chunk.length, the p->chunk.index remains unchanged. So the new list entry starts with the memchunk in q.
For example:
bq = pa_memblockq_new("test memblockq", 0, 200, 10, &ss, 4, 4, 40, &silence);
chunk1 = memchunk_from_str(p, "1234567890");
pa_memblockq_push(bq, &chunk1);
chunk2 = memchunk_from_str(p, "FF");
pa_memblockq_seek(bq, -4, 0, true);
pa_memblockq_push(bq, &chunk2);
The buffer in bq is "123456FF12".