diff options
Diffstat (limited to 'src/nvim/undo.c')
-rw-r--r-- | src/nvim/undo.c | 109 |
1 files changed, 51 insertions, 58 deletions
diff --git a/src/nvim/undo.c b/src/nvim/undo.c index 9a3da5bcdb..59920cfbe1 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -82,6 +82,7 @@ #include <assert.h> #include <inttypes.h> +#include <limits.h> #include <errno.h> #include <stdbool.h> #include <string.h> @@ -569,7 +570,7 @@ int u_savecommon(linenr_T top, linenr_T bot, linenr_T newbot, int reload) } if (size > 0) { - uep->ue_array = xmalloc(sizeof(char_u *) * size); + uep->ue_array = xmalloc(sizeof(char_u *) * (size_t)size); for (i = 0, lnum = top + 1; i < size; ++i) { fast_breakcheck(); if (got_int) { @@ -638,7 +639,6 @@ char_u *u_get_undo_file_name(char_u *buf_ffname, int reading) char_u dir_name[IOSIZE + 1]; char_u *munged_name = NULL; char_u *undo_file_name = NULL; - int dir_len; char_u *p; char_u *ffname = buf_ffname; #ifdef HAVE_READLINK @@ -659,11 +659,11 @@ char_u *u_get_undo_file_name(char_u *buf_ffname, int reading) * When not reading use the first directory that exists or ".". */ dirp = p_udir; while (*dirp != NUL) { - dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ","); + size_t dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ","); if (dir_len == 1 && dir_name[0] == '.') { /* Use same directory as the ffname, * "dir/name" -> "dir/.name.un~" */ - undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5)); + undo_file_name = vim_strnsave(ffname, STRLEN(ffname) + 5); p = path_tail(undo_file_name); memmove(p + 1, p, STRLEN(p) + 1); *p = '.'; @@ -715,42 +715,40 @@ static void u_free_uhp(u_header_T *uhp) static int serialize_header(FILE *fp, buf_T *buf, char_u *hash) { - int len; - /* Start writing, first the magic marker and undo info version. */ - if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1) + if (fwrite(UF_START_MAGIC, UF_START_MAGIC_LEN, 1, fp) != 1) return FAIL; - put_bytes(fp, (long_u)UF_VERSION, 2); + put_bytes(fp, UF_VERSION, 2); /* Write a hash of the buffer text, so that we can verify it is still the * same when reading the buffer text. */ - if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1) + if (fwrite(hash, UNDO_HASH_SIZE, 1, fp) != 1) return FAIL; /* buffer-specific data */ - put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4); - len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0; - put_bytes(fp, (long_u)len, 4); + put_bytes(fp, (uintmax_t)buf->b_ml.ml_line_count, 4); + size_t len = buf->b_u_line_ptr ? STRLEN(buf->b_u_line_ptr) : 0; + put_bytes(fp, len, 4); if (len > 0 && fwrite(buf->b_u_line_ptr, len, 1, fp) != 1) return FAIL; - put_bytes(fp, (long_u)buf->b_u_line_lnum, 4); - put_bytes(fp, (long_u)buf->b_u_line_colnr, 4); + put_bytes(fp, (uintmax_t)buf->b_u_line_lnum, 4); + put_bytes(fp, (uintmax_t)buf->b_u_line_colnr, 4); /* Undo structures header data */ put_header_ptr(fp, buf->b_u_oldhead); put_header_ptr(fp, buf->b_u_newhead); put_header_ptr(fp, buf->b_u_curhead); - put_bytes(fp, (long_u)buf->b_u_numhead, 4); - put_bytes(fp, (long_u)buf->b_u_seq_last, 4); - put_bytes(fp, (long_u)buf->b_u_seq_cur, 4); + put_bytes(fp, (uintmax_t)buf->b_u_numhead, 4); + put_bytes(fp, (uintmax_t)buf->b_u_seq_last, 4); + put_bytes(fp, (uintmax_t)buf->b_u_seq_cur, 4); put_time(fp, buf->b_u_time_cur); /* Optional fields. */ putc(4, fp); putc(UF_LAST_SAVE_NR, fp); - put_bytes(fp, (long_u)buf->b_u_save_nr_last, 4); + put_bytes(fp, (uintmax_t)buf->b_u_save_nr_last, 4); putc(0, fp); /* end marker */ @@ -759,22 +757,19 @@ static int serialize_header(FILE *fp, buf_T *buf, char_u *hash) static int serialize_uhp(FILE *fp, buf_T *buf, u_header_T *uhp) { - int i; - u_entry_T *uep; - - if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL) + if (put_bytes(fp, UF_HEADER_MAGIC, 2) == FAIL) return FAIL; put_header_ptr(fp, uhp->uh_next.ptr); put_header_ptr(fp, uhp->uh_prev.ptr); put_header_ptr(fp, uhp->uh_alt_next.ptr); put_header_ptr(fp, uhp->uh_alt_prev.ptr); - put_bytes(fp, uhp->uh_seq, 4); + put_bytes(fp, (uintmax_t)uhp->uh_seq, 4); serialize_pos(uhp->uh_cursor, fp); - put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4); - put_bytes(fp, (long_u)uhp->uh_flags, 2); + put_bytes(fp, (uintmax_t)uhp->uh_cursor_vcol, 4); + put_bytes(fp, (uintmax_t)uhp->uh_flags, 2); /* Assume NMARKS will stay the same. */ - for (i = 0; i < NMARKS; ++i) + for (size_t i = 0; i < NMARKS; ++i) serialize_pos(uhp->uh_namedm[i], fp); serialize_visualinfo(&uhp->uh_visual, fp); put_time(fp, uhp->uh_time); @@ -782,17 +777,17 @@ static int serialize_uhp(FILE *fp, buf_T *buf, u_header_T *uhp) /* Optional fields. */ putc(4, fp); putc(UHP_SAVE_NR, fp); - put_bytes(fp, (long_u)uhp->uh_save_nr, 4); + put_bytes(fp, (uintmax_t)uhp->uh_save_nr, 4); putc(0, fp); /* end marker */ /* Write all the entries. */ - for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next) { - put_bytes(fp, (long_u)UF_ENTRY_MAGIC, 2); + for (u_entry_T *uep = uhp->uh_entry; uep; uep = uep->ue_next) { + put_bytes(fp, UF_ENTRY_MAGIC, 2); if (serialize_uep(fp, buf, uep) == FAIL) return FAIL; } - put_bytes(fp, (long_u)UF_ENTRY_END_MAGIC, 2); + put_bytes(fp, UF_ENTRY_END_MAGIC, 2); return OK; } @@ -875,16 +870,13 @@ static u_header_T *unserialize_uhp(FILE *fp, char_u *file_name) */ static int serialize_uep(FILE *fp, buf_T *buf, u_entry_T *uep) { - int i; - size_t len; - - put_bytes(fp, (long_u)uep->ue_top, 4); - put_bytes(fp, (long_u)uep->ue_bot, 4); - put_bytes(fp, (long_u)uep->ue_lcount, 4); - put_bytes(fp, (long_u)uep->ue_size, 4); - for (i = 0; i < uep->ue_size; ++i) { - len = STRLEN(uep->ue_array[i]); - if (put_bytes(fp, (long_u)len, 4) == FAIL) + put_bytes(fp, (uintmax_t)uep->ue_top, 4); + put_bytes(fp, (uintmax_t)uep->ue_bot, 4); + put_bytes(fp, (uintmax_t)uep->ue_lcount, 4); + put_bytes(fp, (uintmax_t)uep->ue_size, 4); + for (size_t i = 0; i < (size_t)uep->ue_size; ++i) { + size_t len = STRLEN(uep->ue_array[i]); + if (put_bytes(fp, len, 4) == FAIL) return FAIL; if (len > 0 && fwrite(uep->ue_array[i], len, 1, fp) != 1) return FAIL; @@ -910,8 +902,8 @@ static u_entry_T *unserialize_uep(FILE *fp, int *error, char_u *file_name) uep->ue_lcount = get4c(fp); uep->ue_size = get4c(fp); if (uep->ue_size > 0) { - array = xmalloc(sizeof(char_u *) * uep->ue_size); - memset(array, 0, sizeof(char_u *) * uep->ue_size); + array = xmalloc(sizeof(char_u *) * (size_t)uep->ue_size); + memset(array, 0, sizeof(char_u *) * (size_t)uep->ue_size); } else array = NULL; uep->ue_array = array; @@ -938,9 +930,9 @@ static u_entry_T *unserialize_uep(FILE *fp, int *error, char_u *file_name) */ static void serialize_pos(pos_T pos, FILE *fp) { - put_bytes(fp, (long_u)pos.lnum, 4); - put_bytes(fp, (long_u)pos.col, 4); - put_bytes(fp, (long_u)pos.coladd, 4); + put_bytes(fp, (uintmax_t)pos.lnum, 4); + put_bytes(fp, (uintmax_t)pos.col, 4); + put_bytes(fp, (uintmax_t)pos.coladd, 4); } /* @@ -966,8 +958,8 @@ static void serialize_visualinfo(visualinfo_T *info, FILE *fp) { serialize_pos(info->vi_start, fp); serialize_pos(info->vi_end, fp); - put_bytes(fp, (long_u)info->vi_mode, 4); - put_bytes(fp, (long_u)info->vi_curswant, 4); + put_bytes(fp, (uintmax_t)info->vi_mode, 4); + put_bytes(fp, (uintmax_t)info->vi_curswant, 4); } /* @@ -987,7 +979,7 @@ static void unserialize_visualinfo(visualinfo_T *info, FILE *fp) * pointers when reading. */ static void put_header_ptr(FILE *fp, u_header_T *uhp) { - put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4); + put_bytes(fp, (uintmax_t)(uhp != NULL ? uhp->uh_seq : 0), 4); } /* @@ -1061,9 +1053,7 @@ void u_write_undo(char_u *name, int forceit, buf_T *buf, char_u *hash) goto theend; } else { char_u mbuf[UF_START_MAGIC_LEN]; - int len; - - len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN); + ssize_t len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN); close(fd); if (len < UF_START_MAGIC_LEN || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0) { @@ -1121,7 +1111,7 @@ void u_write_undo(char_u *name, int forceit, buf_T *buf, char_u *hash) && os_fileinfo((char *)buf->b_ffname, &file_info_old) && os_fileinfo((char *)file_name, &file_info_new) && file_info_old.stat.st_gid != file_info_new.stat.st_gid - && os_fchown(fd, -1, file_info_old.stat.st_gid) != 0) { + && os_fchown(fd, (uv_uid_t)-1, (uv_gid_t)file_info_old.stat.st_gid)) { os_setperm(file_name, (perm & 0707) | ((perm & 07) << 3)); } # ifdef HAVE_SELINUX @@ -1178,7 +1168,7 @@ void u_write_undo(char_u *name, int forceit, buf_T *buf, char_u *hash) uhp = uhp->uh_next.ptr; } - if (put_bytes(fp, (long_u)UF_HEADER_END_MAGIC, 2) == OK) + if (put_bytes(fp, UF_HEADER_END_MAGIC, 2) == OK) write_ok = true; #ifdef U_DEBUG if (headers_written != buf->b_u_numhead) { @@ -1358,7 +1348,7 @@ void u_read_undo(char_u *name, char_u *hash, char_u *orig_name) * sequence numbers of the headers. * When there are no headers uhp_table is NULL. */ if (num_head > 0) { - uhp_table = xmalloc(num_head * sizeof(u_header_T *)); + uhp_table = xmalloc((size_t)num_head * sizeof(u_header_T *)); } while ((c = get2c(fp)) == UF_HEADER_MAGIC) { @@ -1433,15 +1423,18 @@ void u_read_undo(char_u *name, char_u *hash, char_u *orig_name) break; } if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq) { - old_idx = i; + assert(i <= SHRT_MAX); + old_idx = (short)i; SET_FLAG(i); } if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq) { - new_idx = i; + assert(i <= SHRT_MAX); + new_idx = (short)i; SET_FLAG(i); } if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq) { - cur_idx = i; + assert(i <= SHRT_MAX); + cur_idx = (short)i; SET_FLAG(i); } } @@ -1993,7 +1986,7 @@ static void u_undoredo(int undo) /* delete the lines between top and bot and save them in newarray */ if (oldsize > 0) { - newarray = xmalloc(sizeof(char_u *) * oldsize); + newarray = xmalloc(sizeof(char_u *) * (size_t)oldsize); /* delete backwards, it goes faster in most cases */ for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum) { /* what can we do when we run out of memory? */ |