fix: avoid uninitialized read in queue_using_linkedlist dequeue() - #3238
Open
Saadanjum0 wants to merge 1 commit into
Open
Saadanjum0 wants to merge 1 commit into
Saadanjum0 wants to merge 1 commit into
Conversation
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.
Saadanjum0
requested review from
Panquesito7 and
realstealthninja
as code owners
October 2, 2026 12:49
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Change
dequeue()indata_structures/queue_using_linkedlist.cppdeclaredint ele;without initializing it. When the queue is empty, the function only printsStack is emptyand falls straight through toreturn ele;, returning an undefined value instead of whatever is intended for the empty-queue case. This is undefined behavior, confirmed directly by clang:Because it's undefined behavior, the observed symptom depends on the compiler and optimization level rather than being one fixed value. At
-O0the uninitialized read reliably surfaces as a crash: the project's existing assert-based self-test style exercisesdequeue()on an empty queue, and the pre-fix code aborts (SIGABRT) 5/5 runs when built withclang++ -std=c++17 -Wall -Wextra -O0. At-O2, the same pre-fix binary happened to pass 5/5 runs instead, because the optimizer picked0for 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
ele = 0so the empty-queue case has a defined return value, matching the sentinel already used byqueue_using_array.cpp'sdequeue()for the same situation.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-O0and-O2.Checklist