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()
This commit is contained in:
Felipe Oliveira Carvalho 2014-04-19 02:12:47 -03:00 committed by Thiago de Arruda
parent 4b6b9117b3
commit 42f1bd9b22
10 changed files with 54 additions and 97 deletions

View File

@ -4020,22 +4020,14 @@ void do_sub(exarg_T *eap)
orig_line = vim_strsave(ml_get(lnum)); orig_line = vim_strsave(ml_get(lnum));
if (orig_line != NULL) { if (orig_line != NULL) {
char_u *new_line = concat_str(new_start, char_u *new_line = concat_str(new_start,
sub_firstline + copycol); sub_firstline + copycol);
if (new_line == NULL) { // Position the cursor relative to the end of the line, the
vim_free(orig_line); // previous substitute may have inserted or deleted characters
orig_line = NULL; // before the cursor.
} else { len_change = (int)STRLEN(new_line) - (int)STRLEN(orig_line);
/* Position the cursor relative to the curwin->w_cursor.col += len_change;
* end of the line, the previous ml_replace(lnum, new_line, FALSE);
* 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);
}
} }
} }

View File

@ -102,28 +102,26 @@ void ga_remove_duplicate_strings(garray_T *gap)
/// ///
/// @param gap /// @param gap
/// ///
/// @returns NULL when out of memory. /// @returns the concatenated strings
char_u* ga_concat_strings(garray_T *gap) char_u* ga_concat_strings(garray_T *gap)
{ {
int i; size_t len = 0;
int len = 0;
char_u *s;
for (i = 0; i < gap->ga_len; ++i) { for (int i = 0; i < gap->ga_len; ++i) {
len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + 1; len += strlen(((char **)(gap->ga_data))[i]) + 1;
} }
s = alloc(len + 1); char *s = xmallocz(len);
*s = NUL; *s = NUL;
for (i = 0; i < gap->ga_len; ++i) { for (int i = 0; i < gap->ga_len; ++i) {
if (*s != NUL) { 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. /// Concatenate a string to a growarray which contains characters.

View File

@ -2161,7 +2161,7 @@ static char *cs_resolve_file(int i, char *name)
&& (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
&& (name[0] != '/') && (name[0] != '/')
) { ) {
fullname = (char *)alloc(len); fullname = xmalloc(len);
(void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name); (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
} else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL) { } else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL) {
/* Check for csdir to be non empty to avoid empty path concatenated to /* Check for csdir to be non empty to avoid empty path concatenated to

View File

@ -1433,10 +1433,8 @@ scripterror:
char_u *r; char_u *r;
r = concat_fnames(p, path_tail(alist_name(&GARGLIST[0])), TRUE); r = concat_fnames(p, path_tail(alist_name(&GARGLIST[0])), TRUE);
if (r != NULL) { vim_free(p);
vim_free(p); p = r;
p = r;
}
} }
#ifdef USE_FNAME_CASE #ifdef USE_FNAME_CASE

View File

@ -1586,16 +1586,10 @@ recover_names (
tail = make_percent_swname(dir_name, fname_res); tail = make_percent_swname(dir_name, fname_res);
} else } else
#endif #endif
{ tail = path_tail(fname_res);
tail = path_tail(fname_res); tail = concat_fnames(dir_name, tail, TRUE);
tail = concat_fnames(dir_name, tail, TRUE); num_names = recov_file_names(names, tail, FALSE);
} vim_free(tail);
if (tail == NULL)
num_names = 0;
else {
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 *) ""); f = fix_fname(name != NULL ? name : (char_u *) "");
d = NULL; d = NULL;
if (f != NULL) { if (f != NULL) {
s = alloc((unsigned)(STRLEN(f) + 1)); s = (char_u *)xstrdup((char *)f);
STRCPY(s, f);
for (d = s; *d != NUL; mb_ptr_adv(d)) for (d = s; *d != NUL; mb_ptr_adv(d))
if (vim_ispathsep(*d)) if (vim_ispathsep(*d))
*d = '%'; *d = '%';
@ -1855,12 +1848,8 @@ static int recov_file_names(char_u **names, char_u *path, int prepend_dot)
++num_names; ++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); 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 */ if (num_names >= 1) { /* check if we have the same name twice */
p = names[num_names - 1]; p = names[num_names - 1];
i = (int)STRLEN(names[num_names - 1]) - (int)STRLEN(names[num_names]); i = (int)STRLEN(names[num_names - 1]) - (int)STRLEN(names[num_names]);
@ -3497,16 +3486,12 @@ get_file_in_dir (
*tail = NUL; *tail = NUL;
t = concat_fnames(fname, dname + 2, TRUE); t = concat_fnames(fname, dname + 2, TRUE);
*tail = save_char; *tail = save_char;
if (t == NULL) /* out of memory */ retval = concat_fnames(t, tail, TRUE);
retval = NULL; vim_free(t);
else {
retval = concat_fnames(t, tail, TRUE);
vim_free(t);
}
} }
} else } else {
retval = concat_fnames(dname, tail, TRUE); retval = concat_fnames(dname, tail, TRUE);
}
return retval; return retval;
} }

