srdja / Collections-C

A library of generic data structures for the C language.
http://srdja.github.io/Collections-C
GNU Lesser General Public License v3.0
2.82k stars 328 forks source link

deque_remove_at error #149

Closed yaoture3 closed 1 year ago

yaoture3 commented 2 years ago

Issue repro steps:

  1. Add at least 2 elements with deque_add_first.

  2. Use deque_remove_at to get a element which index greater than 0,your will find element is wrong.

I think the problem is at this sentence 'void *removed = deque->buffer[index];', in which 'index' should be replaced by 'p'.

enum cc_stat deque_remove_at(Deque *deque, size_t index, void **out) { if (index >= deque->size) return CC_ERR_OUT_OF_RANGE;

const size_t c = deque->capacity - 1;
const size_t l = deque->last & c;
const size_t f = deque->first & c;
const size_t p = (deque->first + index) & c;

void *removed  = deque->buffer[index];

if (index == 0)

......