Skip to content

fix: avoid uninitialized read in queue_using_linkedlist dequeue() - #3238

Open
Saadanjum0 wants to merge 1 commit into
TheAlgorithms:masterfrom
Saadanjum0:fix/queue-linkedlist-uninitialized-dequeue
Open

Saadanjum0 wants to merge 1 commit into
TheAlgorithms:masterfrom
Saadanjum0:fix/queue-linkedlist-uninitialized-dequeue

Conversation

@Saadanjum0

@Saadanjum0 Saadanjum0 commented Oct 2, 2026 •

Copy link
Copy Markdown

Description of Change

dequeue() in data_structures/queue_using_linkedlist.cpp declared int ele; without initializing it. When the queue is empty, the function only prints Stack is empty and falls straight through to return ele;, returning an undefined value instead of whatever is intended for the empty-queue case. This is undefined behavior, confirmed directly by clang:

warning: variable 'ele' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]

Because it's undefined behavior, the observed symptom depends on the compiler and optimization level rather than being one fixed value. At -O0 the uninitialized read reliably surfaces as a crash: the project's existing assert-based self-test style exercises dequeue() on an empty queue, and the pre-fix code aborts (SIGABRT) 5/5 runs when built with clang++ -std=c++17 -Wall -Wextra -O0. At -O2, the same pre-fix binary happened to pass 5/5 runs instead, because the optimizer picked 0 for the uninitialized value in that build — a coincidence of this compiler/flags, not a guarantee. That compiler/flag-dependent split is exactly why this is a real bug worth fixing rather than a false alarm: relying on an uninitialized read means the behavior can silently change with the toolchain.

Fix

  • Initialize ele = 0 so the empty-queue case has a defined return value, matching the sentinel already used by queue_using_array.cpp's dequeue() for the same situation.
  • Added braces around the previously brace-less if/else for clarity.
  • Added a tests() self-test (assert-based, matches the project's existing test style) that exercises enqueue/dequeue ordering and asserts the empty-queue return value both before any enqueue and after draining the queue. With the fix applied, the test passes consistently at both -O0 and -O2.

Checklist

  • Added description of change
  • File name matches guidelines (no new file added)
  • Added tests and example, test passes
  • Relevant documentation/comments added
  • PR title follows semantic commit guidelines
  • Searched for duplicate/prior work on this bug (none found)
  • I acknowledge that all my contributions will be made under the project's license.

dequeue() declared 'ele' without initializing it, so calling dequeue()
on an empty queue returned whatever garbage happened to be on the
stack instead of a defined value. Confirmed by clang's
-Wsometimes-uninitialized and reproduced at runtime.

Initialize ele to 0, matching the sentinel already used by
queue_using_array.cpp's dequeue() for the same empty-queue case, and
add a self-test that exercises enqueue/dequeue ordering plus the
empty-queue return value.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant