diff options
Diffstat (limited to 'src')
55 files changed, 944 insertions, 1650 deletions
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 03dd972df1..ffb62add3c 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -2235,9 +2235,8 @@ setfname ( close_buffer(NULL, obuf, DOBUF_WIPE, FALSE); } sfname = vim_strsave(sfname); - if (ffname == NULL || sfname == NULL) { + if (ffname == NULL) { free(sfname); - free(ffname); return FAIL; } #ifdef USE_FNAME_CASE @@ -4114,8 +4113,6 @@ chk_modeline ( while (s[-1] != ':'); s = linecopy = vim_strsave(s); /* copy the line, it will change */ - if (linecopy == NULL) - return FAIL; save_sourcing_lnum = sourcing_lnum; save_sourcing_name = sourcing_name; diff --git a/src/nvim/charset.c b/src/nvim/charset.c index 6618a7c22d..ae5da557d5 100644 --- a/src/nvim/charset.c +++ b/src/nvim/charset.c @@ -1910,10 +1910,8 @@ void backslash_halve(char_u *p) /// @return String with the number of backslashes halved. char_u* backslash_halve_save(char_u *p) { + // TODO(philix): simplify and improve backslash_halve_save algorithm char_u *res = vim_strsave(p); - if (res == NULL) { - return p; - } backslash_halve(res); return res; } diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 0e5d1c20b7..06416639bd 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -519,10 +519,6 @@ static void diff_check_unchanged(tabpage_T *tp, diff_T *dp) dp->df_lnum[i_org] + off_org, FALSE)); - if (line_org == NULL) { - return; - } - int i_new; for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new) { if (tp->tp_diffbuf[i_new] == NULL) { @@ -972,10 +968,7 @@ void ex_diffpatch(exarg_T *eap) if (curbuf->b_fname != NULL) { newname = vim_strnsave(curbuf->b_fname, (int)(STRLEN(curbuf->b_fname) + 4)); - - if (newname != NULL) { - STRCAT(newname, ".new"); - } + STRCAT(newname, ".new"); } // don't use a new tab page, each tab page has its own diffs @@ -1580,9 +1573,6 @@ static int diff_equal_entry(diff_T *dp, int idx1, int idx2) char_u *line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1], dp->df_lnum[idx1] + i, FALSE)); - if (line == NULL) { - return FALSE; - } int cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2], dp->df_lnum[idx2] + i, FALSE)); free(line); @@ -1886,9 +1876,6 @@ int diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) // Make a copy of the line, the next ml_get() will invalidate it. char_u *line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE)); - if (line_org == NULL) { - return FALSE; - } int idx = diff_buf_idx(wp->w_buffer); if (idx == DB_COUNT) { @@ -2283,16 +2270,14 @@ void ex_diffgetput(exarg_T *eap) break; } p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from], nr, FALSE)); - if (p != NULL) { - ml_append(lnum + i - 1, p, 0, FALSE); - free(p); - added++; - if (buf_empty && (curbuf->b_ml.ml_line_count == 2)) { - // Added the first line into an empty buffer, need to - // delete the dummy empty line. - buf_empty = FALSE; - ml_delete((linenr_T)2, FALSE); - } + ml_append(lnum + i - 1, p, 0, FALSE); + free(p); + added++; + if (buf_empty && (curbuf->b_ml.ml_line_count == 2)) { + // Added the first line into an empty buffer, need to + // delete the dummy empty line. + buf_empty = FALSE; + ml_delete((linenr_T)2, FALSE); } } new_count = dp->df_count[idx_to] + added; diff --git a/src/nvim/digraph.c b/src/nvim/digraph.c index b7f0c40788..443640d23d 100644 --- a/src/nvim/digraph.c +++ b/src/nvim/digraph.c @@ -1738,28 +1738,24 @@ char_u* keymap_init(void) keymap_unload(); do_cmdline_cmd((char_u *)"unlet! b:keymap_name"); } else { - char_u *buf; + char *buf; size_t buflen; // Source the keymap file. It will contain a ":loadkeymap" command // which will call ex_loadkeymap() below. buflen = STRLEN(curbuf->b_p_keymap) + STRLEN(p_enc) + 14; - buf = alloc((unsigned)buflen); - - if (buf == NULL) { - return e_outofmem; - } + buf = xmalloc(buflen); // try finding "keymap/'keymap'_'encoding'.vim" in 'runtimepath' - vim_snprintf((char *)buf, buflen, "keymap/%s_%s.vim", + vim_snprintf(buf, buflen, "keymap/%s_%s.vim", curbuf->b_p_keymap, p_enc); - if (source_runtime(buf, FALSE) == FAIL) { + if (source_runtime((char_u *)buf, FALSE) == FAIL) { // try finding "keymap/'keymap'.vim" in 'runtimepath' - vim_snprintf((char *)buf, buflen, "keymap/%s.vim", + vim_snprintf(buf, buflen, "keymap/%s.vim", curbuf->b_p_keymap); - if (source_runtime(buf, FALSE) == FAIL) { + if (source_runtime((char_u *)buf, FALSE) == FAIL) { free(buf); return (char_u *)N_("E544: Keymap file not found"); } @@ -1818,12 +1814,10 @@ void ex_loadkeymap(exarg_T *eap) s = skiptowhite(p); kp->to = vim_strnsave(p, (int)(s - p)); - if ((kp->from == NULL) - || (kp->to == NULL) - || (STRLEN(kp->from) + STRLEN(kp->to) >= KMAP_LLEN) + if ((STRLEN(kp->from) + STRLEN(kp->to) >= KMAP_LLEN) || (*kp->from == NUL) || (*kp->to == NUL)) { - if ((kp->to != NULL) && (*kp->to == NUL)) { + if (*kp->to == NUL) { EMSG(_("E791: Empty keymap entry")); } free(kp->from); diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 440787643e..a47a39a3ac 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -1727,8 +1727,6 @@ change_indent ( /* Save new line */ new_line = vim_strsave(ml_get_curline()); - if (new_line == NULL) - return; /* We only put back the new line up to the cursor */ new_line[curwin->w_cursor.col] = NUL; @@ -2128,10 +2126,7 @@ ins_compl_add ( match->cp_number = -1; if (flags & ORIGINAL_TEXT) match->cp_number = 0; - if ((match->cp_str = vim_strnsave(str, len)) == NULL) { - free(match); - return FAIL; - } + match->cp_str = vim_strnsave(str, len); match->cp_icase = icase; /* match-fname is: @@ -2210,18 +2205,16 @@ static void ins_compl_longest_match(compl_T *match) if (compl_leader == NULL) { /* First match, use it as a whole. */ compl_leader = vim_strsave(match->cp_str); - if (compl_leader != NULL) { - had_match = (curwin->w_cursor.col > compl_col); - ins_compl_delete(); - ins_bytes(compl_leader + ins_compl_len()); - ins_redraw(FALSE); + had_match = (curwin->w_cursor.col > compl_col); + ins_compl_delete(); + ins_bytes(compl_leader + ins_compl_len()); + ins_redraw(FALSE); - /* When the match isn't there (to avoid matching itself) remove it - * again after redrawing. */ - if (!had_match) - ins_compl_delete(); - compl_used_match = FALSE; - } + /* When the match isn't there (to avoid matching itself) remove it + * again after redrawing. */ + if (!had_match) + ins_compl_delete(); + compl_used_match = FALSE; } else { /* Reduce the text if this match differs from compl_leader. */ p = compl_leader; @@ -2328,9 +2321,10 @@ void set_completion(colnr_T startcol, list_T *list) compl_length = (int)curwin->w_cursor.col - (int)startcol; /* compl_pattern doesn't need to be set */ compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length); - if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, - -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) + if (ins_compl_add(compl_orig_text, -1, p_ic, NULL, NULL, 0, + ORIGINAL_TEXT, FALSE) != OK) { return; + } /* Handle like dictionary completion. */ ctrl_x_mode = CTRL_X_WHOLE_LINE; @@ -2873,14 +2867,12 @@ static int ins_compl_bs(void) free(compl_leader); compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col); - if (compl_leader != NULL) { - ins_compl_new_leader(); - if (compl_shown_match != NULL) - /* Make sure current match is not a hidden item. */ - compl_curr_match = compl_shown_match; - return NUL; - } - return K_BS; + ins_compl_new_leader(); + if (compl_shown_match != NULL) + /* Make sure current match is not a hidden item. */ + compl_curr_match = compl_shown_match; + + return NUL; } /* @@ -2980,8 +2972,7 @@ static void ins_compl_addleader(int c) free(compl_leader); compl_leader = vim_strnsave(ml_get_curline() + compl_col, (int)(curwin->w_cursor.col - compl_col)); - if (compl_leader != NULL) - ins_compl_new_leader(); + ins_compl_new_leader(); } } @@ -3003,15 +2994,10 @@ static void ins_compl_restart(void) */ static void ins_compl_set_original_text(char_u *str) { - char_u *p; - /* Replace the original text entry. */ if (compl_first_match->cp_flags & ORIGINAL_TEXT) { /* safety check */ - p = vim_strsave(str); - if (p != NULL) { - free(compl_first_match->cp_str); - compl_first_match->cp_str = p; - } + free(compl_first_match->cp_str); + compl_first_match->cp_str = vim_strsave(str); } } @@ -4356,8 +4342,6 @@ static int ins_complete(int c) ) { /* Match any word of at least two chars */ compl_pattern = vim_strsave((char_u *)"\\<\\k\\k"); - if (compl_pattern == NULL) - return FAIL; compl_col += curs_col; compl_length = 0; } else { @@ -4432,8 +4416,6 @@ static int ins_complete(int c) return FAIL; } else if (ctrl_x_mode == CTRL_X_CMDLINE) { compl_pattern = vim_strnsave(line, curs_col); - if (compl_pattern == NULL) - return FAIL; set_cmd_context(&compl_xp, compl_pattern, (int)STRLEN(compl_pattern), curs_col); if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL @@ -4513,8 +4495,6 @@ static int ins_complete(int c) line = ml_get(curwin->w_cursor.lnum); compl_length = curs_col - compl_col; compl_pattern = vim_strnsave(line + compl_col, compl_length); - if (compl_pattern == NULL) - return FAIL; } else if (ctrl_x_mode == CTRL_X_SPELL) { if (spell_bad_len > 0) compl_col = curs_col - spell_bad_len; @@ -4530,8 +4510,6 @@ static int ins_complete(int c) /* Need to obtain "line" again, it may have become invalid. */ line = ml_get(curwin->w_cursor.lnum); compl_pattern = vim_strnsave(line + compl_col, compl_length); - if (compl_pattern == NULL) - return FAIL; } else { EMSG2(_(e_intern2), "ins_complete()"); return FAIL; @@ -4568,8 +4546,8 @@ static int ins_complete(int c) /* Always add completion for the original text. */ free(compl_orig_text); compl_orig_text = vim_strnsave(line + compl_col, compl_length); - if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, - -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) { + if (ins_compl_add(compl_orig_text, -1, p_ic, NULL, NULL, 0, + ORIGINAL_TEXT, FALSE) != OK) { free(compl_pattern); compl_pattern = NULL; free(compl_orig_text); @@ -6227,11 +6205,10 @@ char_u *get_last_insert_save(void) if (last_insert == NULL) return NULL; s = vim_strsave(last_insert + last_insert_skip); - if (s != NULL) { - len = (int)STRLEN(s); - if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */ - s[len - 1] = NUL; - } + len = (int)STRLEN(s); + if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */ + s[len - 1] = NUL; + return s; } @@ -8006,8 +7983,6 @@ static int ins_tab(void) pos = curwin->w_cursor; cursor = &pos; saved_line = vim_strsave(ml_get_curline()); - if (saved_line == NULL) - return FALSE; ptr = saved_line + pos.col; } else { ptr = ml_get_cursor(); @@ -8411,7 +8386,6 @@ static colnr_T get_nolist_virtcol(void) */ static char_u *do_insert_char_pre(int c) { - char_u *res; char_u buf[MB_MAXBYTES + 1]; /* Return quickly when there is nothing to do. */ @@ -8429,7 +8403,7 @@ static char_u *do_insert_char_pre(int c) ++textlock; set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */ - res = NULL; + char_u *res = NULL; if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf)) { /* Get the value of v:char. It may be empty or more than one * character. Only use it when changed, otherwise continue with the diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 6e0681420b..4ddf037f64 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -1040,8 +1040,6 @@ var_redir_start ( /* Make a copy of the name, it is used in redir_lval until redir ends. */ redir_varname = vim_strsave(name); - if (redir_varname == NULL) - return FAIL; redir_lval = xcalloc(1, sizeof(lval_T)); @@ -1457,12 +1455,11 @@ int get_spellword(list_T *list, char_u **pp) */ typval_T *eval_expr(char_u *arg, char_u **nextcmd) { - typval_T *tv; + typval_T *tv = xmalloc(sizeof(typval_T)); - tv = (typval_T *)alloc(sizeof(typval_T)); - if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL) { + if (eval0(arg, tv, nextcmd, TRUE) == FAIL) { free(tv); - tv = NULL; + return NULL; } return tv; @@ -1485,7 +1482,6 @@ call_vim_function ( typval_T *rettv ) { - typval_T *argvars; long n; int len; int i; @@ -1493,9 +1489,7 @@ call_vim_function ( void *save_funccalp = NULL; int ret; - argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T))); - if (argvars == NULL) - return FAIL; + typval_T *argvars = xmalloc((argc + 1) * sizeof(typval_T)); for (i = 0; i < argc; i++) { /* Pass a NULL or empty argument as an empty string */ @@ -2541,8 +2535,6 @@ get_lval ( lp->ll_newkey = vim_strnsave(key, len); if (len == -1) clear_tv(&var1); - if (lp->ll_newkey == NULL) - p = NULL; break; } /* existing variable, need to check if it can be changed */ @@ -3429,20 +3421,19 @@ void del_menutrans_vars(void) static char_u *cat_prefix_varname(int prefix, char_u *name); static char_u *varnamebuf = NULL; -static int varnamebuflen = 0; +static size_t varnamebuflen = 0; /* * Function to concatenate a prefix and a variable name. */ static char_u *cat_prefix_varname(int prefix, char_u *name) { - int len; + size_t len = STRLEN(name) + 3; - len = (int)STRLEN(name) + 3; if (len > varnamebuflen) { free(varnamebuf); len += 10; /* some additional space */ - varnamebuf = alloc(len); + varnamebuf = xmalloc(len); if (varnamebuf == NULL) { varnamebuflen = 0; return NULL; @@ -4924,9 +4915,7 @@ static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate) * Copy the string into allocated memory, handling backslashed * characters. */ - name = alloc((unsigned)(p - *arg + extra)); - if (name == NULL) - return FAIL; + name = xmalloc(p - *arg + extra); rettv->v_type = VAR_STRING; rettv->vval.v_string = name; @@ -5041,9 +5030,7 @@ static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate) /* * Copy the string into allocated memory, handling '' to ' reduction. */ - str = alloc((unsigned)((p - *arg) - reduce)); - if (str == NULL) - return FAIL; + str = xmalloc((p - *arg) - reduce); rettv->v_type = VAR_STRING; rettv->vval.v_string = str; @@ -5192,7 +5179,7 @@ list_free ( */ listitem_T *listitem_alloc(void) { - return (listitem_T *)alloc(sizeof(listitem_T)); + return xmalloc(sizeof(listitem_T)); } /* @@ -6157,17 +6144,12 @@ dict_free ( * Allocate a Dictionary item. * The "key" is copied to the new item. * Note that the value of the item "di_tv" still needs to be initialized! - * Returns NULL when out of memory. */ dictitem_T *dictitem_alloc(char_u *key) { - dictitem_T *di; - - di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key))); - if (di != NULL) { - STRCPY(di->di_key, key); - di->di_flags = 0; - } + dictitem_T *di = xmalloc(sizeof(dictitem_T) + STRLEN(key)); + STRCPY(di->di_key, key); + di->di_flags = 0; return di; } @@ -6176,15 +6158,12 @@ dictitem_T *dictitem_alloc(char_u *key) */ static dictitem_T *dictitem_copy(dictitem_T *org) { - dictitem_T *di; + dictitem_T *di = xmalloc(sizeof(dictitem_T) + STRLEN(org->di_key)); + + STRCPY(di->di_key, org->di_key); + di->di_flags = 0; + copy_tv(&org->di_tv, &di->di_tv); - di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) - + STRLEN(org->di_key))); - if (di != NULL) { - STRCPY(di->di_key, org->di_key); - di->di_flags = 0; - copy_tv(&org->di_tv, &di->di_tv); - } return di; } @@ -6352,8 +6331,6 @@ dictitem_T *dict_find(dict_T *d, char_u *key, int len) akey = key; else if (len >= AKEYLEN) { tofree = akey = vim_strnsave(key, len); - if (akey == NULL) - return NULL; } else { /* Avoid a malloc/free by using buf[]. */ vim_strncpy(buf, key, len); @@ -6673,34 +6650,33 @@ static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copy */ static char_u *string_quote(char_u *str, int function) { - unsigned len; char_u *p, *r, *s; - len = (function ? 13 : 3); + size_t len = (function ? 13 : 3); if (str != NULL) { - len += (unsigned)STRLEN(str); + len += STRLEN(str); for (p = str; *p != NUL; mb_ptr_adv(p)) if (*p == '\'') ++len; } - s = r = alloc(len); - if (r != NULL) { - if (function) { - STRCPY(r, "function('"); - r += 10; - } else - *r++ = '\''; - if (str != NULL) - for (p = str; *p != NUL; ) { - if (*p == '\'') - *r++ = '\''; - MB_COPY_CHAR(p, r); - } + s = r = xmalloc(len); + + if (function) { + STRCPY(r, "function('"); + r += 10; + } else *r++ = '\''; - if (function) - *r++ = ')'; - *r++ = NUL; - } + if (str != NULL) + for (p = str; *p != NUL; ) { + if (*p == '\'') + *r++ = '\''; + MB_COPY_CHAR(p, r); + } + *r++ = '\''; + if (function) + *r++ = ')'; + *r++ = NUL; + return s; } @@ -7262,8 +7238,6 @@ call_func ( /* Make a copy of the name, if it comes from a funcref variable it could * be changed or deleted in the called function. */ name = vim_strnsave(funcname, len); - if (name == NULL) - return ret; /* * In a script change <SID>name() and s:name() to K_SNR 123_name(). @@ -7288,13 +7262,9 @@ call_func ( STRCPY(fname_buf + i, name + llen); fname = fname_buf; } else { - fname = alloc((unsigned)(i + STRLEN(name + llen) + 1)); - if (fname == NULL) - error = ERROR_OTHER; - else { - memmove(fname, fname_buf, (size_t)i); - STRCPY(fname + i, name + llen); - } + fname = xmalloc(i + STRLEN(name + llen) + 1); + memmove(fname, fname_buf, (size_t)i); + STRCPY(fname + i, name + llen); } } else fname = name; @@ -9257,20 +9227,18 @@ static void f_foldtext(typval_T *argvars, typval_T *rettv) } } txt = _("+-%s%3ld lines: "); - r = alloc((unsigned)(STRLEN(txt) - + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */ - + 20 /* for %3ld */ - + STRLEN(s))); /* concatenated */ - if (r != NULL) { - sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str, - (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr - - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1)); - len = (int)STRLEN(r); - STRCAT(r, s); - /* remove 'foldmarker' and 'commentstring' */ - foldtext_cleanup(r + len); - rettv->vval.v_string = r; - } + r = xmalloc(STRLEN(txt) + + STRLEN(vimvars[VV_FOLDDASHES].vv_str) // for %s + + 20 // for %3ld + + STRLEN(s)); // concatenated + sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str, + (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr + - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1)); + len = (int)STRLEN(r); + STRCAT(r, s); + /* remove 'foldmarker' and 'commentstring' */ + foldtext_cleanup(r + len); + rettv->vval.v_string = r; } } @@ -9331,12 +9299,9 @@ static void f_function(typval_T *argvars, typval_T *rettv) * would also work, but some plugins depend on the name being * printable text. */ sprintf(sid_buf, "<SNR>%" PRId64 "_", (int64_t)current_SID); - rettv->vval.v_string = - alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1)); - if (rettv->vval.v_string != NULL) { - STRCPY(rettv->vval.v_string, sid_buf); - STRCAT(rettv->vval.v_string, s + off); - } + rettv->vval.v_string = xmalloc(STRLEN(sid_buf) + STRLEN(s + off) + 1); + STRCPY(rettv->vval.v_string, sid_buf); + STRCAT(rettv->vval.v_string, s + off); } else rettv->vval.v_string = vim_strsave(s); rettv->v_type = VAR_FUNC; @@ -9625,11 +9590,8 @@ static void f_getcmdpos(typval_T *argvars, typval_T *rettv) static void f_getcmdtype(typval_T *argvars, typval_T *rettv) { rettv->v_type = VAR_STRING; - rettv->vval.v_string = alloc(2); - if (rettv->vval.v_string != NULL) { - rettv->vval.v_string[0] = get_cmdline_type(); - rettv->vval.v_string[1] = NUL; - } + rettv->vval.v_string = xmallocz(1); + rettv->vval.v_string[0] = get_cmdline_type(); } /* @@ -9641,17 +9603,14 @@ static void f_getcwd(typval_T *argvars, typval_T *rettv) |