summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-28 00:30:30 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-28 00:30:30 -0600
commit56bd0e6c2ab87ab140cef35882cff52153f36b7c (patch)
treeca97d8d3c64a1dc2d4afbce14e157505d7642949
parenta5a920383fe85f4dada0cb80418f7a935c12dc4a (diff)
downloadzash-56bd0e6c2ab87ab140cef35882cff52153f36b7c.tar.gz
fix(edit): vi `y` keeps the line and never pastes the old kill
`y0` with the cursor at column 0 yanks an empty range, and the old code then inserted the kill buffer at the cursor: with nothing killed yet that was memcpy(dst, nullptr, 0), which UBSan rejects at src/edit_builtin.c:375; with an earlier `cw` behind it, the old kill text was pasted into the line (vi `y` changes nothing on the line). The `y` case now copies the range into the kill buffer with kill_set() - the same block remove_range() used - and puts the cursor at the start of the range, which is where vi leaves it. insert() also returns early for an empty string, so a later caller cannot repeat the null memcpy. Test: tests/linux/i-vi.sh yanks an empty range after a `cw`, so the kill buffer holds text that must not appear. Before the change UBSan reports "null pointer passed as argument 2" and the line comes out as "oneecho yank"; after it, the line is "echo yank". Verified on Linux with make check and the same pty session under ASan+UBSan.
-rw-r--r--src/edit_builtin.c30
-rw-r--r--tests/linux/i-vi.exp10
-rw-r--r--tests/linux/i-vi.sh4
3 files changed, 29 insertions, 15 deletions
diff --git a/src/edit_builtin.c b/src/edit_builtin.c
index 4d5cfb1..75dd31e 100644
--- a/src/edit_builtin.c
+++ b/src/edit_builtin.c
@@ -370,6 +370,8 @@ static void reserve(size_t extra) {
* @param n How many.
*/
static void insert(const char *s, size_t n) {
+ if (!n) /* `y` over an empty range has no kill buffer to paste */
+ return;
reserve(n);
memmove(ed.buf + ed.pos + n, ed.buf + ed.pos, ed.len - ed.pos);
memcpy(ed.buf + ed.pos, s, n);
@@ -377,6 +379,16 @@ static void insert(const char *s, size_t n) {
ed.pos += n;
}
+/** @brief Make the bytes in [from, to) the kill buffer. */
+static void kill_set(size_t from, size_t to) {
+ if (from >= to)
+ return;
+ free(ed.kill);
+ ed.kill_len = to - from;
+ ed.kill = xmalloc(ed.kill_len);
+ memcpy(ed.kill, ed.buf + from, ed.kill_len);
+}
+
/**
* @brief Remove the bytes in [from, to) and put the cursor at `from`.
* @param from First byte to remove.
@@ -386,12 +398,8 @@ static void insert(const char *s, size_t n) {
static void remove_range(size_t from, size_t to, bool save) {
if (from >= to)
return;
- if (save) {
- free(ed.kill);
- ed.kill_len = to - from;
- ed.kill = xmalloc(ed.kill_len);
- memcpy(ed.kill, ed.buf + from, ed.kill_len);
- }
+ if (save)
+ kill_set(from, to);
memmove(ed.buf + from, ed.buf + to, ed.len - to);
ed.len -= to - from;
ed.pos = from;
@@ -1281,11 +1289,11 @@ static bool vi_command(int key) {
}
}
if (key == 'y') {
- size_t keep = ed.pos;
- vi_save_undo();
- remove_range(from, to, true);
- insert(ed.kill, ed.kill_len);
- ed.pos = keep < from ? keep : from;
+ /* Yank: the text becomes the kill buffer and stays put. Going
+ * through remove_range() and inserting it back pasted an older
+ * kill buffer when the range was empty (`y0` at column 0). */
+ kill_set(from, to);
+ ed.pos = from;
break;
}
vi_save_undo();
diff --git a/tests/linux/i-vi.exp b/tests/linux/i-vi.exp
index d9b4e7d..91ef7c6 100644
--- a/tests/linux/i-vi.exp
+++ b/tests/linux/i-vi.exp
@@ -19,18 +19,20 @@ UPPER
$ echo BYE world
BYE world
$ ech count
-/home/skurtz/src/zash/zash: ech: not found
+SHELL: ech: not found
$ #echo comment
$ echo done
done
$ echo def
def
$ two three
-/home/skurtz/src/zash/zash: two: not found
+SHELL: two: not found
$ ZZ ZZ two three
-/home/skurtz/src/zash/zash: ZZ: not found
+SHELL: ZZ: not found
+$ echo yank
+yank
$ abcdef
-/home/skurtz/src/zash/zash: abcdef: not found
+SHELL: abcdef: not found
$ echo bdef
bdef
$ exit
diff --git a/tests/linux/i-vi.sh b/tests/linux/i-vi.sh
index 2279886..348d14e 100644
--- a/tests/linux/i-vi.sh
+++ b/tests/linux/i-vi.sh
@@ -32,6 +32,10 @@ send echo one two three\x1b0dw.\r
expect two three
send echo one two three\x1b0cwZZ\x1bw.\r
expect ZZ ZZ two three
+# `y` over an empty range, with nothing killed yet: nothing is pasted and
+# no null buffer reaches memcpy (ASan sees that one).
+send \x15echo yank\x1b0y0\r
+expect echo yank
# ... and a count repeats it that many times, on top of the change's own
send echo abcdef\x1b0x3.\r
expect abcdef