Skip to content

Fix deque index wraparound overflow - #86

Closed
krotname wants to merge 1 commit into
mainfrom
codex/fix-deque-wraparound-overflow-issue
Closed

Fix deque index wraparound overflow#86
krotname wants to merge 1 commit into
mainfrom
codex/fix-deque-wraparound-overflow-issue

Conversation

@krotname

Copy link
Copy Markdown
Owner

Motivation

  • Prevent integer overflow in prev() for extremely large capacities where the expression (i-1+cap) % cap can overflow to a negative value and lead to ArrayIndexOutOfBoundsException during deque operations.

Description

  • Replace modulo-based wraparound in RingDeque.next and RingDeque.prev with branch-based increment/decrement logic (i++; return i == cap ? 0 : i; and i--; return i == -1 ? cap - 1 : i;) to preserve circular-buffer semantics without risking overflow.

Testing

  • Compiled and ran the code with javac -d /tmp/codewars-classes src/main/java/algorithms/sprint2/Deque.java, executed the internal test via java -cp /tmp/codewars-classes -Dos.name=Windows -ea algorithms.sprint2.Deque which printed Test OK, exercised the sample CLI input via printf '4\n4\npush_front 861\npush_front -819\npop_back\npop_back\n' | java -cp /tmp/codewars-classes algorithms.sprint2.Deque, and ran a reflection-based DequePrevCheck that verifies prev(1_073_741_824) with cap=1_073_741_825, and all automated checks succeeded.

Codex Task

@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@krotname

krotname commented Aug 1, 2026

Copy link
Copy Markdown
Owner Author

Superseded by the validated combined merge in #115: #115

@krotname krotname closed this Aug 1, 2026
@krotname
krotname deleted the codex/fix-deque-wraparound-overflow-issue branch August 1, 2026 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant