Repository: neovim/neovim
File: src/nvim/main.c (C)

Code before:
  swap_exists_action = SEA_DIALOG;
  no_wait_return = true;
  bool save_msg_didany = msg_didany;
  set_buflisted(true);
  // Create memfile and read from stdin.
  open_buffer(true, NULL, 0);
  if (buf_is_empty(curbuf) && curbuf->b_next != NULL) {
    // stdin was empty, go to buffer 2 (e.g. "echo file1 | xargs nvim"). #8561
    do_cmdline_cmd("silent! bnext");
    // Delete the empty stdin buffer.
    do_cmdline_cmd("bwipeout 1");
  }
  no_wait_return = false;
  msg_didany = save_msg_didany;
  TIME_MSG("reading stdin");
  check_swap_exists_action();
}

// Create the requested number of windows and edit buffers in them.
// Also does recovery if "recoverymode" set.
static void create_windows(mparm_T *parmp)
{
  // Create the number of windows that was requested.
  if (parmp->window_count == -1) {      // was not set
    parmp->window_count = 1;
  }
  if (parmp->window_count == 0) {
    parmp->window_count = GARGCOUNT;
  }
  if (parmp->window_count > 1) {
    // Don't change the windows if there was a command in vimrc that
    // already split some windows
    if (parmp->window_layout == 0) {
      parmp->window_layout = WIN_HOR;
    }
    if (parmp->window_layout == WIN_TABS) {
      parmp->window_count = make_tabpages(parmp->window_count);
      TIME_MSG("making tab pages");
    } else if (firstwin->w_next == NULL || firstwin->w_next->w_floating) {
      parmp->window_count = make_windows(parmp->window_count, parmp->window_layout == WIN_VER);
      TIME_MSG("making windows");
    } else {
      parmp->window_count = win_count();
    }
  } else {
    parmp->window_count = 1;
  }

  if (recoverymode) {                   // do recover
    msg_scroll = true;                  // scroll message up
    ml_recover(true);

Review comment (bug):
asan failure. this is one reason why the old code was using `do_cmdline_cmd`

```
= ==13649==ERROR: LeakSanitizer: detected memory leaks
= 
= Direct leak of 176 byte(s) in 1 object(s) allocated from:
=     #0 0x564318bc0da3 in malloc (/home/runner/work/neovim/neovim/build/bin/nvim+0xd1eda3) (BuildId: 94f2d704be41a838937af8c98dba131aa568a09b)
=     #1 0x564319852584 in try_malloc /home/runner/work/neovim/neovim/src/nvim/memory.c:113:15
=     #2 0x5643198527e4 in xmalloc /home/runner/work/neovim/neovim/src/nvim/memory.c:147:15
=     #3 0x564319800ac2 in mf_open /home/runner/work/neovim/neovim/src/nvim/memfile.c:89:20
=     #4 0x56431980c253 in ml_open /home/runner/work/neovim/neovim/src/nvim/memline.c:310:20
=     #5 0x564318e24d70 in open_buffer /home/runner/work/neovim/neovim/src/nvim/buffer.c:230:7
=     #6 0x564318e2cb84 in enter_buffer /home/runner/work/neovim/neovim/src/nvim/buffer.c:1741:5
=     #7 0x564318e428e2 in set_curbuf /home/runner/work/neovim/neovim/src/nvim/buffer.c:1676:5
=     #8 0x5643196bc78f in read_stdin /home/runner/work/neovim/neovim/src/nvim/main.c:1638:5
=     #9 0x5643196ac8dc in main /home/runner/work/neovim/neovim/src/nvim/main.c:529:5
=     #10 0x7f67e2c2a1c9 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
=     #11 0x7f67e2c2a28a in __libc_start_main csu/../csu/libc-start.c:360:3
=     #12 0x564318b20f74 in _start (/home/runner/work/neovim/neovim/build/bin/nvim+0xc7ef74) (BuildId: 94f2d704be41a838937af8c98dba131aa568a09b)
```

Code after:
  swap_exists_action = SEA_DIALOG;
  no_wait_return = true;
  bool save_msg_didany = msg_didany;
  buf_T *prev_buf = NULL;

  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);
    if (newbuf == NULL) {
      semsg("Failed to create buffer for stdin");
      return;
    }

    // remember the current buffer so we can go back to it
    prev_buf = curbuf;
    set_curbuf(newbuf, 0, false);
  }

  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
    if ((curbuf->b_next != NULL) || (curbuf->b_prev != NULL)) {
      do_bufdel(DOBUF_WIPE, NULL, 0, 0, 0, 1);
    }
  }

  // we had to switch buffers to load stdin, switch back
  if (prev_buf) {
    set_curbuf(prev_buf, 0, false);
  }

  no_wait_return = false;
  msg_didany = save_msg_didany;
  TIME_MSG("reading stdin");
  check_swap_exists_action();
}

// Create the requested number of windows and edit buffers in them.
// Also does recovery if "recoverymode" set.
static void create_windows(mparm_T *parmp)
{
  // Create the number of windows that was requested.
  if (parmp->window_count == -1) {      // was not set
    parmp->window_count = 1;
  }
  if (parmp->window_count == 0) {
    parmp->window_count = GARGCOUNT;
  }