No OOM in vim_strsave_escaped[_ext]()

This commit is contained in:
Felipe Oliveira Carvalho
2014-05-30 22:46:26 -03:00
parent f7e64c3c5f
commit 8234f2839f
10 changed files with 31 additions and 61 deletions

View File

@@ -2485,8 +2485,6 @@ ins_compl_dictionaries (
if (ctrl_x_mode == CTRL_X_WHOLE_LINE) { if (ctrl_x_mode == CTRL_X_WHOLE_LINE) {
char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
if (pat_esc == NULL)
goto theend;
size_t len = STRLEN(pat_esc) + 10; size_t len = STRLEN(pat_esc) + 10;
ptr = xmalloc(len); ptr = xmalloc(len);
vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); 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()); saved_text = vim_strsave(get_cursor_pos_ptr());
curwin->w_cursor.col = orig_col; curwin->w_cursor.col = orig_col;
if (saved_text == NULL)
break; /* Can't do it, out of memory */
saved_text[startcol] = NUL; saved_text[startcol] = NUL;
/* Backspace over characters that will move to the next line */ /* Backspace over characters that will move to the next line */

View File

@@ -18603,8 +18603,6 @@ int store_session_globals(FILE *fd)
* CR into \n and \r. */ * CR into \n and \r. */
p = vim_strsave_escaped(get_tv_string(&this_var->di_tv), p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
(char_u *)"\\\"\n\r"); (char_u *)"\\\"\n\r");
if (p == NULL) /* out of memory */
break;
for (t = p; *t != NUL; ++t) for (t = p; *t != NUL; ++t)
if (*t == '\n') if (*t == '\n')
*t = 'n'; *t = 'n';

View File

@@ -3545,10 +3545,8 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp)
for (l = repl; *l; ++l) for (l = repl; *l; ++l)
if (vim_strchr(ESCAPE_CHARS, *l) != NULL) { if (vim_strchr(ESCAPE_CHARS, *l) != NULL) {
l = vim_strsave_escaped(repl, ESCAPE_CHARS); l = vim_strsave_escaped(repl, ESCAPE_CHARS);
if (l != NULL) {
free(repl); free(repl);
repl = l; repl = l;
}
break; break;
} }
} }
@@ -3559,11 +3557,9 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp)
char_u *l; char_u *l;
l = vim_strsave_escaped(repl, (char_u *)"!"); l = vim_strsave_escaped(repl, (char_u *)"!");
if (l != NULL) {
free(repl); free(repl);
repl = l; repl = l;
} }
}
p = repl_cmdline(eap, p, srclen, repl, cmdlinep); p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
free(repl); free(repl);

View File

