diff options
author | Felipe Oliveira Carvalho <felipekde@gmail.com> | 2014-04-19 02:12:47 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-04-24 10:31:31 -0300 |
commit | 42f1bd9b2228aaca4fb8a5597a3b5774f7ef6876 (patch) | |
tree | 8c056bd42ec55f2103d65af680a54bd55dc71d7f | |
parent | 4b6b9117b3e8b9b624c354a703f01f0980c60946 (diff) | |
download | rneovim-42f1bd9b2228aaca4fb8a5597a3b5774f7ef6876.tar.gz rneovim-42f1bd9b2228aaca4fb8a5597a3b5774f7ef6876.tar.bz2 rneovim-42f1bd9b2228aaca4fb8a5597a3b5774f7ef6876.zip |
No OOM error condition in ga_concat_strings(), concat_fnames(), concat_str()
- xmallocz() is not static anymore. There are many use cases for this function
in the codebase and we should start using it.
- Simpler types in ga_concat_strings()
-rw-r--r-- | src/ex_cmds.c | 24 | ||||
-rw-r--r-- | src/garray.c | 20 | ||||
-rw-r--r-- | src/if_cscope.c | 2 | ||||
-rw-r--r-- | src/main.c | 6 | ||||
-rw-r--r-- | src/memline.c | 35 | ||||
-rw-r--r-- | src/misc1.c | 11 | ||||
-rw-r--r-- | src/option.c | 12 | ||||
-rw-r--r-- | src/path.c | 28 | ||||
-rw-r--r-- | src/quickfix.c | 9 | ||||
-rw-r--r-- | src/undo.c | 8 |
10 files changed, 56 insertions, 99 deletions
diff --git a/src/ex_cmds.c b/src/ex_cmds.c index e2ea0a9625..e6897d0022 100644 --- a/src/ex_cmds.c +++ b/src/ex_cmds.c @@ -4020,22 +4020,14 @@ void do_sub(exarg_T *eap) orig_line = vim_strsave(ml_get(lnum)); if (orig_line != NULL) { char_u *new_line = concat_str(new_start, - sub_firstline + copycol); - - if (new_line == NULL) { - vim_free(orig_line); - orig_line = NULL; - } else { - /* Position the cursor relative to the - * end of the line, the previous - * substitute may have inserted or - * deleted characters before the - * cursor. */ - len_change = (int)STRLEN(new_line) - - (int)STRLEN(orig_line); - curwin->w_cursor.col += len_change; - ml_replace(lnum, new_line, FALSE); - } + sub_firstline + copycol); + + // Position the cursor relative to the end of the line, the + // previous substitute may have inserted or deleted characters + // before the cursor. + len_change = (int)STRLEN(new_line) - (int)STRLEN(orig_line); + curwin->w_cursor.col += len_change; + ml_replace(lnum, new_line, FALSE); } } diff --git a/src/garray.c b/src/garray.c index 4084e572ad..514d486791 100644 --- a/src/garray.c +++ b/src/garray.c @@ -102,28 +102,26 @@ void ga_remove_duplicate_strings(garray_T *gap) /// /// @param gap /// -/// @returns NULL when out of memory. +/// @returns the concatenated strings char_u* ga_concat_strings(garray_T *gap) { - int i; - int len = 0; - char_u *s; + size_t len = 0; - for (i = 0; i < gap->ga_len; ++i) { - len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + 1; + for (int i = 0; i < gap->ga_len; ++i) { + len += strlen(((char **)(gap->ga_data))[i]) + 1; } - s = alloc(len + 1); + char *s = xmallocz(len); *s = NUL; - for (i = 0; i < gap->ga_len; ++i) { + for (int i = 0; i < gap->ga_len; ++i) { if (*s != NUL) { - STRCAT(s, ","); + strcat(s, ","); } - STRCAT(s, ((char_u **)(gap->ga_data))[i]); + strcat(s, ((char **)(gap->ga_data))[i]); } - return s; + return (char_u *)s; } /// Concatenate a string to a growarray which contains characters. diff --git a/src/if_cscope.c b/src/if_cscope.c index ae1495ce06..f76305dde6 100644 --- a/src/if_cscope.c +++ b/src/if_cscope.c @@ -2161,7 +2161,7 @@ static char *cs_resolve_file(int i, char *name) && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) && (name[0] != '/') ) { - fullname = (char *)alloc(len); + fullname = xmalloc(len); (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name); } else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL) { /* Check for csdir to be non empty to avoid empty path concatenated to diff --git a/src/main.c b/src/main.c index b2295ea0be..af63ed3c4f 100644 --- a/src/main.c +++ b/src/main.c @@ -1433,10 +1433,8 @@ scripterror: char_u *r; r = concat_fnames(p, path_tail(alist_name(&GARGLIST[0])), TRUE); - if (r != NULL) { - vim_free(p); - p = r; - } + vim_free(p); + p = r; } #ifdef USE_FNAME_CASE diff --git a/src/memline.c b/src/memline.c index 7dc386af66..dea6fceaf2 100644 --- a/src/memline.c +++ b/src/memline.c @@ -1586,16 +1586,10 @@ recover_names ( tail = make_percent_swname(dir_name, fname_res); } else #endif - { - tail = path_tail(fname_res); - tail = concat_fnames(dir_name, tail, TRUE); - } - if (tail == NULL) - num_names = 0; - else { - num_names = recov_file_names(names, tail, FALSE); - vim_free(tail); - } + tail = path_tail(fname_res); + tail = concat_fnames(dir_name, tail, TRUE); + num_names = recov_file_names(names, tail, FALSE); + vim_free(tail); } } @@ -1709,8 +1703,7 @@ static char_u *make_percent_swname(char_u *dir, char_u *name) f = fix_fname(name != NULL ? name : (char_u *) ""); d = NULL; if (f != NULL) { - s = alloc((unsigned)(STRLEN(f) + 1)); - STRCPY(s, f); + s = (char_u *)xstrdup((char *)f); for (d = s; *d != NUL; mb_ptr_adv(d)) if (vim_ispathsep(*d)) *d = '%'; @@ -1855,12 +1848,8 @@ static int recov_file_names(char_u **names, char_u *path, int prepend_dot) ++num_names; } - /* - * Form the normal swap file name pattern by appending ".sw?". - */ + // Form the normal swap file name pattern by appending ".sw?". names[num_names] = concat_fnames(path, (char_u *)".sw?", FALSE); - if (names[num_names] == NULL) - goto end; if (num_names >= 1) { /* check if we have the same name twice */ p = names[num_names - 1]; i = (int)STRLEN(names[num_names - 1]) - (int)STRLEN(names[num_names]); @@ -3497,16 +3486,12 @@ get_file_in_dir ( *tail = NUL; t = concat_fnames(fname, dname + 2, TRUE); *tail = save_char; - if (t == NULL) /* out of memory */ - retval = NULL; - else { - retval = concat_fnames(t, tail, TRUE); - vim_free(t); - } + retval = concat_fnames(t, tail, TRUE); + vim_free(t); } - } else + } else { retval = concat_fnames(dname, tail, TRUE); - + } return retval; } diff --git a/src/misc1.c b/src/misc1.c index 671ab93327..45e09c7bf0 100644 --- a/src/misc1.c +++ b/src/misc1.c @@ -3125,11 +3125,11 @@ static char_u *vim_version_dir(char_u *vimdir) if (vimdir == NULL || *vimdir == NUL) return NULL; p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); - if (p != NULL && os_isdir(p)) + if (os_isdir(p)) return p; vim_free(p); p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); - if (p != NULL && os_isdir(p)) + if (os_isdir(p)) return p; vim_free(p); return NULL; @@ -3163,11 +3163,8 @@ void vim_setenv(char_u *name, char_u *val) */ if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) { char_u *buf = concat_str(val, (char_u *)"/lang"); - - if (buf != NULL) { - bindtextdomain(VIMPACKAGE, (char *)buf); - vim_free(buf); - } + bindtextdomain(VIMPACKAGE, (char *)buf); + vim_free(buf); } } diff --git a/src/option.c b/src/option.c index 3f8d037833..a12b3d879a 100644 --- a/src/option.c +++ b/src/option.c @@ -5048,13 +5048,11 @@ static char_u *compile_cap_prog(synblock_T *synblock) else { /* Prepend a ^ so that we only match at one column */ re = concat_str((char_u *)"^", synblock->b_p_spc); - if (re != NULL) { - synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC); - vim_free(re); - if (synblock->b_cap_prog == NULL) { - synblock->b_cap_prog = rp; /* restore the previous program */ - return e_invarg; - } + synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC); + vim_free(re); + if (synblock->b_cap_prog == NULL) { + synblock->b_cap_prog = rp; /* restore the previous program */ + return e_invarg; } } diff --git a/src/path.c b/src/path.c index d2b52ee93e..4914c681da 100644 --- a/src/path.c +++ b/src/path.c @@ -272,32 +272,26 @@ int vim_fnamencmp(char_u *x, char_u *y, size_t len) */ char_u *concat_fnames(char_u *fname1, char_u *fname2, int sep) { - char_u *dest; - - dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); - if (dest != NULL) { - STRCPY(dest, fname1); - if (sep) - add_pathsep(dest); - STRCAT(dest, fname2); + char_u *dest = xmalloc(STRLEN(fname1) + STRLEN(fname2) + 3); + + STRCPY(dest, fname1); + if (sep) { + add_pathsep(dest); } + STRCAT(dest, fname2); + return dest; } /* * 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) { - char_u *dest; size_t l = STRLEN(str1); - - dest = alloc((unsigned)(l + STRLEN(str2) + 1L)); - if (dest != NULL) { - STRCPY(dest, str1); - STRCPY(dest + l, str2); - } + char_u *dest = xmalloc(l + STRLEN(str2) + 1); + STRCPY(dest, str1); + STRCPY(dest + l, str2); return dest; } @@ -916,8 +910,6 @@ expand_in_path ( paths = ga_concat_strings(&path_ga); ga_clear_strings(&path_ga); - if (paths == NULL) - return 0; files = globpath(paths, pattern, (flags & EW_ICASE) ? WILD_ICASE : 0); vim_free(paths); diff --git a/src/quickfix.c b/src/quickfix.c index 46511b2c19..6bc959db5c 100644 --- a/src/quickfix.c +++ b/src/quickfix.c @@ -1119,8 +1119,8 @@ static int qf_get_fnum(char_u *directory, char_u *fname) slash_adjust(directory); slash_adjust(fname); #endif - if (directory != NULL && !vim_isAbsName(fname) - && (ptr = concat_fnames(directory, fname, TRUE)) != NULL) { + if (directory != NULL && !vim_isAbsName(fname)) { + ptr = concat_fnames(directory, fname, TRUE); /* * Here we check if the file really exists. * This should normally be true, but if make works without @@ -1280,10 +1280,7 @@ static char_u *qf_guess_filepath(char_u *filename) vim_free(fullname); fullname = concat_fnames(ds_ptr->dirname, filename, TRUE); - /* If concat_fnames failed, just go on. The worst thing that can happen - * is that we delete the entire stack. - */ - if (fullname != NULL && os_file_exists(fullname)) + if (os_file_exists(fullname)) break; ds_ptr = ds_ptr->next; diff --git a/src/undo.c b/src/undo.c index e95a34cbd1..91add9e3dd 100644 --- a/src/undo.c +++ b/src/undo.c @@ -721,11 +721,11 @@ char_u *u_get_undo_file_name(char_u *buf_ffname, int reading) } } - /* When reading check if the file exists. */ - if (undo_file_name != NULL && (!reading - || mch_stat((char *)undo_file_name, - &st) >= 0)) + // When reading check if the file exists. + if (undo_file_name != NULL && + (!reading || mch_stat((char *)undo_file_name, &st) >= 0)) { break; + } vim_free(undo_file_name); undo_file_name = NULL; } |