mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
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:
parent
4b6b9117b3
commit
42f1bd9b22
@ -4022,22 +4022,14 @@ void do_sub(exarg_T *eap)
|
||||
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);
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
search_match_lines = regmatch.endpos[0].lnum
|
||||
- regmatch.startpos[0].lnum;
|
||||
|
20
src/garray.c
20
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.
|
||||
|
@ -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
|
||||
|
@ -1433,11 +1433,9 @@ scripterror:
|
||||
char_u *r;
|
||||
|
||||
r = concat_fnames(p, path_tail(alist_name(&GARGLIST[0])), TRUE);
|
||||
if (r != NULL) {
|
||||
vim_free(p);
|
||||
p = r;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_FNAME_CASE
|
||||
/* Make the case of the file name match the actual file. */
|
||||
|
@ -1586,18 +1586,12 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* check for out-of-memory */
|
||||
for (i = 0; i < num_names; ++i) {
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
retval = concat_fnames(dname, tail, TRUE);
|
||||
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
@ -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,13 +3163,10 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
@ -5048,7 +5048,6 @@ 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) {
|
||||
@ -5056,7 +5055,6 @@ static char_u *compile_cap_prog(synblock_T *synblock)
|
||||
return e_invarg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vim_regfree(rp);
|
||||
return NULL;
|
||||
|
18
src/path.c
18
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;
|
||||
char_u *dest = xmalloc(STRLEN(fname1) + STRLEN(fname2) + 3);
|
||||
|
||||
dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
|
||||
if (dest != NULL) {
|
||||
STRCPY(dest, fname1);
|
||||
if (sep)
|
||||
if (sep) {
|
||||
add_pathsep(dest);
|
||||
STRCAT(dest, fname2);
|
||||
}
|
||||
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) {
|
||||
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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user