View File

@ -3125,11 +3125,11 @@ static char_u *vim_version_dir(char_u *vimdir)
if (vimdir == NULL || *vimdir == NUL) if (vimdir == NULL || *vimdir == NUL)
return NULL; return NULL;
p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
if (p != NULL && os_isdir(p)) if (os_isdir(p))
return p; return p;
vim_free(p); vim_free(p);
p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
if (p != NULL && os_isdir(p)) if (os_isdir(p))
return p; return p;
vim_free(p); vim_free(p);
return NULL; return NULL;
@ -3163,11 +3163,8 @@ void vim_setenv(char_u *name, char_u *val)
*/ */
if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) { if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) {
char_u *buf = concat_str(val, (char_u *)"/lang"); char_u *buf = concat_str(val, (char_u *)"/lang");
bindtextdomain(VIMPACKAGE, (char *)buf);
if (buf != NULL) { vim_free(buf);
bindtextdomain(VIMPACKAGE, (char *)buf);
vim_free(buf);
}
} }
} }

View File

@ -5048,13 +5048,11 @@ static char_u *compile_cap_prog(synblock_T *synblock)
else { else {
/* Prepend a ^ so that we only match at one column */ /* Prepend a ^ so that we only match at one column */
re = concat_str((char_u *)"^", synblock->b_p_spc); re = concat_str((char_u *)"^", synblock->b_p_spc);
if (re != NULL) { synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC); vim_free(re);
vim_free(re); if (synblock->b_cap_prog == NULL) {
if (synblock->b_cap_prog == NULL) { synblock->b_cap_prog = rp; /* restore the previous program */
synblock->b_cap_prog = rp; /* restore the previous program */ return e_invarg;
return e_invarg;
}
} }
} }

View File

@ -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 *concat_fnames(char_u *fname1, char_u *fname2, int sep)
{ {
char_u *dest; char_u *dest = xmalloc(STRLEN(fname1) + STRLEN(fname2) + 3);
dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); STRCPY(dest, fname1);
if (dest != NULL) { if (sep) {
STRCPY(dest, fname1); add_pathsep(dest);
if (sep)
add_pathsep(dest);
STRCAT(dest, fname2);
} }
STRCAT(dest, fname2);
return dest; return dest;
} }
/* /*
* Concatenate two strings and return the result in allocated memory. * 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 *concat_str(char_u *str1, char_u *str2)
{ {
char_u *dest;
size_t l = STRLEN(str1); size_t l = STRLEN(str1);
char_u *dest = xmalloc(l + STRLEN(str2) + 1);
dest = alloc((unsigned)(l + STRLEN(str2) + 1L)); STRCPY(dest, str1);
if (dest != NULL) { STRCPY(dest + l, str2);
STRCPY(dest, str1);
STRCPY(dest + l, str2);
}
return dest; return dest;
} }
@ -916,8 +910,6 @@ expand_in_path (
paths = ga_concat_strings(&path_ga); paths = ga_concat_strings(&path_ga);
ga_clear_strings(&path_ga); ga_clear_strings(&path_ga);
if (paths == NULL)
return 0;
files = globpath(paths, pattern, (flags & EW_ICASE) ? WILD_ICASE : 0); files = globpath(paths, pattern, (flags & EW_ICASE) ? WILD_ICASE : 0);
vim_free(paths); vim_free(paths);

View File

@ -1119,8 +1119,8 @@ static int qf_get_fnum(char_u *directory, char_u *fname)
slash_adjust(directory); slash_adjust(directory);
slash_adjust(fname); slash_adjust(fname);
#endif #endif
if (directory != NULL && !vim_isAbsName(fname) if (directory != NULL && !vim_isAbsName(fname)) {
&& (ptr = concat_fnames(directory, fname, TRUE)) != NULL) { ptr = concat_fnames(directory, fname, TRUE);
/* /*
* Here we check if the file really exists. * Here we check if the file really exists.
* This should normally be true, but if make works without * 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); vim_free(fullname);
fullname = concat_fnames(ds_ptr->dirname, filename, TRUE); fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
/* If concat_fnames failed, just go on. The worst thing that can happen if (os_file_exists(fullname))
* is that we delete the entire stack.
*/
if (fullname != NULL && os_file_exists(fullname))
break; break;
ds_ptr = ds_ptr->next; ds_ptr = ds_ptr->next;

View File

@ -721,11 +721,11 @@ char_u *u_get_undo_file_name(char_u *buf_ffname, int reading)
} }
} }
/* When reading check if the file exists. */ // When reading check if the file exists.
if (undo_file_name != NULL && (!reading if (undo_file_name != NULL &&
|| mch_stat((char *)undo_file_name, (!reading || mch_stat((char *)undo_file_name, &st) >= 0)) {
&st) >= 0))
break; break;
}
vim_free(undo_file_name); vim_free(undo_file_name);
undo_file_name = NULL; undo_file_name = NULL;
} }