diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/ex_cmds.c | 10 | ||||
-rw-r--r-- | src/nvim/regexp.c | 5 |
2 files changed, 10 insertions, 5 deletions
diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index f017550dd4..a0618ce7d7 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -3953,15 +3953,19 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const long cmdpreview_ p1 = ml_get(sub_firstlnum + (linenr_T)nmatch - 1); nmatch_tl += nmatch - 1; } - size_t copy_len = (size_t)(regmatch.startpos[0].col - copycol); + int copy_len = regmatch.startpos[0].col - copycol; new_end = sub_grow_buf(&new_start, &new_start_len, (colnr_T)strlen(p1) - regmatch.endpos[0].col - + (colnr_T)copy_len + sublen + 1); + + copy_len + sublen + 1); // copy the text up to the part that matched - memmove(new_end, sub_firstline + copycol, copy_len); + memmove(new_end, sub_firstline + copycol, (size_t)copy_len); new_end += copy_len; + if (new_start_len - copy_len < sublen) { + sublen = new_start_len - copy_len - 1; + } + // Finally, at this point we can know where the match actually will // start in the new text int start_col = (int)(new_end - new_start); diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index d5d0f3346f..1d0a987780 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -1765,9 +1765,10 @@ static int vim_regsub_both(char *source, typval_T *expr, char *dest, int destlen // "flags & REGSUB_COPY" == 0 to the call with // "flags & REGSUB_COPY" != 0. if (copy) { - if (eval_result[nested] != NULL) { + size_t reslen = eval_result[nested] != NULL ? strlen(eval_result[nested]) : 0; + if (eval_result[nested] != NULL && reslen < (size_t)destlen) { STRCPY(dest, eval_result[nested]); - dst += strlen(eval_result[nested]); + dst += reslen; XFREE_CLEAR(eval_result[nested]); } } else { |