From 7db6ffa2267a210cd7bbffa0c55f68656e396234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Fri, 24 Apr 2020 15:35:29 -0300 Subject: Fix deleting first page corrupting page pointer --- page.c | 8 ++++++++ point.c | 11 +++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/page.c b/page.c index 1244df4..864623e 100644 --- a/page.c +++ b/page.c @@ -46,6 +46,13 @@ void split_page(struct page *back) { back->next = front; } +void copy_page(struct page *dest, struct page *src) { + memcpy(dest->elements, src->elements, PAGE_SIZE); + dest->gap_start = src->gap_start; + dest->gap_end = src->gap_end; + dest->element_count = src->element_count; +} + void free_page(struct page *page) { if (page->prev) { page->prev->next = page->next; @@ -53,6 +60,7 @@ void free_page(struct page *page) { if (page->next) { page->next->prev = page->prev; } + free(page->elements); free(page); } diff --git a/point.c b/point.c index 7977a4e..27d62be 100644 --- a/point.c +++ b/point.c @@ -35,8 +35,8 @@ void move_point_backward(struct point *point) { if (point->index > 1) { point->index--; } else if (point->page->prev) { - point->index = point->page->prev->element_count; point->page = point->page->prev; + point->index = point->page->element_count; } else { point->index = 0; } @@ -65,15 +65,14 @@ void insert_at_point(struct point *point, uint8_t c) { } void delete_at_point(struct point *point) { - if (point->page->element_count == 1) { + if (point->page->element_count == 1 && point->index == 1) { if (point->page->prev) { - point->page = point->page->prev; - point->index = point->page->element_count; + move_point_backward(point); free_page(point->page->next); } else if (point->page->next) { - point->page = point->page->next; + copy_page(point->page, point->page->next); + free_page(point->page->next); point->index = 0; - free_page(point->page->prev); } else { align_gap(point); delete_at_gap(point->page); -- cgit v1.2.3