vim-patch:9.1.1222: using wrong length for last inserted string (#32975)

Problem:  using wrong length for last inserted string
          (Christ van Willegen, after v9.1.1212)
Solution: use the correct length in get_last_insert_save(), make
          get_last_insert() return a string_T (John Marriott)

closes: vim/vim#16921

8ac0f73eb1

N/A patches:
vim-patch:9.1.1129: missing out-of-memory test in buf_write()
vim-patch:9.1.1218: missing out-of-memory check in filepath.c

Co-authored-by: John Marriott <basilisk@internode.on.net>
This commit is contained in:
zeertzjq
2025-03-19 07:08:39 +08:00
committed by GitHub
parent 62d9fab9af
commit 4d83649d10
2 changed files with 21 additions and 28 deletions

View File

@@ -2696,8 +2696,8 @@ int stuff_inserted(int c, int count, int no_esc)
{
char last = NUL;
String *insert = get_last_insert(); // text to be inserted
if (insert->data == NULL) {
String insert = get_last_insert(); // text to be inserted
if (insert.data == NULL) {
emsg(_(e_noinstext));
return FAIL;
}
@@ -2707,30 +2707,30 @@ int stuff_inserted(int c, int count, int no_esc)
stuffcharReadbuff(c);
}
if (insert->size > 0) {
if (insert.size > 0) {
// look for the last ESC in 'insert'
for (char *p = insert->data + insert->size - 1; p >= insert->data; p--) {
for (char *p = insert.data + insert.size - 1; p >= insert.data; p--) {
if (*p == ESC) {
insert->size = (size_t)(p - insert->data);
insert.size = (size_t)(p - insert.data);
break;
}
}
}
if (insert->size > 0) {
char *p = insert->data + insert->size - 1;
if (insert.size > 0) {
char *p = insert.data + insert.size - 1;
// when the last char is either "0" or "^" it will be quoted if no ESC
// comes after it OR if it will inserted more than once and "ptr"
// starts with ^D. -- Acevedo
if ((*p == '0' || *p == '^')
&& (no_esc || (*insert->data == Ctrl_D && count > 1))) {
&& (no_esc || (*insert.data == Ctrl_D && count > 1))) {
last = *p;
insert->size--;
insert.size--;
}
}
do {
stuffReadbuffLen(insert->data, (ptrdiff_t)insert->size);
stuffReadbuffLen(insert.data, (ptrdiff_t)insert.size);
// A trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^".
switch (last) {
case '0':
@@ -2752,36 +2752,28 @@ int stuff_inserted(int c, int count, int no_esc)
return OK;
}
String *get_last_insert(void)
String get_last_insert(void)
FUNC_ATTR_PURE
{
static String insert = STRING_INIT;
insert = last_insert.data == NULL ? NULL_STRING : (String){
insert.data = last_insert.data + last_insert_skip,
insert.size = last_insert.size - (size_t)last_insert_skip,
return last_insert.data == NULL ? NULL_STRING : (String){
.data = last_insert.data + last_insert_skip,
.size = last_insert.size - (size_t)last_insert_skip,
};
return &insert;
}
// Get last inserted string, and remove trailing <Esc>.
// Returns pointer to allocated memory (must be freed) or NULL.
char *get_last_insert_save(void)
{
String *insert = get_last_insert();
String insert = get_last_insert();
if (insert->data == NULL) {
if (insert.data == NULL) {
return NULL;
}
char *s = xmemdupz(insert->data, insert->size);
if (insert->size > 0) {
// remain trailing ESC
insert->size--;
if (s[insert->size] == ESC) {
s[insert->size] = NUL;
}
char *s = xmemdupz(insert.data, insert.size);
if (insert.size > 0 && s[insert.size - 1] == ESC) { // remain trailing ESC
s[insert.size - 1] = NUL;
}
return s;
}

View File

@@ -3849,7 +3849,8 @@ void ex_display(exarg_T *eap)
}
// display last inserted text
if ((p = get_last_insert()->data) != NULL
String insert = get_last_insert();
if ((p = insert.data) != NULL
&& (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int
&& !message_filtered(p)) {
msg_puts("\n c \". ");