From 50818e7001d2cc198f31feaa7901e88f1e1c1a97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 28 Sep 2020 16:32:14 +0200 Subject: text: avoid direct access to txt->blocks in I/O related code --- text.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'text.c') diff --git a/text.c b/text.c index 488017b..fa1aa86 100644 --- a/text.c +++ b/text.c @@ -173,6 +173,7 @@ static void lineno_cache_invalidate(LineCache *cache); static size_t lines_skip_forward(Text *txt, size_t pos, size_t lines, size_t *lines_skiped); static size_t lines_count(Text *txt, size_t pos, size_t len); static void text_saved(Text*, struct stat *meta); +static Block *text_block_mmaped(Text*); static ssize_t write_all(int fd, const char *buf, size_t count) { size_t rem = count; @@ -993,9 +994,8 @@ static bool text_save_begin_inplace(TextSave *ctx) { if (fstat(ctx->fd, &now) == -1) goto err; struct stat loaded = text_stat(txt); - Block *block = array_get_ptr(&txt->blocks, 0); - if (now.st_dev == loaded.st_dev && now.st_ino == loaded.st_ino && - block && block->type == MMAP_ORIG && block->size) { + Block *block = text_block_mmaped(txt); + if (block && now.st_dev == loaded.st_dev && now.st_ino == loaded.st_ino) { /* The file we are going to overwrite is currently mmap-ed from * text_load, therefore we copy the mmap-ed block to a temporary * file and remap it at the same position such that all pointers @@ -1229,6 +1229,13 @@ static void text_saved(Text *txt, struct stat *meta) { text_snapshot(txt); } +static Block *text_block_mmaped(Text *txt) { + Block *block = array_get_ptr(&txt->blocks, 0); + if (block && block->type == MMAP_ORIG && block->size) + return block; + return NULL; +} + /* A delete operation can either start/stop midway through a piece or at * a boundry. In the former case a new piece is created to represent the * remaining text before/after the modification point. -- cgit v1.2.3