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

Code before:
    const list_T *l = get_vim_var_list(VV_ARGV);
    int argc = tv_list_len(l);
    list_T *argv_cpy = tv_list_alloc(argc + 2);
    bool added_startup_arg = false;
    TV_LIST_ITER_CONST(l, li, {
      const char *arg = tv_get_string(TV_LIST_ITEM_TV(li));
      size_t arg_size = strlen(arg);
      assert(arg_size <= (size_t)SSIZE_MAX);
      tv_list_append_string(argv_cpy, arg, (ssize_t)arg_size);
      if (!added_startup_arg) {
        tv_list_append_string(argv_cpy, "-c", 2);
        size_t cmd_size = strlen(eap->arg);
        assert(cmd_size <= (size_t)SSIZE_MAX);
        tv_list_append_string(argv_cpy, eap->arg, (ssize_t)cmd_size);
        added_startup_arg = true;
      }
    });
    set_vim_var_list(VV_ARGV, argv_cpy);
  }

  char *quit_cmd = (eap->do_ecmd_cmd) ? eap->do_ecmd_cmd : "qall";

  // Prepend "confirm " to cmd if :confirm is used
  if (cmdmod.cmod_flags & CMOD_CONFIRM) {
    char *quit_cmd_copy = NULL;
    quit_cmd_copy = concat_str("confirm ", quit_cmd);
    quit_cmd = quit_cmd_copy;
  }

  Error err = ERROR_INIT;
  restarting = true;
  nvim_command(cstr_as_string(quit_cmd), &err);
  if (ERROR_SET(&err)) {
    emsg(err.msg);  // Could not exit
    api_clear_error(&err);
    not_restarting();
    return;
  }
  if (!exiting) {
    emsg("restart failed: +cmd did not quit the server");
    not_restarting();
  }
}

/// ":close": close current window, unless it is the last one
static void ex_close(exarg_T *eap)
{
  win_T *win = NULL;
  int winnr = 0;
  if (cmdwin_type != 0) {
    cmdwin_result = Ctrl_C;

Review comment (suggestion):
I'm quite new to C, so I'd like feedback on whether this is the correct way to prepend a static string. I tested it and it works fine, but I'm still not the best at catching memory issues.

Code after:
    const list_T *l = get_vim_var_list(VV_ARGV);
    int argc = tv_list_len(l);
    list_T *argv_cpy = tv_list_alloc(argc + 2);
    bool added_startup_arg = false;
    TV_LIST_ITER_CONST(l, li, {
      const char *arg = tv_get_string(TV_LIST_ITEM_TV(li));
      size_t arg_size = strlen(arg);
      assert(arg_size <= (size_t)SSIZE_MAX);
      tv_list_append_string(argv_cpy, arg, (ssize_t)arg_size);
      if (!added_startup_arg) {
        tv_list_append_string(argv_cpy, "-c", 2);
        size_t cmd_size = strlen(eap->arg);
        assert(cmd_size <= (size_t)SSIZE_MAX);
        tv_list_append_string(argv_cpy, eap->arg, (ssize_t)cmd_size);
        added_startup_arg = true;
      }
    });
    set_vim_var_list(VV_ARGV, argv_cpy);
  }

  char *quit_cmd = (eap->do_ecmd_cmd) ? eap->do_ecmd_cmd : "qall";
  char *quit_cmd_copy = NULL;

  // Prepend "confirm " to cmd if :confirm is used
  if (cmdmod.cmod_flags & CMOD_CONFIRM) {
    quit_cmd_copy = concat_str("confirm ", quit_cmd);
    quit_cmd = quit_cmd_copy;
  }

  Error err = ERROR_INIT;
  restarting = true;
  nvim_command(cstr_as_string(quit_cmd), &err);
  xfree(quit_cmd_copy);
  if (ERROR_SET(&err)) {
    emsg(err.msg);  // Could not exit
    api_clear_error(&err);
    not_restarting();
    return;
  }
  if (!exiting) {
    emsg("restart failed: +cmd did not quit the server");
    not_restarting();
  }
}

/// ":close": close current window, unless it is the last one
static void ex_close(exarg_T *eap)
{
  win_T *win = NULL;
  int winnr = 0;
  if (cmdwin_type != 0) {