diff options
| author | ZyX <kp-pav@yandex.ru> | 2017-01-23 01:52:36 +0300 | 
|---|---|---|
| committer | ZyX <kp-pav@yandex.ru> | 2017-01-23 02:02:35 +0300 | 
| commit | 47a7d325639370056c58f56adfe038d9cfd26e77 (patch) | |
| tree | 2d55bf185219178da328b9e728b506c17c3d8458 /src/nvim/eval.c | |
| parent | 043d8ba422b4f35d82e8acef8438248de94ab167 (diff) | |
| download | rneovim-47a7d325639370056c58f56adfe038d9cfd26e77.tar.gz rneovim-47a7d325639370056c58f56adfe038d9cfd26e77.tar.bz2 rneovim-47a7d325639370056c58f56adfe038d9cfd26e77.zip | |
coverity/155513: Do not assume xcalloc can return NULL
Diffstat (limited to 'src/nvim/eval.c')
| -rw-r--r-- | src/nvim/eval.c | 80 | 
1 files changed, 37 insertions, 43 deletions
| diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 6b14d21da7..dffbbe9df9 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -9748,60 +9748,54 @@ static void f_function(typval_T *argvars, typval_T *rettv, FunPtr fptr)        }      }      if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL) { -      partial_T *pt = (partial_T *)xcalloc(1, sizeof(partial_T)); +      partial_T *const pt = xcalloc(1, sizeof(*pt));        // result is a VAR_PARTIAL -      if (pt != NULL) { -        if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0)) { -          listitem_T *li; +      if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0)) { +        const int arg_len = (arg_pt == NULL ? 0 : arg_pt->pt_argc); +        const int lv_len = (list == NULL ? 0 : list->lv_len); + +        pt->pt_argc = arg_len + lv_len; +        pt->pt_argv = xmalloc(sizeof(pt->pt_argv[0]) * pt->pt_argc); +        if (pt->pt_argv == NULL) { +          xfree(pt); +          xfree(name); +          return; +        } else {            int i = 0; -          int arg_len = 0; -          int lv_len = 0; - -          if (arg_pt != NULL) { -            arg_len = arg_pt->pt_argc; +          for (; i < arg_len; i++) { +            copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);            } -          if (list != NULL) { -            lv_len = list->lv_len; -          } -          pt->pt_argc = arg_len + lv_len; -          pt->pt_argv = (typval_T *)xmalloc(sizeof(typval_T) * pt->pt_argc); -          if (pt->pt_argv == NULL) { -            xfree(pt); -            xfree(name); -            return; -          } else { -            for (i = 0; i < arg_len; i++) { -              copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]); -            } -            if (lv_len > 0) { -              for (li = list->lv_first; li != NULL; li = li->li_next) { -                copy_tv(&li->li_tv, &pt->pt_argv[i++]); -              } +          if (lv_len > 0) { +            for (listitem_T *li = list->lv_first; +                 li != NULL; +                 li = li->li_next) { +              copy_tv(&li->li_tv, &pt->pt_argv[i++]);              }            }          } +      } -        // For "function(dict.func, [], dict)" and "func" is a partial -        // use "dict". That is backwards compatible. -        if (dict_idx > 0) { -          // The dict is bound explicitly, pt_auto is false -          pt->pt_dict = argvars[dict_idx].vval.v_dict; +      // For "function(dict.func, [], dict)" and "func" is a partial +      // use "dict". That is backwards compatible. +      if (dict_idx > 0) { +        // The dict is bound explicitly, pt_auto is false +        pt->pt_dict = argvars[dict_idx].vval.v_dict; +        (pt->pt_dict->dv_refcount)++; +      } else if (arg_pt != NULL) { +        // If the dict was bound automatically the result is also +        // bound automatically. +        pt->pt_dict = arg_pt->pt_dict; +        pt->pt_auto = arg_pt->pt_auto; +        if (pt->pt_dict != NULL) {            (pt->pt_dict->dv_refcount)++; -        } else if (arg_pt != NULL) { -          // If the dict was bound automatically the result is also -          // bound automatically. -          pt->pt_dict = arg_pt->pt_dict; -          pt->pt_auto = arg_pt->pt_auto; -          if (pt->pt_dict != NULL) { -            (pt->pt_dict->dv_refcount)++; -          }          } - -        pt->pt_refcount = 1; -        pt->pt_name = name; -        func_ref(pt->pt_name);        } + +      pt->pt_refcount = 1; +      pt->pt_name = name; +      func_ref(pt->pt_name); +        rettv->v_type = VAR_PARTIAL;        rettv->vval.v_partial = pt;      } else { | 
