Skip to content

[BUG]Mutex leak in remove_from_queue() upon memory allocation failure #51

@hanqing2025

Description

@hanqing2025

Describe the Bug

A mutex leak in remove_from_queue() causes permanent deadlock when memory reallocation fails after removing items. The function acquires a mutex but returns without releasing it on error paths.

Location: obe.c (lines 207-215)

int remove_from_queue(obe_queue_t *queue)
{
    void **tmp;

    pthread_mutex_lock(&queue->mutex);  // Lock acquired
    if (queue->size > 1)
        memmove(&queue->queue[0], &queue->queue[1], sizeof(*queue->queue) * (queue->size-1));
    tmp = realloc(queue->queue, sizeof(*queue->queue) * (queue->size-1));
    queue->size--;
    if (!tmp && queue->size)
    {
        syslog(LOG_ERR, "Malloc failed\n");
        return -1;  // BUG: Returns without unlock!
    }
    queue->queue = tmp;

    pthread_cond_signal(&queue->out_cv);
    pthread_mutex_unlock(&queue->mutex);  // Never reached on error

    return 0;
}

Impact:

  • Mutex queue->mutex remains permanently locked after realloc() failure
  • All queue operations (add, remove, peek) deadlock permanently
  • Encoder pipeline stalls completely → Denial of Service

Execution Flow:

Thread 1:
  → pthread_mutex_lock(&queue->mutex)    // Lock acquired
  → memmove() to shift queue elements    // Success
  → tmp = realloc(...)                    // Memory allocation fails
  → if (!tmp && queue->size)              // Condition TRUE
  → return -1                             // Exit without unlock
  → queue->mutex remains locked forever

Thread 2, 3, ...N:
  → Attempt any queue operation
  → pthread_mutex_lock(&queue->mutex)    // BLOCKS permanently
  → Complete deadlock state

CWE Classification:

  • CWE-667: Improper Locking
  • CWE-833: Deadlock

I would appreciate it if you could review and confirm this potential issue. Thank you for your time and for maintaining this project!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions