diff options
author | Bram Moolenaar <Bram@vim.org> | 2016-04-07 21:40:38 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2016-04-07 21:40:38 +0200 |
commit | 0e4c1de5560c7f8b4cae539ec8cff0949daba3fc (patch) | |
tree | f6daf3541d698b421905285e17ffdd8a6b33a051 /src/channel.c | |
parent | baec5c1768098f9dd867b465aaabfdfb294f10c2 (diff) |
patch 7.4.1717v7.4.1717
Problem: Leaking memory when opening a channel fails.
Solution: Unreference partials in job options.
Diffstat (limited to 'src/channel.c')
-rw-r--r-- | src/channel.c | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/src/channel.c b/src/channel.c index b21c4432cc..7b811dbce2 100644 --- a/src/channel.c +++ b/src/channel.c @@ -858,7 +858,7 @@ channel_open_func(typval_T *argvars) char *rest; int port; jobopt_T opt; - channel_T *channel; + channel_T *channel = NULL; address = get_tv_string(&argvars[0]); if (argvars[1].v_type != VAR_UNKNOWN @@ -890,11 +890,11 @@ channel_open_func(typval_T *argvars) opt.jo_timeout = 2000; if (get_job_options(&argvars[1], &opt, JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL) - return NULL; + goto theend; if (opt.jo_timeout < 0) { EMSG(_(e_invarg)); - return NULL; + goto theend; } channel = channel_open((char *)address, port, opt.jo_waittime, NULL); @@ -903,6 +903,8 @@ channel_open_func(typval_T *argvars) opt.jo_set = JO_ALL; channel_set_options(channel, &opt); } +theend: + free_job_options(&opt); return channel; } @@ -2897,7 +2899,7 @@ common_channel_read(typval_T *argvars, typval_T *rettv, int raw) clear_job_options(&opt); if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID) == FAIL) - return; + goto theend; channel = get_channel_arg(&argvars[0], TRUE); if (channel != NULL) @@ -2930,6 +2932,9 @@ common_channel_read(typval_T *argvars, typval_T *rettv, int raw) } } } + +theend: + free_job_options(&opt); } # if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \ @@ -3056,13 +3061,13 @@ send_common( channel_T *channel; int part_send; + clear_job_options(opt); channel = get_channel_arg(&argvars[0], TRUE); if (channel == NULL) return NULL; part_send = channel_part_send(channel); *part_read = channel_part_read(channel); - clear_job_options(opt); if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL) return NULL; @@ -3145,6 +3150,7 @@ ch_expr_common(typval_T *argvars, typval_T *rettv, int eval) free_tv(listtv); } } + free_job_options(&opt); } /* @@ -3175,6 +3181,7 @@ ch_raw_common(typval_T *argvars, typval_T *rettv, int eval) timeout = channel_get_timeout(channel, part_read); rettv->vval.v_string = channel_read_block(channel, part_read, timeout); } + free_job_options(&opt); } # if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO) @@ -3545,6 +3552,9 @@ handle_io(typval_T *item, int part, jobopt_T *opt) return OK; } +/* + * Clear a jobopt_T before using it. + */ void clear_job_options(jobopt_T *opt) { @@ -3552,6 +3562,22 @@ clear_job_options(jobopt_T *opt) } /* + * Free any members of a jobopt_T. + */ + void +free_job_options(jobopt_T *opt) +{ + if (opt->jo_partial != NULL) + partial_unref(opt->jo_partial); + if (opt->jo_out_partial != NULL) + partial_unref(opt->jo_out_partial); + if (opt->jo_err_partial != NULL) + partial_unref(opt->jo_err_partial); + if (opt->jo_close_partial != NULL) + partial_unref(opt->jo_close_partial); +} + +/* * Get the PART_ number from the first character of an option name. */ static int @@ -4053,6 +4079,9 @@ job_start(typval_T *argvars) return NULL; job->jv_status = JOB_FAILED; +#ifndef USE_ARGV + ga_init2(&ga, (int)sizeof(char*), 20); +#endif /* Default mode is NL. */ clear_job_options(&opt); @@ -4060,7 +4089,7 @@ job_start(typval_T *argvars) if (get_job_options(&argvars[1], &opt, JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL) - return job; + goto theend; /* Check that when io is "file" that there is a file name. */ for (part = PART_OUT; part <= PART_IN; ++part) @@ -4070,7 +4099,7 @@ job_start(typval_T *argvars) || *opt.jo_io_name[part] == NUL)) { EMSG(_("E920: _io file requires _name to be set")); - return job; + goto theend; } if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER) @@ -4091,7 +4120,7 @@ job_start(typval_T *argvars) else buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE); if (buf == NULL) - return job; + goto theend; if (buf->b_ml.ml_mfp == NULL) { char_u numbuf[NUMBUFLEN]; @@ -4105,17 +4134,13 @@ job_start(typval_T *argvars) else s = opt.jo_io_name[PART_IN]; EMSG2(_("E918: buffer must be loaded: %s"), s); - return job; + goto theend; } job->jv_in_buf = buf; } job_set_options(job, &opt); -#ifndef USE_ARGV - ga_init2(&ga, (int)sizeof(char*), 20); -#endif - if (argvars[0].v_type == VAR_STRING) { /* Command is a string. */ @@ -4123,11 +4148,11 @@ job_start(typval_T *argvars) if (cmd == NULL || *cmd == NUL) { EMSG(_(e_invarg)); - return job; + goto theend; } #ifdef USE_ARGV if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL) - return job; + goto theend; argv[argc] = NULL; #endif } @@ -4136,7 +4161,7 @@ job_start(typval_T *argvars) || argvars[0].vval.v_list->lv_len < 1) { EMSG(_(e_invarg)); - return job; + goto theend; } else { @@ -4148,7 +4173,7 @@ job_start(typval_T *argvars) /* Pass argv[] to mch_call_shell(). */ argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1)); if (argv == NULL) - return job; + goto theend; #endif for (li = l->lv_first; li != NULL; li = li->li_next) { @@ -4222,6 +4247,7 @@ theend: #else vim_free(ga.ga_data); #endif + free_job_options(&opt); return job; } |