Fill an interior page with cells from children[*idx..child_count-2]. Updates cell_count, content_offset, ptr_offset, and *idx.
| 764 | // Fill an interior page with cells from children[*idx..child_count-2]. |
| 765 | // Updates cell_count, content_offset, ptr_offset, and *idx. |
| 766 | static void fill_interior_page(uint8_t *page, const PageRef *children, int child_count, |
| 767 | bool is_index, int *idx, int *cell_count, int *content_offset, |
| 768 | int *ptr_offset) { |
| 769 | while (*idx < child_count - SKIP_ONE) { |
| 770 | uint8_t tbuf[INTERIOR_CELL_BUF]; |
| 771 | uint8_t *heap_cell = NULL; |
| 772 | int clen = build_interior_cell(&children[*idx], is_index, tbuf, &heap_cell); |
| 773 | uint8_t *cell_data = heap_cell ? heap_cell : tbuf; |
| 774 | |
| 775 | int available = *content_offset - *ptr_offset - CELL_PTR_SIZE; |
| 776 | if (clen > available && *cell_count > 0) { |
| 777 | free(heap_cell); |
| 778 | /* Full with children[*idx] as this page's right child. If that |
| 779 | * leaves exactly ONE child (the last), the next page would hold |
| 780 | * zero cells and only a right-child pointer — SQLite reports |
| 781 | * SQLITE_CORRUPT when a cursor descends into such a page (the |
| 782 | * rightmost path, which a key sorting after every row takes; |
| 783 | * #1783). Give the last cell back so the next page gets one |
| 784 | * cell plus the last child as its right child. */ |
| 785 | if (*idx == child_count - 2 && *cell_count > SKIP_ONE) { |
| 786 | unplace_last_interior_cell(page, cell_count, content_offset, ptr_offset); |
| 787 | (*idx)--; |
| 788 | } |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | *content_offset -= clen; |
| 793 | memcpy(page + *content_offset, cell_data, clen); |
| 794 | put_u16(page + *ptr_offset, (uint16_t)*content_offset); |
| 795 | *ptr_offset += CELL_PTR_SIZE; |
| 796 | (*cell_count)++; |
| 797 | free(heap_cell); |
| 798 | (*idx)++; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | static uint32_t pb_build_interior(PageBuilder *pb, bool is_index) { |
| 803 | if (!pb->leaves) { |
no test coverage detected