summaryrefslogtreecommitdiff
path: root/page.c
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-04-23 06:47:13 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2020-04-23 06:47:13 -0300
commit388f43addc4a94681f27853defe9f46ec1b9ee63 (patch)
treef6ca6782e4810b13a5fd4c66f9452f1bc78defc1 /page.c
parent6947a5feb7c21c454eb1f297cc0b792e21b564c6 (diff)
downloadjet-388f43addc4a94681f27853defe9f46ec1b9ee63.tar.gz
jet-388f43addc4a94681f27853defe9f46ec1b9ee63.zip
Fix page going out of bounds on move and insert
Diffstat (limited to 'page.c')
-rw-r--r--page.c26
1 files changed, 16 insertions, 10 deletions
diff --git a/page.c b/page.c
index e084705..1244df4 100644
--- a/page.c
+++ b/page.c
@@ -57,25 +57,31 @@ void free_page(struct page *page) {
}
void move_gap_forward(struct page *page) {
- page->elements[page->gap_start] = page->elements[page->gap_end];
- page->gap_start++;
- page->gap_end++;
+ if (page->gap_end < PAGE_SIZE) {
+ page->elements[page->gap_start] = page->elements[page->gap_end];
+ page->gap_start++;
+ page->gap_end++;
+ }
}
void move_gap_backward(struct page *page) {
- page->gap_end--;
- page->gap_start--;
- page->elements[page->gap_end] = page->elements[page->gap_start];
+ if (page->gap_start > 0) {
+ page->gap_end--;
+ page->gap_start--;
+ page->elements[page->gap_end] = page->elements[page->gap_start];
+ }
}
void insert_at_gap(struct page *page, uint8_t c) {
- page->elements[page->gap_start] = c;
- page->gap_start++;
- page->element_count++;
+ if (page->element_count < PAGE_SIZE) {
+ page->elements[page->gap_start] = c;
+ page->gap_start++;
+ page->element_count++;
+ }
}
void delete_at_gap(struct page *page) {
- if (page->gap_start) {
+ if (page->gap_start > 0) {
page->gap_start--;
page->element_count--;
}