Skip to content

Conversation

@0xfullex
Copy link
Contributor

@0xfullex 0xfullex commented Jan 6, 2026

Summary

  • Fix integer overflow in binary search calculations
  • Fix linked list removal for single-element list edge case

Changes

Binary Search Integer Overflow Fix

Changed the mid-point calculation formula in three binary search locations to prevent integer overflow:

Before (vulnerable to overflow):

mid = (left + right) / 2;
mid = (left + right) / 2 + (left + right) % 2;

After (safe):

mid = left + (right - left) / 2;
mid = left + (right - left + 1) / 2;  // for ceiling division

Affected functions:

  • article_block_find_by_aid() - two binary search loops
  • section_list_find_article_with_offset() - one binary search loop

Linked List Boundary Condition Fix

In section_list_move_topic(), when removing an article from a single-element list:

Before (bug):

// After setting head/tail to NULL, still tried to update neighbor pointers
p_article->p_prior->p_next = p_article->p_next;
p_article->p_next->p_prior = p_article->p_prior;

After (fixed):

if (p_section_src->p_article_head == p_article) // Single element list
{
    p_section_src->p_article_head = NULL;
    p_section_src->p_article_tail = NULL;
}
else
{
    // Only update neighbor pointers if list is not empty after removal
    p_article->p_prior->p_next = p_article->p_next;
    p_article->p_next->p_prior = p_article->p_prior;
}

Test Plan

  • Compile verification
  • Test with large article counts to verify binary search
  • Test moving topics from single-article sections

🤖 Generated with Claude Code

- Fix integer overflow in binary search by using safe formula:
  `mid = left + (right - left) / 2` instead of `(left + right) / 2`
- Fix linked list removal in section_list_move_topic() to handle
  single-element list correctly by checking if list becomes empty
  before updating neighbor pointers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
leafok88 added a commit that referenced this pull request Jan 7, 2026
@leafok88 leafok88 merged commit 1fea979 into leafok:main Jan 7, 2026
2 checks passed
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.

2 participants