Queue icon indicating copy to clipboard operation
Queue copied to clipboard

queue_get returns incorrect value when no elements in queue

Open le91688 opened this issue 2 years ago • 0 comments

according to the doc , if you call queue_get on a queue with no elements, it should return > 0.

/**
 * get the first element of the queue
 *
 * q - the queue
 * e - pointer which will be set to the element
 *
 * returns 0 if everything worked, > 0 if no elements in queue, < 0 if error occured
 */
int8_t queue_get(queue_t *q, void **e);

It currently returns Q_ERR_NUM_ELEMENTS (-7) instead.

int8_t queue_get(queue_t *q, void **e) {
    *e = NULL;
    if (q == NULL)
        return Q_ERR_INVALID;
    if (0 != queue_lock_internal(q))
        return Q_ERR_LOCK;

    int8_t r = queue_get_internal(q, e, NULL, NULL, NULL);

    if (0 != queue_unlock_internal(q))
        return Q_ERR_LOCK;
    return r;
}

queue_get calls queue_get_internal which sets this here

int8_t queue_get_internal(queue_t *q, void **e, int (*action)(pthread_cond_t *, pthread_mutex_t *),
                          int (*cmp)(void *, void *), void *cmpel) {
    if (q == NULL) { // queue not valid
        *e = NULL;
        return Q_ERR_INVALID;
    }

    // are elements in the queue?
    if (q->num_els == 0) {
        if (action == NULL) {
            *e = NULL;
            return Q_ERR_NUM_ELEMENTS;  <------------- here
        } else {

le91688 avatar May 18 '22 17:05 le91688