Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/section_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ ARTICLE *article_block_find_by_aid(int32_t aid)
while (left < right)
{
// get block offset no less than mid value of left and right block offsets
mid = (left + right) / 2 + (left + right) % 2;
// Use safe formula to avoid integer overflow: mid = ceil((left + right) / 2)
mid = left + (right - left + 1) / 2;

if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
{
Expand All @@ -393,7 +394,8 @@ ARTICLE *article_block_find_by_aid(int32_t aid)
// aid in the range [ aid of articles[left], aid of articles[right] ]
while (left < right)
{
mid = (left + right) / 2;
// Use safe formula to avoid integer overflow
mid = left + (right - left) / 2;

if (aid <= p_block->articles[mid].aid)
{
Expand Down Expand Up @@ -1217,7 +1219,8 @@ ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t
while (left < right)
{
// get page id no less than mid value of left page id and right page id
mid = (left + right) / 2 + (left + right) % 2;
// Use safe formula to avoid integer overflow: mid = ceil((left + right) / 2)
mid = left + (right - left + 1) / 2;

if (aid < p_section->p_page_first_article[mid]->aid)
{
Expand Down Expand Up @@ -1514,14 +1517,17 @@ int section_list_move_topic(SECTION_LIST *p_section_src, SECTION_LIST *p_section
{
p_section_src->p_article_tail = p_article->p_prior;
}
if (p_section_src->p_article_head == p_article) // || p_section_src->p_article_tail == p_article
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;
}

p_article->p_prior->p_next = p_article->p_next;
p_article->p_next->p_prior = p_article->p_prior;
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;
}

// Update sid of article
p_article->sid = p_section_dest->sid;
Expand Down