Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

queue_get returns incorrect value when no elements in queue #3

Open
le91688 opened this issue May 18, 2022 · 0 comments
Open

queue_get returns incorrect value when no elements in queue #3

le91688 opened this issue May 18, 2022 · 0 comments

Comments

@le91688
Copy link

le91688 commented May 18, 2022

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 {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant