diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2014-06-16 20:27:25 -0400 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2014-06-16 20:27:25 -0400 |
commit | d199d18159c624844c9c8052d1a98b91084fb803 (patch) | |
tree | 827dd9c4f8f1f6ef73ff1f20a609d08e36640986 | |
parent | 8bbeb4b480a72d0099a18c4d8200313600045231 (diff) | |
parent | e85598e5a91c714c10034b6b3986a666065d1078 (diff) | |
download | rneovim-d199d18159c624844c9c8052d1a98b91084fb803.tar.gz rneovim-d199d18159c624844c9c8052d1a98b91084fb803.tar.bz2 rneovim-d199d18159c624844c9c8052d1a98b91084fb803.zip |
Merge #787 'removal of redundant OOM error handling'
-rw-r--r-- | src/nvim/buffer.c | 2 | ||||
-rw-r--r-- | src/nvim/charset.c | 5 | ||||
-rw-r--r-- | src/nvim/edit.c | 6 | ||||
-rw-r--r-- | src/nvim/eval.c | 339 | ||||
-rw-r--r-- | src/nvim/ex_cmds.c | 2 | ||||
-rw-r--r-- | src/nvim/ex_cmds2.c | 2 | ||||
-rw-r--r-- | src/nvim/ex_docmd.c | 29 | ||||
-rw-r--r-- | src/nvim/ex_getln.c | 76 | ||||
-rw-r--r-- | src/nvim/fileio.c | 12 | ||||
-rw-r--r-- | src/nvim/getchar.c | 17 | ||||
-rw-r--r-- | src/nvim/hashtab.c | 37 | ||||
-rw-r--r-- | src/nvim/hashtab.h | 3 | ||||
-rw-r--r-- | src/nvim/mark.c | 3 | ||||
-rw-r--r-- | src/nvim/mbyte.c | 14 | ||||
-rw-r--r-- | src/nvim/menu.c | 27 | ||||
-rw-r--r-- | src/nvim/message.c | 74 | ||||
-rw-r--r-- | src/nvim/misc1.c | 18 | ||||
-rw-r--r-- | src/nvim/misc2.c | 2 | ||||
-rw-r--r-- | src/nvim/ops.c | 16 | ||||
-rw-r--r-- | src/nvim/option.c | 34 | ||||
-rw-r--r-- | src/nvim/quickfix.c | 3 | ||||
-rw-r--r-- | src/nvim/regexp.c | 6 | ||||
-rw-r--r-- | src/nvim/search.c | 14 | ||||
-rw-r--r-- | src/nvim/spell.c | 4 | ||||
-rw-r--r-- | src/nvim/strings.c | 36 | ||||
-rw-r--r-- | src/nvim/syntax.c | 12 | ||||
-rw-r--r-- | src/nvim/tag.c | 6 | ||||
-rw-r--r-- | src/nvim/undo.c | 12 |
28 files changed, 300 insertions, 511 deletions
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index db611823fc..67cd5a3094 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -1908,7 +1908,7 @@ static char_u *fname_match(regprog_T *prog, char_u *name) else { /* Replace $(HOME) with '~' and try matching again. */ p = home_replace_save(NULL, name); - if (p != NULL && vim_regexec(®match, p, (colnr_T)0)) + if (vim_regexec(®match, p, (colnr_T)0)) match = name; free(p); } diff --git a/src/nvim/charset.c b/src/nvim/charset.c index 948cb7e569..549c926763 100644 --- a/src/nvim/charset.c +++ b/src/nvim/charset.c @@ -9,6 +9,7 @@ #include "nvim/vim.h" #include "nvim/charset.h" #include "nvim/farsi.h" +#include "nvim/func_attr.h" #include "nvim/main.h" #include "nvim/mbyte.h" #include "nvim/memline.h" @@ -321,12 +322,12 @@ void trans_characters(char_u *buf, int bufsize) } /// Translate a string into allocated memory, replacing special chars with -/// printable chars. Returns NULL when out of memory. +/// printable chars. /// /// @param s /// /// @return translated string -char_u *transstr(char_u *s) +char_u *transstr(char_u *s) FUNC_ATTR_NONNULL_RET { char_u *res; char_u *p; diff --git a/src/nvim/edit.c b/src/nvim/edit.c index a802ae4942..5a70964d57 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -2485,8 +2485,6 @@ ins_compl_dictionaries ( if (ctrl_x_mode == CTRL_X_WHOLE_LINE) { char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); - if (pat_esc == NULL) - goto theend; size_t len = STRLEN(pat_esc) + 10; ptr = xmalloc(len); vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); @@ -5253,8 +5251,6 @@ internal_format ( */ saved_text = vim_strsave(get_cursor_pos_ptr()); curwin->w_cursor.col = orig_col; - if (saved_text == NULL) - break; /* Can't do it, out of memory */ saved_text[startcol] = NUL; /* Backspace over characters that will move to the next line */ @@ -7979,7 +7975,7 @@ static int ins_tab(void) /* * Handle CR or NL in insert mode. - * Return TRUE when out of memory or can't undo. + * Return TRUE when it can't undo. */ static int ins_eol(int c) { diff --git a/src/nvim/eval.c b/src/nvim/eval.c index e4e90142cc..1de2b7bbf1 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -26,6 +26,7 @@ #include "nvim/ex_eval.h" #include "nvim/ex_getln.h" #include "nvim/fileio.h" +#include "nvim/func_attr.h" #include "nvim/fold.h" #include "nvim/getchar.h" #include "nvim/hashtab.h" @@ -1396,8 +1397,6 @@ ex_let_vars ( /* Put the rest of the list (may be empty) in the var after ';'. * Create a new list for this. */ l = list_alloc(); - if (l == NULL) - return FAIL; while (item != NULL) { list_append_tv(l, &item->li_tv); item = item->li_next; @@ -2236,8 +2235,6 @@ static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, ch /* Need to add an item to the Dictionary. */ di = dictitem_alloc(lp->ll_newkey); - if (di == NULL) - return; if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL) { free(di); return; @@ -4297,8 +4294,6 @@ eval_index ( if (!empty2 && (n2 < 0 || n2 + 1 < n1)) n2 = -1; l = list_alloc(); - if (l == NULL) - return FAIL; item = list_find(rettv->vval.v_list, n1); while (n1++ <= n2) { list_append_tv(l, &item->li_tv); @@ -4608,8 +4603,6 @@ static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate) if (evaluate) { l = list_alloc(); - if (l == NULL) - return FAIL; } *arg = skipwhite(*arg + 1); @@ -4618,12 +4611,9 @@ static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate) goto failret; if (evaluate) { item = listitem_alloc(); - if (item != NULL) { - item->li_tv = tv; - item->li_tv.v_lock = 0; - list_append(l, item); - } else - clear_tv(&tv); + item->li_tv = tv; + item->li_tv.v_lock = 0; + list_append(l, item); } if (**arg == ']') @@ -4657,7 +4647,7 @@ failret: * Allocate an empty header for a list. * Caller should take care of the reference count. */ -list_T *list_alloc(void) +list_T *list_alloc(void) FUNC_ATTR_NONNULL_RET { list_T *list = xcalloc(1, sizeof(list_T)); @@ -4725,7 +4715,7 @@ list_free ( /* * Allocate a list item. */ -listitem_T *listitem_alloc(void) +listitem_T *listitem_alloc(void) FUNC_ATTR_NONNULL_RET { return xmalloc(sizeof(listitem_T)); } @@ -5106,17 +5096,13 @@ static void list_append_number(list_T *l, varnumber_T n) /* * Insert typval_T "tv" in list "l" before "item". * If "item" is NULL append at the end. - * Return FAIL when out of memory. */ -int list_insert_tv(list_T *l, typval_T *tv, listitem_T *item) +void list_insert_tv(list_T *l, typval_T *tv, listitem_T *item) { listitem_T *ni = listitem_alloc(); - if (ni == NULL) - return FAIL; copy_tv(tv, &ni->li_tv); list_insert(l, ni, item); - return OK; } void list_insert(list_T *l, listitem_T *ni, listitem_T *item) @@ -5143,24 +5129,22 @@ void list_insert(list_T *l, listitem_T *ni, listitem_T *item) /* * Extend "l1" with "l2". * If "bef" is NULL append at the end, otherwise insert before this item. - * Returns FAIL when out of memory. */ -static int list_extend(list_T *l1, list_T *l2, listitem_T *bef) +static void list_extend(list_T *l1, list_T *l2, listitem_T *bef) { listitem_T *item; int todo = l2->lv_len; /* We also quit the loop when we have inserted the original item count of * the list, avoid a hang when we extend a list with itself. */ - for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next) - if (list_insert_tv(l1, &item->li_tv, bef) == FAIL) - return FAIL; - return OK; + for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next) { + list_insert_tv(l1, &item->li_tv, bef); + } } /* * Concatenate lists "l1" and "l2" into a new list, stored in "tv". - * Return FAIL when out of memory. + * Return FAIL on failure to copy. */ static int list_concat(list_T *l1, list_T *l2, typval_T *tv) { @@ -5177,51 +5161,47 @@ static int list_concat(list_T *l1, list_T *l2, typval_T *tv) tv->vval.v_list = l; /* append all items from the second list */ - return list_extend(l, l2, NULL); + list_extend(l, l2, NULL); + return OK; } /* * Make a copy of list "orig". Shallow if "deep" is FALSE. * The refcount of the new list is set to 1. * See item_copy() for "copyID". - * Returns NULL when out of memory. + * Returns NULL if orig is NULL or some failure happens. */ static list_T *list_copy(list_T *orig, int deep, int copyID) { - list_T *copy; listitem_T *item; listitem_T *ni; if (orig == NULL) return NULL; - copy = list_alloc(); - if (copy != NULL) { - if (copyID != 0) { - /* Do this before adding the items, because one of the items may - * refer back to this list. */ - orig->lv_copyID = copyID; - orig->lv_copylist = copy; - } - for (item = orig->lv_first; item != NULL && !got_int; - item = item->li_next) { - ni = listitem_alloc(); - if (ni == NULL) + list_T *copy = list_alloc(); + if (copyID != 0) { + /* Do this before adding the items, because one of the items may + * refer back to this list. */ + orig->lv_copyID = copyID; + orig->lv_copylist = copy; + } + for (item = orig->lv_first; item != NULL && !got_int; + item = item->li_next) { + ni = listitem_alloc(); + if (deep) { + if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL) { + free(ni); break; - if (deep) { - if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL) { - free(ni); - break; - } - } else - copy_tv(&item->li_tv, &ni->li_tv); - list_append(copy, ni); - } - ++copy->lv_refcount; - if (item != NULL) { - list_unref(copy); - copy = NULL; - } + } + } else + copy_tv(&item->li_tv, &ni->li_tv); + list_append(copy, ni); + } + ++copy->lv_refcount; + if (item != NULL) { + list_unref(copy); + copy = NULL; } return copy; @@ -5592,7 +5572,7 @@ void set_ref_in_item(typval_T *tv, int copyID) /* * Allocate an empty header for a dictionary. */ -dict_T *dict_alloc(void) +dict_T *dict_alloc(void) FUNC_ATTR_NONNULL_RET { dict_T *d = xmalloc(sizeof(dict_T)); @@ -5614,19 +5594,14 @@ dict_T *dict_alloc(void) /* * Allocate an empty dict for a return value. - * Returns OK or FAIL. */ -static int rettv_dict_alloc(typval_T *rettv) +static void rettv_dict_alloc(typval_T *rettv) { - dict_T *d = dict_alloc(); - - if (d == NULL) - return FAIL; + dict_T *d = dict_alloc(); rettv->vval.v_dict = d; rettv->v_type = VAR_DICT; ++d->dv_refcount; - return OK; } @@ -5687,7 +5662,7 @@ dict_free ( * The "key" is copied to the new item. * Note that the value of the item "di_tv" still needs to be initialized! */ -dictitem_T *dictitem_alloc(char_u *key) +dictitem_T *dictitem_alloc(char_u *key) FUNC_ATTR_NONNULL_RET { dictitem_T *di = xmalloc(sizeof(dictitem_T) + STRLEN(key)); STRCPY(di->di_key, key); @@ -5698,7 +5673,7 @@ dictitem_T *dictitem_alloc(char_u *key) /* * Make a copy of a Dictionary item. */ -static dictitem_T *dictitem_copy(dictitem_T *org) +static dictitem_T *dictitem_copy(dictitem_T *org) FUNC_ATTR_NONNULL_RET { dictitem_T *di = xmalloc(sizeof(dictitem_T) + STRLEN(org->di_key)); @@ -5737,11 +5712,10 @@ void dictitem_free(dictitem_T *item) * Make a copy of dict "d". Shallow if "deep" is FALSE. * The refcount of the new dict is set to 1. * See item_copy() for "copyID". - * Returns NULL when out of memory. + * Returns NULL if orig is NULL or some other failure. */ static dict_T *dict_copy(dict_T *orig, int deep, int copyID) { - dict_T *copy; dictitem_T *di; int todo; hashitem_T *hi; @@ -5749,8 +5723,8 @@ static dict_T *dict_copy(dict_T *orig, int deep, int copyID) if (orig == NULL) return NULL; - copy = dict_alloc(); - if (copy != NULL) { + dict_T *copy = dict_alloc(); + { if (copyID != 0) { orig->dv_copyID = copyID; orig->dv_copydict = copy; @@ -5761,8 +5735,6 @@ static dict_T *dict_copy(dict_T *orig, int deep, int copyID) --todo; di = dictitem_alloc(hi->hi_key); - if (di == NULL) - break; if (deep) { if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep, copyID) == FAIL) { @@ -5790,7 +5762,7 @@ static dict_T *dict_copy(dict_T *orig, int deep, int copyID) /* * Add item "item" to Dictionary "d". - * Returns FAIL when out of memory and when key already exists. + * Returns FAIL when key already exists. */ int dict_add(dict_T *d, dictitem_T *item) { @@ -5800,15 +5772,13 @@ int dict_add(dict_T *d, dictitem_T *item) /* * Add a number or string entry to dictionary "d". * When "str" is NULL use number "nr", otherwise use "str". - * Returns FAIL when out of memory and when key already exists. + * Returns FAIL when key already exists. */ int dict_add_nr_str(dict_T *d, char *key, long nr, char_u *str) { dictitem_T *item; item = dictitem_alloc((char_u *)key); - if (item == NULL) - return FAIL; item->di_tv.v_lock = 0; if (str == NULL) { item->di_tv.v_type = VAR_NUMBER; @@ -5826,15 +5796,12 @@ int dict_add_nr_str(dict_T *d, char *key, long nr, char_u *str) /* * Add a list entry to dictionary "d". - * Returns FAIL when out of memory and when key already exists. + * Returns FAIL when key already exists. */ int dict_add_list(dict_T *d, char *key, list_T *list) { - dictitem_T *item; + dictitem_T *item = dictitem_alloc((char_u *)key); - item = dictitem_alloc((char_u *)key); - if (item == NULL) - return FAIL; item->di_tv.v_lock = 0; item->di_tv.v_type = VAR_LIST; item->di_tv.vval.v_list = list; @@ -5889,7 +5856,7 @@ dictitem_T *dict_find(dict_T *d, char_u *key, int len) /* * Get a string item from a dictionary. * When "save" is TRUE allocate memory for it. - * Returns NULL if the entry doesn't exist or out of memory. + * Returns NULL if the entry doesn't exist. */ char_u *get_dict_string(dict_T *d, char_u *key, int save) { @@ -5900,20 +5867,19 @@ char_u *get_dict_string(dict_T *d, char_u *key, int save) if (di == NULL) return NULL; s = get_tv_string(&di->di_tv); - if (save && s != NULL) + if (save) { s = vim_strsave(s); + } return s; } /* * Get a number item from a dictionary. - * Returns 0 if the entry doesn't exist or out of memory. + * Returns 0 if the entry doesn't exist. */ long get_dict_number(dict_T *d, char_u *key) { - dictitem_T *di; - - di = dict_find(d, key, -1); + dictitem_T *di = dict_find(d, key, -1); if (di == NULL) return 0; return get_tv_number(&di->di_tv); @@ -6003,8 +5969,6 @@ static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate) if (evaluate) { d = dict_alloc(); - if (d == NULL) - return FAIL; } tvkey.v_type = VAR_UNKNOWN; tv.v_type = VAR_UNKNOWN; @@ -6045,11 +6009,10 @@ static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate) } item = dictitem_alloc(key); clear_tv(&tvkey); - if (item != NULL) { - item->di_tv = tv; - item->di_tv.v_lock = 0; - if (dict_add(d, item) == FAIL) - dictitem_free(item); + item->di_tv = tv; + item->di_tv.v_lock = 0; + if (dict_add(d, item) == FAIL) { + dictitem_free(item); } } @@ -8203,7 +8166,7 @@ void dict_extend(dict_T *d1, dict_T *d2, char_u *action) } if (di1 == NULL) { di1 = dictitem_copy(HI2DI(hi2)); - if (di1 != NULL && dict_add(d1, di1) == FAIL) + if (dict_add(d1, di1) == FAIL) dictitem_free(di1); } else if (*action == 'e') { EMSG2(_("E737: Key already exists: %s"), hi2->hi_key); @@ -8322,13 +8285,11 @@ static void f_feedkeys(typval_T *argvars, typval_T *rettv) /* Need to escape K_SPECIAL and CSI before putting the string in the * typeahead buffer. */ keys_esc = vim_strsave_escape_csi(keys); - if (keys_esc != NULL) { - ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE), - typebuf.tb_len, !typed, FALSE); - free(keys_esc); - if (vgetc_busy) - typebuf_was_filled = TRUE; - } + ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE), + typebuf.tb_len, !typed, FALSE); + free(keys_esc); + if (vgetc_busy) + typebuf_was_filled = TRUE; } } @@ -10409,8 +10370,6 @@ static void dict_list(typval_T *argvars, typval_T *rettv, int what) di = HI2DI(hi); li = listitem_alloc(); - if (li == NULL) - break; list_append(rettv->vval.v_list, li); if (what == 0) { @@ -10427,21 +10386,15 @@ static void dict_list(typval_T *argvars, typval_T *rettv, int what) li->li_tv.v_type = VAR_LIST; li->li_tv.v_lock = 0; li->li_tv.vval.v_list = l2; - if (l2 == NULL) - break; ++l2->lv_refcount; li2 = listitem_alloc(); - if (li2 == NULL) - break; list_append(l2, li2); li2->li_tv.v_type = VAR_STRING; li2->li_tv.v_lock = 0; li2->li_tv.vval.v_string = vim_strsave(di->di_key); li2 = listitem_alloc(); - if (li2 == NULL) - break; list_append(l2, li2); copy_tv(&di->di_tv, &li2->li_tv); } @@ -10820,24 +10773,27 @@ static void get_maparg(typval_T *argvars, typval_T *rettv, int exact) if (rhs != NULL) rettv->vval.v_string = str2special_save(rhs, FALSE); - } else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL) { - /* Return a dictionary. */ - char_u *lhs = str2special_save(mp->m_keys, TRUE); - char_u *mapmode = map_mode_to_chars(mp->m_mode); - dict_T *dict = rettv->vval.v_dict; + } else { + rettv_dict_alloc(rettv); + if (rhs != NULL) { + // Return a dictionary. + char_u *lhs = str2special_save(mp->m_keys, TRUE); + char_u *mapmode = map_mode_to_chars(mp->m_mode); + dict_T *dict = rettv->vval.v_dict; - dict_add_nr_str(dict, "lhs", 0L, lhs); - dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str); - dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L, NULL); - dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL); - dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL); - dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL); - dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL); - dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL); - dict_add_nr_str(dict, "mode", 0L, mapmode); + dict_add_nr_str(dict, "lhs", 0L, lhs); + dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str); + dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L, NULL); + dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL); + dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL); + dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL); + dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL); + dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL); + dict_add_nr_str(dict, "mode", 0L, mapmode); - free(lhs); - free(mapmode); + free(lhs); + free(mapmode); + } } } @@ -11580,11 +11536,7 @@ static void f_readfile(typval_T *argvars, typval_T *rettv) prevlen = prevsize = 0; } - if ((li = listitem_alloc()) == NULL) { - free(s); - failed = TRUE; - break; - } + li = listitem_alloc(); li->li_tv.v_type = VAR_STRING; li->li_tv.v_lock = 0; li->li_tv.vval.v_string = s; @@ -11851,10 +11803,7 @@ static void f_repeat(typval_T *argvars, typval_T *rettv) rettv_list_alloc(rettv); if (argvars[0].vval.v_list != NULL) { while (n-- > 0) { - if (list_extend(rettv->vval.v_list, argvars[0].vval.v_list, NULL) - == FAIL) { - break; - } + list_extend(rettv->vval.v_list, argvars[0].vval.v_list, NULL); } } } else { @@ -11953,15 +11902,10 @@ static void f_resolve(typval_T *argvars, typval_T *rettv) * concatenate the remainders. */ q = path_next_component(vim_ispathsep(*buf) ? buf + 1 : buf); if (*q != NUL) { - if (remain == NULL) - remain = vim_strsave(q - 1); - else { - cpy = concat_str(q - 1, remain); - if (cpy != NULL) { - free(remain); - remain = cpy; - } - } + cpy = remain; + remain = remain ? + concat_str(q - 1, remain) : (char_u *) xstrdup((char *)q - 1); + free(cpy); q[-1] = NUL; } @@ -12017,10 +11961,8 @@ static void f_resolve(typval_T *argvars, typval_T *rettv) || vim_ispathsep(p[2])))))) { /* Prepend "./". */ cpy = concat_str((char_u *)"./", p); - if (cpy != NULL) { - free(p); - p = cpy; - } + free(p); + p = cpy; } else if (!is_relative_to_current) { /* Strip leading "./". */ q = p; @@ -13411,14 +13353,10 @@ static void f_spellsuggest(typval_T *argvars, typval_T *rettv) str = ((char_u **)ga.ga_data)[i]; li = listitem_alloc(); - if (li == NULL) - free(str); - else { - li->li_tv.v_type = VAR_STRING; - li->li_tv.v_lock = 0; - li->li_tv.vval.v_string = str; - list_append(rettv->vval.v_list, li); - } + li->li_tv.v_type = VAR_STRING; + li->li_tv.v_lock = 0; + li->li_tv.vval.v_string = str; + list_append(rettv->vval.v_list, li); } ga_clear(&ga); } @@ -14504,24 +14442,22 @@ static void f_undofile(typval_T *argvars, typval_T *rettv) */ static void f_undotree(typval_T *argvars, typval_T *rettv) { - if (rettv_dict_alloc(rettv) == OK) { - dict_T *dict = rettv->vval.v_dict; - list_T *list; + rettv_dict_alloc(rettv); - dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL); - dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL); - dict_add_nr_str(dict, "save_last", - (long)curbuf->b_u_save_nr_last, NULL); - dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL); - dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL); - dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL); + dict_T *dict = rettv->vval.v_dict; + list_T *list; - list = list_alloc(); - if (list != NULL) { - u_eval_tree(curbuf->b_u_oldhead, list); - dict_add_list(dict, "entries", list); - } - } + dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL); + dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL); + dict_add_nr_str(dict, "save_last", + (long)curbuf->b_u_save_nr_last, NULL); + dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL); + dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL); + dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL); + + list = list_alloc(); + u_eval_tree(curbuf->b_u_oldhead, list); + dict_add_list(dict, "entries", list); } /* @@ -14700,8 +14636,7 @@ static void f_winsaveview(typval_T *argvars, typval_T *rettv) { dict_T *dict; - if (rettv_dict_alloc(rettv) == FAIL) - return; + rettv_dict_alloc(rettv); dict = rettv->vval.v_dict; dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL); @@ -17050,15 +16985,13 @@ void ex_function(exarg_T *eap) int j = FAIL; if (sourcing_name != NULL) { scriptname = autoload_name(name); - if (scriptname != NULL) { - p = vim_strchr(scriptname, '/'); - plen = (int)STRLEN(p); - slen = (int)STRLEN(sourcing_name); - if (slen > plen && fnamecmp(p, - sourcing_name + slen - plen) == 0) - j = OK; - free(scriptname); - } + p = vim_strchr(scriptname, '/'); + plen = (int)STRLEN(p); + slen = (int)STRLEN(sourcing_name); + if (slen > plen && fnamecmp(p, + sourcing_name + slen - plen) == 0) + j = OK; + free(scriptname); } if (j == FAIL) { EMSG2(_( @@ -17074,10 +17007,6 @@ void ex_function(exarg_T *eap) if (fudi.fd_di == NULL) { /* add new dict entry */ fudi.fd_di = dictitem_alloc(fudi.fd_newkey); - if (fudi.fd_di == NULL) { - free(fp); - goto erret; - } if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL) { free(fudi.fd_di); free(fp); @@ -17491,9 +17420,6 @@ static void func_do_profile(ufunc_T *fp) } fp->uf_tml_idx = -1; - if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL - || fp->uf_tml_self == NULL) - return; /* out of memory */ fp->uf_profiling = TRUE; } @@ -17684,21 +17610,20 @@ script_autoload ( /* * Return the autoload script name for a function or variable name. - * Returns NULL when out of memory. */ static char_u *autoload_name(char_u *name) { - char_u *p; - char_u *scriptname; - /* Get the script file name: replace '#' with '/', append ".vim". */ - scriptname = xmalloc(STRLEN(name) + 14); + char_u *scriptname = xmalloc(STRLEN(name) + 14); STRCPY(scriptname, "autoload/"); STRCAT(scriptname, name); *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL; STRCAT(scriptname, ".vim"); + + char_u *p; while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL) *p = '/'; + return scriptname; } @@ -18652,8 +18577,6 @@ int store_session_globals(FILE *fd) * CR into \n and \r. */ p = vim_strsave_escaped(get_tv_string(&this_var->di_tv), (char_u *)"\\\"\n\r"); - if (p == NULL) /* out of memory */ - break; for (t = p; *t != NUL; ++t) if (*t == '\n') *t = 'n'; @@ -18696,17 +18619,13 @@ int store_session_globals(FILE *fd) */ void last_set_msg(scid_T scriptID) { - char_u *p; - if (scriptID != 0) { - p = home_replace_save(NULL, get_scriptname(scriptID)); - if (p != NULL) { - verbose_enter(); - MSG_PUTS(_("\n\tLast set from ")); - MSG_PUTS(p); - free(p); - verbose_leave(); - } + char_u *p = home_replace_save(NULL, get_scriptname(scriptID)); + verbose_enter(); + MSG_PUTS(_("\n\tLast set from ")); + MSG_PUTS(p); + free(p); + verbose_leave(); } } @@ -19107,10 +19026,8 @@ static void on_job_data(RStream *rstream, void *data, bool eof, char *type) static void apply_job_autocmds(Job *job, char *name, char *type, char *str) { - list_T *list; - // Create the list which will be set to v:job_data - list = list_alloc(); + list_T *list = list_alloc(); list_append_number(list, job_id(job)); list_append_string(list, (uint8_t *)type, -1); diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index ce70f59c27..fb6ec70f57 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -1853,7 +1853,7 @@ int viminfo_readline(vir_T *virp) * * Check for a long line as written by viminfo_writestring(). * - * Return the string in allocated memory (NULL when out of memory). + * Return the string in allocated memory (or NULL to indicate failure). */ char_u * viminfo_readstring ( diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index 1876276e9f..17c0f3bf3e 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -3012,8 +3012,6 @@ void ex_scriptencoding(exarg_T *eap) if (*eap->arg != NUL) { name = enc_canonize(eap->arg); - if (name == NULL) /* out of memory */ - return; } else name = eap->arg; diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index e39eac993c..701a51799f 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -3545,10 +3545,8 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp) for (l = repl; *l; ++l) if (vim_strchr(ESCAPE_CHARS, *l) != NULL) { l = vim_strsave_escaped(repl, ESCAPE_CHARS); - if (l != NULL) { - free(repl); - repl = l; - } + free(repl); + repl = l; break; } } @@ -3559,10 +3557,8 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp) char_u *l; l = vim_strsave_escaped(repl, (char_u *)"!"); - if (l != NULL) { - free(repl); - repl = l; - } + free(repl); + repl = l; } p = repl_cmdline(eap, p, srclen, repl, cmdlinep); @@ -7913,8 +7909,7 @@ makeopens ( return FAIL; } else if (ssop_flags & SSOP_CURDIR) { sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow); - if (sname == NULL - || fputs("cd ", fd) < 0 + if (fputs("cd ", fd) < 0 || ses_put_fname(fd, sname, &ssop_flags) == FAIL || put_eol(fd) == FAIL) { free(sname); @@ -8539,17 +8534,13 @@ static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp) * Write a file name to the session file. * Takes care of the "slash" option in 'sessionoptions' and escapes special * characters. - * Returns FAIL if writing fails or out of memory. + * Returns FAIL if writing fails. */ static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp) { - char_u *sname; char_u *p; - int retval = OK; - sname = home_replace_save(NULL, name); - if (sname == NULL) - return FAIL; + char_u *sname = home_replace_save(NULL, name); if (*flagp & SSOP_SLASH) { /* change all backslashes to forward slashes */ @@ -8561,13 +8552,9 @@ static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp) /* escape special characters */ p = vim_strsave_fnameescape(sname, FALSE); free(sname); - if (p == NULL) - return FAIL; /* write the result */ - if (fputs((char *)p, fd) < 0) - retval = FAIL; - + bool retval = fputs((char *)p, fd) < 0 ? FAIL : OK; free(p); return retval; } diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 994b2f510f..02c82eaf21 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -28,6 +28,7 @@ #include "nvim/ex_eval.h" #include "nvim/farsi.h" #include "nvim/fileio.h" +#include "nvim/func_attr.h" #include "nvim/getchar.h" #include "nvim/if_cscope.h" #include "nvim/indent.h" @@ -2307,10 +2308,8 @@ char_u *save_cmdline_alloc(void) */ void restore_cmdline_alloc(char_u *p) { - if (p != NULL) { - restore_cmdline((struct cmdline_info *)p); - free(p); - } + restore_cmdline((struct cmdline_info *)p); + free(p); } /* @@ -2949,27 +2948,21 @@ void ExpandEscape(expand_T *xp, char_u *str, int numfiles, char_u **files, int o /* for ":set path=" we need to escape spaces twice */ if (xp->xp_backslash == XP_BS_THREE) { p = vim_strsave_escaped(files[i], (char_u *)" "); - if (p != NULL) { - free(files[i]); - files[i] = p; + free(files[i]); + files[i] = p; #if defined(BACKSLASH_IN_FILENAME) - p = vim_strsave_escaped(files[i], (char_u *)" "); - if (p != NULL) { - free(files[i]); - files[i] = p; - } + p = vim_strsave_escaped(files[i], (char_u *)" "); + free(files[i]); + files[i] = p; #endif - } } #ifdef BACKSLASH_IN_FILENAME p = vim_strsave_fnameescape(files[i], FALSE); #else p = vim_strsave_fnameescape(files[i], xp->xp_shell); #endif - if (p != NULL) { - free(files[i]); - files[i] = p; - } + free(files[i]); + files[i] = p; /* If 'str' starts with "\~", replace "~" at start of * files[i] with "\~". */ @@ -2989,10 +2982,8 @@ void ExpandEscape(expand_T *xp, char_u *str, int numfiles, char_u **files, int o */ for (i = 0; i < numfiles; ++i) { p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); - if (p != NULL) { - free(files[i]); - files[i] = p; - } + free(files[i]); + files[i] = p; } } } @@ -3003,7 +2994,7 @@ void ExpandEscape(expand_T *xp, char_u *str, int numfiles, char_u **files, int o * after a Vim command, or, when "shell" is non-zero, a shell command. * Returns the result in allocated memory. */ -char_u *vim_strsave_fnameescape(char_u *fname, int shell) +char_u *vim_strsave_fnameescape(char_u *fname, int shell) FUNC_ATTR_NONNULL_RET { char_u *p; #ifdef BACKSLASH_IN_FILENAME @@ -3018,12 +3009,10 @@ char_u *vim_strsave_fnameescape(char_u *fname, int shell) p = vim_strsave_escaped(fname, buf); #else p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); - if (shell && csh_like_shell() && p != NULL) { - char_u *s; - + if (shell && csh_like_shell()) { /* For csh and similar shells need to put two backslashes before '!'. * One is taken by Vim, one by the shell. */ - s = vim_strsave_escaped(p, (char_u *)"!"); + char_u *s = vim_strsave_escaped(p, (char_u *)"!"); free(p); p = s; } @@ -3031,8 +3020,9 @@ char_u *vim_strsave_fnameescape(char_u *fname, int shell) /* '>' and '+' are special at the start of some commands, e.g. ":edit" and * ":write". "cd -" has a special meaning. */ - if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) + if (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)) { escape_fname(&p); + } return p; } @@ -3657,10 +3647,14 @@ ExpandFromContext ( return FAIL; } - if (xp->xp_context == EXPAND_SHELLCMD) - return expand_shellcmd(pat, num_file, file, flags); - if (xp->xp_context == EXPAND_OLD_SETTING) - return ExpandOldSetting(num_file, file); + if (xp->xp_context == EXPAND_SHELLCMD) { + expand_shellcmd(pat, num_file, file, flags); + return OK; + } + if (xp->xp_context == EXPAND_OLD_SETTING) { + ExpandOldSetting(num_file, file); + return OK; + } if (xp->xp_context == EXPAND_BUFFERS) return ExpandBufnames(pat, num_file, file, options); if (xp->xp_context == EXPAND_TAGS @@ -3743,10 +3737,12 @@ ExpandFromContext ( ret = FAIL; for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) if (xp->xp_context == tab[i].context) { - if (tab[i].ic) + if (tab[i].ic) { regmatch.rm_ic = TRUE; - ret = ExpandGeneric(xp, ®match, num_file, file, - tab[i].func, tab[i].escaped); + } + ExpandGeneric(xp, ®match, num_file, file, tab[i].func, + tab[i].escaped); + ret = OK; break; } } @@ -3762,10 +3758,8 @@ ExpandFromContext ( * Generic function for command line completion. It calls a function to * obtain strings, one by one. The strings are matched against a regexp * program. Matching strings are copied into an array, which is returned. - * - * Returns OK when no problems encountered, FAIL for error. */ -int ExpandGeneric( +void ExpandGeneric( expand_T *xp, regmatch_T *regmatch, int *num_file, @@ -3790,7 +3784,7 @@ int ExpandGeneric( } } if (count == 0) - return OK; + return; *num_file = count; *file = (char_u **)xmalloc(count * sizeof(char_u *)); @@ -3832,15 +3826,12 @@ int ExpandGeneric( /* Reset the variables used for special highlight names expansion, so that * they don't show up when getting normal highlight names by ID. */ reset_expand_highlight(); - - return OK; } /* * Complete a shell command. - * Returns FAIL or OK; */ -static int +static void expand_shellcmd ( char_u *filepat, /* pattern to match with command names */ int *num_file, /* return: number of matches */ @@ -3928,7 +3919,6 @@ expand_shellcmd ( free(pat); if (mustfree) free(path); - return OK; } /* diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 8b53ab3d31..129f1a1de2 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2087,10 +2087,7 @@ void set_forced_fenc(exarg_T *eap) { if (eap->force_enc != 0) { char_u *fenc = enc_canonize(eap->cmd + eap->force_enc); - - if (fenc != NULL) - set_string_option_direct((char_u *)"fenc", -1, - fenc, OPT_FREE|OPT_LOCAL, 0); + set_string_option_direct((char_u *)"fenc", -1, fenc, OPT_FREE|OPT_LOCAL, 0); free(fenc); } } @@ -5933,8 +5930,6 @@ void do_autocmd(char_u *arg, int forceit) * Check for a legal group name. If not, use AUGROUP_ALL. */ group = au_get_grouparg(&arg); - if (arg == NULL) /* out of memory */ - return; /* * Scan over the events. @@ -6271,8 +6266,6 @@ do_doautocmd ( * Check for a legal group name. If not, use AUGROUP_ALL. */ group = au_get_grouparg(&arg); - if (arg == NULL) /* out of memory */ - return FAIL; if (*arg == '*') { EMSG(_("E217: Can't execute autocommands for ALL events")); @@ -7204,8 +7197,7 @@ set_context_in_autocmd ( include_groups = FALSE; p = arg; group = au_get_grouparg(&arg); - if (group == AUGROUP_ERROR) - return NULL; + /* If there only is a group name that's what we expand. */ if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1])) { arg = p; diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index 57b0cf9a31..b169899cea 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -26,6 +26,7 @@ #include "nvim/ex_docmd.h" #include "nvim/ex_getln.h" #include "nvim/farsi.h" +#include "nvim/func_attr.h" #include "nvim/main.h" #include "nvim/mbyte.h" #include "nvim/memline.h" @@ -2584,7 +2585,7 @@ int input_available(void) * Return 0 for success * 1 for invalid arguments * 2 for no match - * 4 for out of mem + * 4 for out of mem (deprecated, WON'T HAPPEN) * 5 for entry not unique */ int @@ -3724,10 +3725,6 @@ eval_map_expr ( vim_unescape_csi(expr); save_cmd = save_cmdline_alloc(); - if (save_cmd == NULL) { - free(expr); - return NULL; - } /* Forbid changing text or using ":normal" to avoid most of the bad side * effects. Also restore the cursor position. */ @@ -3764,17 +3761,13 @@ static bool is_user_input(int k) /* * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result * can be put in the typeahead buffer. - * Returns NULL when out of memory. */ char_u *vim_strsave_escape_csi(char_u *p) { - char_u *res; - char_u *s, *d; - /* Need a buffer to hold up to three times as much. */ - res = xmalloc(STRLEN(p) * 3 + 1); - d = res; - for (s = p; *s != NUL; ) { + char_u *res = xmalloc(STRLEN(p) * 3 + 1); + char_u *d = res; + for (char_u *s = p; *s != NUL; ) { if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL) { /* Copy special key unmodified. */ *d++ = *s++; diff --git a/src/nvim/hashtab.c b/src/nvim/hashtab.c index f8fd8f44af..85cd59f59f 100644 --- a/src/nvim/hashtab.c +++ b/src/nvim/hashtab.c @@ -171,7 +171,7 @@ void hash_debug_results(void) /// in the new item (@see hashitem_T). Must not be NULL. /// /// @return OK if success. -/// FAIL if key already present, or out of memory. +/// FAIL if key already present int hash_add(hashtab_T *ht, char_u *key) { hash_T hash = hash_hash(key); @@ -180,7 +180,8 @@ int hash_add(hashtab_T *ht, char_u *key) EMSG2(_(e_intern2), "hash_add()"); return FAIL; } - return hash_add_item(ht, hi, key, hash); + hash_add_item(ht, hi, key, hash); + return OK; } /// Add item "hi" for key "key" to hashtable "ht". @@ -190,16 +191,8 @@ int hash_add(hashtab_T *ht, char_u *key) /// @param key Pointer to the key for the new item. The key has to be contained /// in the new item (@see hashitem_T). Must not be NULL. /// @param hash The precomputed hash value for the key. -/// -/// @return OK if success. -/// FAIL if out of memory. -int hash_add_item(hashtab_T *ht, hashitem_T *hi, char_u *key, hash_T hash) +void hash_add_item(hashtab_T *ht, hashitem_T *hi, char_u *key, hash_T hash) { - // If resizing failed before and it fails again we can't add an item. - if (ht->ht_error && (hash_may_resize(ht, 0) == FAIL)) { - return FAIL; - } - ht->ht_used++; if (hi->hi_key == NULL) { ht->ht_filled++; @@ -208,7 +201,7 @@ int hash_add_item(hashtab_T *ht, hashitem_T *hi, char_u *key, hash_T hash) hi->hi_hash = hash; // When the space gets low may resize the array. - return hash_may_resize(ht, 0); + hash_may_resize(ht, 0); } /// Remove item "hi" from hashtable "ht". @@ -250,14 +243,11 @@ void hash_unlock(hashtab_T *ht) /// - Shrink when too much empty space. /// - Grow when not enough empty space. /// If non-zero, passed minitems will be used. -/// -/// @return OK if success. -/// FAIL if out of memory. -static int hash_may_resize(hashtab_T *ht, size_t minitems) +static void hash_may_resize(hashtab_T *ht, size_t minitems) { // Don't resize a locked table. if (ht->ht_locked > 0) { - return OK; + return; } #ifdef HT_DEBUG @@ -276,7 +266,7 @@ static int hash_may_resize(hashtab_T *ht, size_t minitems) // items are required for the lookup to decide a key isn't there. if ((ht->ht_filled < HT_INIT_SIZE - 1) && (ht->ht_array == ht->ht_smallarray)) { - return OK; + return; } // Grow or refill the array when it's more than 2/3 full (including @@ -285,7 +275,7 @@ static int hash_may_resize(hashtab_T *ht, size_t minitems) // at least 1/4 full (avoids repeated grow-shrink operations) size_t oldsize = ht->ht_mask + 1; if ((ht->ht_filled * 3 < oldsize * 2) && (ht->ht_used > oldsize / 5)) { - return OK; + return; } if (ht->ht_used > 1000) { @@ -309,10 +299,8 @@ static int hash_may_resize(hashtab_T *ht, size_t minitems) while (newsize < minsize) { // make sure it's always a power of 2 newsize <<= 1; - if (newsize == 0) { - // overflow - return FAIL; - } + // assert newsize didn't overflow + assert(newsize != 0); } bool newarray_is_small = newsize == HT_INIT_SIZE; @@ -364,9 +352,6 @@ static int hash_may_resize(hashtab_T *ht, size_t minitems) ht->ht_array = newarray; ht->ht_mask = newmask; ht->ht_filled = ht->ht_used; - ht->ht_error = false; - - return OK; } /// Get the hash number for a key. diff --git a/src/nvim/hashtab.h b/src/nvim/hashtab.h index c268175b01..e4ad5e57fe 100644 --- a/src/nvim/hashtab.h +++ b/src/nvim/hashtab.h @@ -59,8 +59,6 @@ typedef struct hashtable_S { size_t ht_used; /// number of items used size_t ht_filled; /// number of items used or removed int ht_locked; /// counter for hash_lock() - bool ht_error; /// when set growing failed, can't add more - /// items before growing works hashitem_T *ht_array; /// points to the array, allocated when it's /// not "ht_smallarray" hashitem_T ht_smallarray[HT_INIT_SIZE]; /// initial array @@ -69,4 +67,5 @@ typedef struct hashtable_S { #ifdef INCLUDE_GENERATED_DECLARATIONS # include "hashtab.h.generated.h" #endif + #endif // NVIM_HASHTAB_H diff --git a/src/nvim/mark.c b/src/nvim/mark.c index 1739510f41..43cc3218af 100644 --- a/src/nvim/mark.c +++ b/src/nvim/mark.c @@ -1416,8 +1416,7 @@ void copy_viminfo_marks(vir_T *virp, FILE *fp_out, int count, int eof, int flags if (fp_out == NULL && (flags & (VIF_GET_OLDFILES | VIF_FORCEIT))) { list = list_alloc(); - if (list != NULL) - set_vim_var_list(VV_OLDFILES, list); + set_vim_var_list(VV_OLDFILES, list); } num_marked_files = get_viminfo_parameter('\''); diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c index 899b94e0fb..9b651b3c12 100644 --- a/src/nvim/mbyte.c +++ b/src/nvim/mbyte.c @@ -84,6 +84,7 @@ #include "nvim/charset.h" #include "nvim/cursor.h" #include "nvim/fileio.h" +#include "nvim/func_attr.h" #include "nvim/memline.h" #include "nvim/message.h" #include "nvim/misc1.h" @@ -3308,24 +3309,23 @@ char_u * enc_skip(char_u *p) * Find the canonical name for encoding "enc". * When the name isn't recognized, returns "enc" itself, but with all lower * case characters and '_' replaced with '-'. - * Returns an allocated string. NULL for out-of-memory. + * Returns an allocated string. */ -char_u * enc_canonize(char_u *enc) +char_u *enc_canonize(char_u *enc) FUNC_ATTR_NONNULL_RET { - char_u *r; char_u *p, *s; int i; if (STRCMP(enc, "default") == 0) { /* Use the default encoding as it's found by set_init_1(). */ - r = get_encoding_default(); + char_u *r = get_encoding_default(); if (r == NULL) r = (char_u *)"latin1"; return vim_strsave(r); } /* copy "enc" to allocated memory, with room for two '-' */ - r = xmalloc(STRLEN(enc) + 3); + char_u *r = xmalloc(STRLEN(enc) + 3); /* Make it all lower case and replace '_' with '-'. */ p = r; for (s = enc; *s != NUL; ++s) { @@ -3411,7 +3411,7 @@ char_u * enc_locale() s = (char *)os_getenv("LANG"); if (s == NULL || *s == NUL) - return FAIL; + return NULL; /* The most generic locale format is: * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]] @@ -3528,8 +3528,6 @@ static char_u * iconv_string(vimconv_T *vcp, char_u *str, int slen, int *unconvl memmove(p, result, done); free(result); result = p; - if (result == NULL) /* out of memory */ - break; } to = (char *)result + done; diff --git a/src/nvim/menu.c b/src/nvim/menu.c index 4f45aa9e34..4d75bc1475 100644 --- a/src/nvim/menu.c +++ b/src/nvim/menu.c @@ -186,11 +186,8 @@ ex_menu ( for (i = 0; i < MENU_INDEX_TIP; ++i) if (modes & (1 << i)) { p = popup_mode_name(menu_path, i); - if (p != NULL) { - menu_nable_recurse(root_menu, p, MENU_ALL_MODES, - enable); - free(p); - } + menu_nable_recurse(root_menu, p, MENU_ALL_MODES, enable); + free(p); } } menu_nable_recurse(root_menu, menu_path, modes, enable); @@ -208,10 +205,8 @@ ex_menu ( for (i = 0; i < MENU_INDEX_TIP; ++i) if (modes & (1 << i)) { p = popup_mode_name(menu_path, i); - if (p != NULL) { - remove_menu(&root_menu, p, MENU_ALL_MODES, TRUE); - free(p); - } + remove_menu(&root_menu, p, MENU_ALL_MODES, TRUE); + free(p); } } @@ -242,13 +237,10 @@ ex_menu ( for (i = 0; i < MENU_INDEX_TIP; ++i) if (modes & (1 << i)) { p = popup_mode_name(menu_path, i); - if (p != NULL) { - /* Include all modes, to make ":amenu" work */ - menuarg.modes = modes; - add_menu_path(p, &menuarg, pri_tab, map_to - ); - free(p); - } + // Include all modes, to make ":amenu" work + menuarg.modes = modes; + add_menu_path(p, &menuarg, pri_tab, map_to); + free(p); } } @@ -1153,10 +1145,9 @@ get_menu_cmd_modes ( */ static char_u *popup_mode_name(char_u *name, int idx) { - char_u *p; int len = (int)STRLEN(name); - p = vim_strnsave(name, len + 1); + char_u *p = vim_strnsave(name, len + 1); memmove(p + 6, p + 5, (size_t)(len - 4)); p[5] = menu_mode_chars[idx]; diff --git a/src/nvim/message.c b/src/nvim/message.c index e099c9b0a5..281270155a 100644 --- a/src/nvim/message.c +++ b/src/nvim/message.c @@ -21,6 +21,7 @@ #include "nvim/eval.h" #include "nvim/ex_eval.h" #include "nvim/fileio.h" +#include "nvim/func_attr.h" #include "nvim/getchar.h" #include "nvim/mbyte.h" #include "nvim/memory.h" @@ -2710,51 +2711,49 @@ do_dialog ( ++no_wait_return; hotkeys = msg_show_console_dialog(message, buttons, dfltbutton); - if (hotkeys != NULL) { - for (;; ) { - /* Get a typed character directly from the user. */ - c = get_keystroke(); - switch (c) { - case CAR: /* User accepts default option */ - case NL: + for (;; ) { + /* Get a typed character directly from the user. */ + c = get_keystroke(); + switch (c) { + case CAR: /* User accepts default option */ + case NL: + retval = dfltbutton; + break; + case Ctrl_C: /* User aborts/cancels */ + case ESC: + retval = 0; + break; + default: /* Could be a hotkey? */ + if (c < 0) /* special keys are ignored here */ + continue; + if (c == ':' && ex_cmd) { retval = dfltbutton; + ins_char_typebuf(':'); break; - case Ctrl_C: /* User aborts/cancels */ - case ESC: - retval = 0; - break; - default: /* Could be a hotkey? */ - if (c < 0) /* special keys are ignored here */ - continue; - if (c == ':' && ex_cmd) { - retval = dfltbutton; - ins_char_typebuf(':'); - break; - } + } - /* Make the character lowercase, as chars in "hotkeys" are. */ - c = vim_tolower(c); - retval = 1; - for (i = 0; hotkeys[i]; ++i) { - if (has_mbyte) { - if ((*mb_ptr2char)(hotkeys + i) == c) - break; - i += (*mb_ptr2len)(hotkeys + i) - 1; - } else if (hotkeys[i] == c) + /* Make the character lowercase, as chars in "hotkeys" are. */ + c = vim_tolower(c); + retval = 1; + for (i = 0; hotkeys[i]; ++i) { + if (has_mbyte) { + if ((*mb_ptr2char)(hotkeys + i) == c) break; - ++retval; - } - if (hotkeys[i]) + i += (*mb_ptr2len)(hotkeys + i) - 1; + } else if (hotkeys[i] == c) break; - /* No hotkey match, so keep waiting */ - continue; + ++retval; } - break; + if (hotkeys[i]) + break; + /* No hotkey match, so keep waiting */ + continue; } - - free(hotkeys); + break; } + free(hotkeys); + State = oldState; setmouse(); --no_wait_return; @@ -2868,9 +2867,10 @@ static char_u * console_dialog_alloc(const char_u *message, * the button is used. * The hotkeys can be multi-byte characters, but without combining chars. * - * Returns an allocated string with hotkeys, or NULL for error. + * Returns an allocated string with hotkeys. */ static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton) + FUNC_ATTR_NONNULL_RET { bool has_hotkey[HAS_HOTKEY_LEN]; char_u *hotk = console_dialog_alloc(message, buttons, has_hotkey); diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c index 7e6dec119b..f025ff7f65 100644 --- a/src/nvim/misc1.c +++ b/src/nvim/misc1.c @@ -24,6 +24,7 @@ #include "nvim/ex_docmd.h" #include "nvim/ex_getln.h" #include "nvim/fileio.h" +#include "nvim/func_attr.h" #include "nvim/fold.h" #include "nvim/getchar.h" #include "nvim/indent.h" @@ -2847,12 +2848,10 @@ expand_env_esc ( if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) { char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); - if (p != NULL) { - if (mustfree) - free(var); - var = p; - mustfree = TRUE; - } + if (mustfree) + free(var); + var = p; + mustfree = TRUE; } if (var != NULL && *var != NUL @@ -3280,20 +3279,17 @@ home_replace ( /* * Like home_replace, store the replaced string in allocated memory. - * When something fails, NULL is returned. */ char_u * home_replace_save ( buf_T *buf, /* when not NULL, check for help files */ char_u *src /* input file name */ -) +) FUNC_ATTR_NONNULL_RET { - char_u *dst; - size_t len = 3; /* space for "~/" and trailing NUL */ if (src != NULL) /* just in case */ len += STRLEN(src); - dst = xmalloc(len); + char_u *dst = xmalloc(len); home_replace(buf, src, dst, (int)len, TRUE); return dst; } diff --git a/src/nvim/misc2.c b/src/nvim/misc2.c index c064b4c5e6..680dff394f 100644 --- a/src/nvim/misc2.c +++ b/src/nvim/misc2.c @@ -318,8 +318,6 @@ int call_shell(char_u *cmd, ShellOpts opts, char_u *extra_shell_arg) if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0) { ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE); - if (ecmd == NULL) - ecmd = cmd; } ncmd = xmalloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1); STRCPY(ncmd, p_sxq); diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 4f1cd5d526..e80ffac6e1 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -943,14 +943,12 @@ do_execreg ( (char_u *) "\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE); - if (p != NULL) { - /* When in Visual mode "'<,'>" will be prepended to the command. - * Remove it when it's already there. */ - if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0) - retval = put_in_typebuf(p + 5, TRUE, TRUE, silent); - else - retval = put_in_typebuf(p, TRUE, TRUE, silent); - } + /* When in Visual mode "'<,'>" will be prepended to the command. + * Remove it when it's already there. */ + if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0) + retval = put_in_typebuf(p + 5, TRUE, TRUE, silent); + else + retval = put_in_typebuf(p, TRUE, TRUE, silent); free(p); } else if (regname == '=') { p = get_expr_line(); @@ -988,8 +986,6 @@ do_execreg ( return FAIL; } escaped = vim_strsave_escape_csi(y_current->y_array[i]); - if (escaped == NULL) - return FAIL; retval = ins_typebuf(escaped, remap, 0, TRUE, silent); free(escaped); if (retval == FAIL) diff --git a/src/nvim/option.c b/src/nvim/option.c index 5690d2c61f..8390efe979 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -3971,10 +3971,8 @@ did_set_string_option ( if (errmsg == NULL) { /* canonize the value, so that STRCMP() can be used on it */ p = enc_canonize(*varp); - if (p != NULL) { - free(*varp); - *varp = p; - } + free(*varp); + *varp = p; if (varp == &p_enc) { errmsg = mb_init(); redraw_titles(); @@ -4000,18 +3998,8 @@ did_set_string_option ( } else if (varp == &p_penc) { /* Canonize printencoding if VIM standard one */ p = enc_canonize(p_penc); - if (p != NULL) { - free(p_penc); - p_penc = p; - } else { - /* Ensure lower case and '-' for '_' */ - for (s = p_penc; *s != NUL; s++) { - if (*s == '_') - *s = '-'; - else - *s = TOLOWER_ASC(*s); - } - } + free(p_penc); + p_penc = p; } else if (varp == &curbuf->b_p_keymap) { /* load or unload key mapping tables */ errmsg = keymap_init(); @@ -7396,10 +7384,9 @@ int ExpandSettings(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u *** return OK; } -int ExpandOldSetting(int *num_file, char_u ***file) +void ExpandOldSetting(int *num_file, char_u ***file) { - char_u *var = NULL; /* init for GCC */ - char_u *buf; + char_u *var = NULL; *num_file = 0; *file = (char_u **)xmalloc(sizeof(char_u *)); @@ -7422,13 +7409,7 @@ int ExpandOldSetting(int *num_file, char_u ***file) /* A backslash is required before some characters. This is the reverse of * what happens in do_set(). */ - buf = vim_strsave_escaped(var, escape_chars); - - if (buf == NULL) { - free(*file); - *file = NULL; - return FAIL; - } + char_u *buf = vim_strsave_escaped(var, escape_chars); #ifdef BACKSLASH_IN_FILENAME /* For MS-Windows et al. we don't double backslashes at the start and @@ -7444,7 +7425,6 @@ int ExpandOldSetting(int *num_file, char_u ***file) *file[0] = buf; *num_file = 1; - return OK; } /* diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 0e40b8eb3e..492ac1e606 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -3239,8 +3239,7 @@ int get_errorlist(win_T *wp, list_T *list) if (bufnum != 0 && (buflist_findnr(bufnum) == NULL)) bufnum = 0; - if ((dict = dict_alloc()) == NULL) - return FAIL; + dict = dict_alloc(); list_append_dict(list, dict); buf[0] = qfp->qf_type; diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index eab8c58b36..ea61436e05 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -6500,10 +6500,8 @@ static int vim_regsub_both(char_u *source, char_u *dest, int copy, int magic, in if (had_backslash && backslash) { /* Backslashes will be consumed, need to double them. */ s = vim_strsave_escaped(eval_result, (char_u *)"\\"); - if (s != NULL) { - free(eval_result); - eval_result = s; - } + free(eval_result); + eval_result = s; } dst += STRLEN(eval_result); diff --git a/src/nvim/search.c b/src/nvim/search.c index f4b034d3f6..ee81f6c851 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -23,6 +23,7 @@ #include "nvim/farsi.h" #include "nvim/fileio.h" #include "nvim/fold.h" +#include "nvim/func_attr.h" #include "nvim/getchar.h" #include "nvim/indent.h" #include "nvim/main.h" @@ -177,15 +178,8 @@ search_regcomp ( } if (curwin->w_p_rl && *curwin->w_p_rlc == 's') { - char_u *rev_pattern; - - rev_pattern = reverse_text(pat); - if (rev_pattern == NULL) - mr_pattern = pat; /* out of memory, keep normal pattern. */ - else { - mr_pattern = rev_pattern; - mr_pattern_alloced = TRUE; - } + mr_pattern = reverse_text(pat); + mr_pattern_alloced = TRUE; } else mr_pattern = pat; @@ -224,7 +218,7 @@ char_u *get_search_pat(void) * * TODO(philix): move reverse_text() to strings.c */ -char_u *reverse_text(char_u *s) +char_u *reverse_text(char_u *s) FUNC_ATTR_NONNULL_RET { /* * Reverse the pattern. diff --git a/src/nvim/spell.c b/src/nvim/spell.c index 8214d95c00..ca522191b9 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -4438,7 +4438,7 @@ static afffile_T *spell_read_aff(spellinfo_T *spin, char_u *fname) if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL) { // Setup for conversion from "ENC" to 'encoding'. aff->af_enc = enc_canonize(items[1]); - if (aff->af_enc != NULL && !spin->si_ascii + if (!spin->si_ascii && convert_setup(&spin->si_conv, aff->af_enc, p_enc) == FAIL) smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), @@ -5978,7 +5978,7 @@ static int spell_read_wordfile(spellinfo_T *spin, char_u *fname) // Setup for conversion to 'encoding'. line += 9; enc = enc_canonize(line); - if (enc != NULL && !spin->si_ascii + if (!spin->si_ascii && convert_setup(&spin->si_conv, enc, p_enc) == FAIL) smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), diff --git a/src/nvim/strings.c b/src/nvim/strings.c index 8c82186e15..45aec5841b 100644 --- a/src/nvim/strings.c +++ b/src/nvim/strings.c @@ -13,7 +13,9 @@ #include "nvim/ex_docmd.h" #include "nvim/ex_getln.h" #include "nvim/fileio.h" +#include "nvim/func_attr.h" #include "nvim/fold.h" +#include "nvim/func_attr.h" #include "nvim/getchar.h" #include "nvim/mark.h" #include "nvim/mbyte.h" @@ -64,6 +66,7 @@ char_u *vim_strnsave(char_u *string, int len) FUNC_ATTR_NONNULL_RET * by a backslash. */ char_u *vim_strsave_escaped(char_u *string, char_u *esc_chars) + FUNC_ATTR_NONNULL_RET { return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE); } @@ -74,10 +77,8 @@ char_u *vim_strsave_escaped(char_u *string, char_u *esc_chars) * Escape the characters with "cc". */ char_u *vim_strsave_escaped_ext(char_u *string, char_u *esc_chars, int cc, int bsl) + FUNC_ATTR_NONNULL_RET { - char_u *p; - char_u *p2; - char_u *escaped_string; unsigned length; int l; @@ -86,7 +87,7 @@ char_u *vim_strsave_escaped_ext(char_u *string, char_u *esc_chars, int cc, int b * Then allocate the memory and insert them. */ length = 1; /* count the trailing NUL */ - for (p = string; *p; p++) { + for (char_u *p = string; *p; p++) { if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) { length += l; /* count a multibyte char */ p += l - 1; @@ -96,9 +97,10 @@ char_u *vim_strsave_escaped_ext(char_u *string, char_u *esc_chars, int cc, int b ++length; /* count a backslash */ ++length; /* count an ordinary char */ } - escaped_string = xmalloc(length); - p2 = escaped_string; - for (p = string; *p; p++) { + + char_u *escaped_string = xmalloc(length); + char_u *p2 = escaped_string; + for (char_u *p = string; *p; p++) { if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) { memmove(p2, p, (size_t)l); p2 += l; @@ -214,11 +216,9 @@ char_u *vim_strsave_up(char_u *string) * Like vim_strnsave(), but make all characters uppercase. * This uses ASCII lower-to-upper case translation, language independent. */ -char_u *vim_strnsave_up(char_u *string, int len) +char_u *vim_strnsave_up(char_u *string, int len) FUNC_ATTR_NONNULL_RET { - char_u *p1; - - p1 = vim_strnsave(string, len); + char_u *p1 = vim_strnsave(string, len); vim_strup(p1); return p1; } @@ -251,19 +251,16 @@ char_u *strup_save(char_u *orig) int l; if (enc_utf8) { - int c, uc; - int newl; - char_u *s; - - c = utf_ptr2char(p); - uc = utf_toupper(c); + int c = utf_ptr2char(p); + int uc = utf_toupper(c); /* Reallocate string when byte count changes. This is rare, * thus it's OK to do another malloc()/free(). */ l = utf_ptr2len(p); - newl = utf_char2len(uc); + int newl = utf_char2len(uc); if (newl != l) { - s = xmalloc(STRLEN(res) + 1 + newl - l); + // TODO(philix): use xrealloc() in strup_save() + char_u *s = xmalloc(STRLEN(res) + 1 + newl - l); memmove(s, res, p - res); STRCPY(s + (p - res) + newl, p + l); p = s + (p - res); @@ -518,7 +515,6 @@ int has_non_ascii(char_u *s) /* * Concatenate two strings and return the result in allocated memory. - * Returns NULL when out of memory. */ char_u *concat_str(char_u *str1, char_u *str2) FUNC_ATTR_NONNULL_RET { diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index 6b4ab867f2..621c6f0d55 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -4399,10 +4399,6 @@ syn_cmd_region ( ++key_end; free(key); key = vim_strnsave_up(rest, (int)(key_end - rest)); - if (key == NULL) { /* out of memory */ - rest = NULL; - break; - } if (STRCMP(key, "MATCHGROUP") == 0) item = ITEM_MATCHGROUP; else if (STRCMP(key, "START") == 0) @@ -4692,12 +4688,8 @@ static void syn_combine_list(short **clstr1, short **clstr2, int list_op) */ static int syn_scl_name2id(char_u *name) { - char_u *name_u; - - /* Avoid using stricmp() too much, it's slow on some systems */ - name_u = vim_strsave_up(name); - if (name_u == NULL) - return 0; + // Avoid using stricmp() too much, it's slow on some systems + char_u *name_u = vim_strsave_up(name); int i; for (i = curwin->w_s->b_syn_clusters.ga_len; --i >= 0; ) { if (SYN_CLSTR(curwin->w_s)[i].scl_name_u != NULL diff --git a/src/nvim/tag.c b/src/nvim/tag.c index 40611bd237..c9a065391b 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -784,8 +784,7 @@ do_tag ( cmd[len] = NUL; } - if ((dict = dict_alloc()) == NULL) - continue; + dict = dict_alloc(); list_append_dict(list, dict); dict_add_nr_str(dict, "text", 0L, tag_name); @@ -2822,8 +2821,7 @@ int get_tags(list_T *list, char_u *pat) if (STRNCMP(tp.tagname, "!_TAG_", 6) == 0) continue; - if ((dict = dict_alloc()) == NULL) - ret = FAIL; + dict = dict_alloc(); list_append_dict(list, dict); full_fname = tag_full_fname(&tp); diff --git a/src/nvim/undo.c b/src/nvim/undo.c index b7a3601d0f..37fa150aee 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -2725,8 +2725,6 @@ void u_eval_tree(u_header_T *first_uhp, list_T *list) while (uhp != NULL) { dict = dict_alloc(); - if (dict == NULL) - return; dict_add_nr_str(dict, "seq", uhp->uh_seq, NULL); dict_add_nr_str(dict, "time", (long)uhp->uh_time, NULL); if (uhp == curbuf->b_u_newhead) @@ -2737,13 +2735,11 @@ void u_eval_tree(u_header_T *first_uhp, list_T *list) dict_add_nr_str(dict, "save", uhp->uh_save_nr, NULL); if (uhp->uh_alt_next.ptr != NULL) { - list_T *alt_list = list_alloc(); + list_T *alt_list = list_alloc(); - if (alt_list != NULL) { - /* Recursive call to add alternate undo tree. */ - u_eval_tree(uhp->uh_alt_next.ptr, alt_list); - dict_add_list(dict, "alt", alt_list); - } + /* Recursive call to add alternate undo tree. */ + u_eval_tree(uhp->uh_alt_next.ptr, alt_list); + dict_add_list(dict, "alt", alt_list); } list_append_dict(list, dict); |