@@ -2947,18 +2947,14 @@ void ExpandEscape(expand_T *xp, char_u *str, int numfiles, char_u **files, int o
/* for ":set path=" we need to escape spaces twice */ /* for ":set path=" we need to escape spaces twice */
if (xp->xp_backslash == XP_BS_THREE) { if (xp->xp_backslash == XP_BS_THREE) {
p = vim_strsave_escaped(files[i], (char_u *)" "); p = vim_strsave_escaped(files[i], (char_u *)" ");
if (p != NULL) {
free(files[i]); free(files[i]);
files[i] = p; files[i] = p;
#if defined(BACKSLASH_IN_FILENAME) #if defined(BACKSLASH_IN_FILENAME)
p = vim_strsave_escaped(files[i], (char_u *)" "); p = vim_strsave_escaped(files[i], (char_u *)" ");
if (p != NULL) {
free(files[i]); free(files[i]);
files[i] = p; files[i] = p;
}
#endif #endif
} }
}
#ifdef BACKSLASH_IN_FILENAME #ifdef BACKSLASH_IN_FILENAME
p = vim_strsave_fnameescape(files[i], FALSE); p = vim_strsave_fnameescape(files[i], FALSE);
#else #else
@@ -2987,14 +2983,12 @@ void ExpandEscape(expand_T *xp, char_u *str, int numfiles, char_u **files, int o
*/ */
for (i = 0; i < numfiles; ++i) { for (i = 0; i < numfiles; ++i) {
p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
if (p != NULL) {
free(files[i]); free(files[i]);
files[i] = p; files[i] = p;
} }
} }
} }
} }
}
/* /*
* Escape special characters in "fname" for when used as a file name argument * Escape special characters in "fname" for when used as a file name argument
@@ -3016,7 +3010,7 @@ char_u *vim_strsave_fnameescape(char_u *fname, int shell)
p = vim_strsave_escaped(fname, buf); p = vim_strsave_escaped(fname, buf);
#else #else
p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
if (shell && csh_like_shell() && p != NULL) { if (shell && csh_like_shell()) {
char_u *s; char_u *s;
/* For csh and similar shells need to put two backslashes before '!'. /* For csh and similar shells need to put two backslashes before '!'.

View File

@@ -2847,13 +2847,11 @@ expand_env_esc (
if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) { if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) {
char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
if (p != NULL) {
if (mustfree) if (mustfree)
free(var); free(var);
var = p; var = p;
mustfree = TRUE; mustfree = TRUE;
} }
}
if (var != NULL && *var != NUL if (var != NULL && *var != NUL
&& (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) { && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) {

View File

@@ -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) { if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0) {
ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE); ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
if (ecmd == NULL)
ecmd = cmd;
} }
ncmd = xmalloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1); ncmd = xmalloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1);
STRCPY(ncmd, p_sxq); STRCPY(ncmd, p_sxq);

View File

@@ -943,14 +943,12 @@ do_execreg (
(char_u *) (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", "\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); Ctrl_V, FALSE);
if (p != NULL) {
/* When in Visual mode "'<,'>" will be prepended to the command. /* When in Visual mode "'<,'>" will be prepended to the command.
* Remove it when it's already there. */ * Remove it when it's already there. */
if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0) if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
retval = put_in_typebuf(p + 5, TRUE, TRUE, silent); retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
else else
retval = put_in_typebuf(p, TRUE, TRUE, silent); retval = put_in_typebuf(p, TRUE, TRUE, silent);
}
free(p); free(p);
} else if (regname == '=') { } else if (regname == '=') {
p = get_expr_line(); p = get_expr_line();

View File

@@ -7424,12 +7424,6 @@ int ExpandOldSetting(int *num_file, char_u ***file)
* what happens in do_set(). */ * what happens in do_set(). */
buf = vim_strsave_escaped(var, escape_chars); buf = vim_strsave_escaped(var, escape_chars);
if (buf == NULL) {
free(*file);
*file = NULL;
return FAIL;
}
#ifdef BACKSLASH_IN_FILENAME #ifdef BACKSLASH_IN_FILENAME
/* For MS-Windows et al. we don't double backslashes at the start and /* For MS-Windows et al. we don't double backslashes at the start and
* before a file name character. */ * before a file name character. */

View File

@@ -6500,11 +6500,9 @@ static int vim_regsub_both(char_u *source, char_u *dest, int copy, int magic, in
if (had_backslash && backslash) { if (had_backslash && backslash) {
/* Backslashes will be consumed, need to double them. */ /* Backslashes will be consumed, need to double them. */
s = vim_strsave_escaped(eval_result, (char_u *)"\\"); s = vim_strsave_escaped(eval_result, (char_u *)"\\");
if (s != NULL) {
free(eval_result); free(eval_result);
eval_result = s; eval_result = s;
} }
}
dst += STRLEN(eval_result); dst += STRLEN(eval_result);
} }

View File

@@ -65,6 +65,7 @@ char_u *vim_strnsave(char_u *string, int len) FUNC_ATTR_NONNULL_RET
* by a backslash. * by a backslash.
*/ */
char_u *vim_strsave_escaped(char_u *string, char_u *esc_chars) 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); return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
} }
@@ -75,10 +76,8 @@ char_u *vim_strsave_escaped(char_u *string, char_u *esc_chars)
* Escape the characters with "cc". * Escape the characters with "cc".
*/ */
char_u *vim_strsave_escaped_ext(char_u *string, char_u *esc_chars, int cc, int bsl) 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; unsigned length;
int l; int l;
@@ -87,7 +86,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. * Then allocate the memory and insert them.
*/ */
length = 1; /* count the trailing NUL */ 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) { if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) {
length += l; /* count a multibyte char */ length += l; /* count a multibyte char */
p += l - 1; p += l - 1;
@@ -97,9 +96,10 @@ char_u *vim_strsave_escaped_ext(char_u *string, char_u *esc_chars, int cc, int b
++length; /* count a backslash */ ++length; /* count a backslash */
++length; /* count an ordinary char */ ++length; /* count an ordinary char */
} }
escaped_string = xmalloc(length);
p2 = escaped_string; char_u *escaped_string = xmalloc(length);
for (p = string; *p; p++) { char_u *p2 = escaped_string;
for (char_u *p = string; *p; p++) {
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) { if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) {
memmove(p2, p, (size_t)l); memmove(p2, p, (size_t)l);
p2 += l; p2 += l;