From eeba0dc2ed4eecaf40014217fa7e0eabfef992af Mon Sep 17 00:00:00 2001 From: glepnir Date: Sat, 23 Aug 2025 05:50:30 +0800 Subject: [PATCH] fix: memory leak from double buffer initialization in read_stdin #35413 Problem: When stdin follows files, set_curbuf() initializes buffer via enter_buffer(), then open_buffer() initializes again, causing memory leaks. Solution: Use readfile() directly for new buffers already initialized by enter_buffer(), only call open_buffer() for original buffer case. --- src/nvim/main.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/nvim/main.c b/src/nvim/main.c index b91c27349e..3bed6964ac 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1627,7 +1627,7 @@ static void read_stdin(void) if (curbuf->b_ffname) { // curbuf is already opened for a file, create a new buffer for stdin. #35269 - buf_T *newbuf = buflist_new(NULL, NULL, 0, 0); + buf_T *newbuf = buflist_new(NULL, NULL, 0, BLN_LISTED); if (newbuf == NULL) { semsg("Failed to create buffer for stdin"); return; @@ -1636,11 +1636,13 @@ static void read_stdin(void) // remember the current buffer so we can go back to it prev_buf = curbuf; set_curbuf(newbuf, 0, false); + readfile(NULL, NULL, 0, 0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN, true); + } else { + set_buflisted(true); + // Create memfile and read from stdin. + open_buffer(true, NULL, 0); } - set_buflisted(true); - // Create memfile and read from stdin. - open_buffer(true, NULL, 0); if (buf_is_empty(curbuf)) { // stdin was empty so we should wipe it (e.g. "echo file1 | xargs nvim"). #8561 // stdin buffer may be first or last ("echo foo | nvim file1 -"). #35269