Remove OOM checks: suggested changes in review

- Replace a vim_strsave/free pair with xrealloc
 - Use xmallocz() in some places
 - Use xrealloc() and forget about the NULL pointer case
 - Remove invalid comment
 - Remove unnecessary checks
 - Replace a complicated xmalloc/STRCPY/free code chunk code with xrealloc()
 - Replace a vim_strsave/free code chunk with xrealloc()
This commit is contained in:
Felipe Oliveira Carvalho 2014-05-12 16:19:50 -03:00
parent 7a830d945f
commit e303a11ebf
5 changed files with 14 additions and 33 deletions

View File

@ -9592,9 +9592,8 @@ static void f_getcmdpos(typval_T *argvars, typval_T *rettv)
static void f_getcmdtype(typval_T *argvars, typval_T *rettv) static void f_getcmdtype(typval_T *argvars, typval_T *rettv)
{ {
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;
rettv->vval.v_string = xmalloc(2); rettv->vval.v_string = xmallocz(1);
rettv->vval.v_string[0] = get_cmdline_type(); rettv->vval.v_string[0] = get_cmdline_type();
rettv->vval.v_string[1] = NUL;
} }
/* /*
@ -12124,8 +12123,6 @@ static void f_readfile(typval_T *argvars, typval_T *rettv)
if (start < p) { if (start < p) {
/* There's part of a line in buf, store it in "prev". */ /* There's part of a line in buf, store it in "prev". */
if (p - start + prevlen >= prevsize) { if (p - start + prevlen >= prevsize) {
/* need bigger "prev" buffer */
char_u *newprev;
/* A common use case is ordinary text files and "prev" gets a /* A common use case is ordinary text files and "prev" gets a
* fragment of a line, so the first allocation is made * fragment of a line, so the first allocation is made
@ -12138,8 +12135,7 @@ static void f_readfile(typval_T *argvars, typval_T *rettv)
long growmin = (long)((p - start) * 2 + prevlen); long growmin = (long)((p - start) * 2 + prevlen);
prevsize = grow50pc > growmin ? grow50pc : growmin; prevsize = grow50pc > growmin ? grow50pc : growmin;
} }
newprev = (prev == NULL) ? xmalloc(prevsize) : xrealloc(prev, prevsize); prev = xrealloc(prev, prevsize);
prev = newprev;
} }
/* Add the line part to end of "prev". */ /* Add the line part to end of "prev". */
memmove(prev + prevlen, start, p - start); memmove(prev + prevlen, start, p - start);
@ -12398,10 +12394,9 @@ static void f_repeat(typval_T *argvars, typval_T *rettv)
if (len <= 0) if (len <= 0)
return; return;
char_u *r = xmalloc(len + 1); char_u *r = xmallocz(len);
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
memmove(r + i * slen, p, (size_t)slen); memmove(r + i * slen, p, (size_t)slen);
r[len] = NUL;
rettv->vval.v_string = r; rettv->vval.v_string = r;
} }
@ -15708,7 +15703,7 @@ static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *
temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE); temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
if (temp_result != NULL && nextcmd == NULL) { if (temp_result != NULL && nextcmd == NULL) {
retval = xmalloc(STRLEN(temp_result) + (expr_start - in_start) retval = xmalloc(STRLEN(temp_result) + (expr_start - in_start)
+ (in_end - expr_end) + 1); + (in_end - expr_end) + 1);
STRCPY(retval, in_start); STRCPY(retval, in_start);
STRCAT(retval, temp_result); STRCAT(retval, temp_result);
STRCAT(retval, expr_end + 1); STRCAT(retval, expr_end + 1);

View File

@ -1986,7 +1986,7 @@ static void alloc_cmdbuff(int len)
else else
len += 20; len += 20;
ccline.cmdbuff = xmalloc(len); /* caller should check for out-of-memory */ ccline.cmdbuff = xmalloc(len);
ccline.cmdbufflen = len; ccline.cmdbufflen = len;
} }
@ -2179,10 +2179,7 @@ void put_on_cmdline(char_u *str, int len, int redraw)
if (len < 0) if (len < 0)
len = (int)STRLEN(str); len = (int)STRLEN(str);
/* Check if ccline.cmdbuff needs to be longer */ realloc_cmdbuff(ccline.cmdlen + len + 1);
if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) {
realloc_cmdbuff(ccline.cmdlen + len + 1);
}
if (!ccline.overstrike) { if (!ccline.overstrike) {
memmove(ccline.cmdbuff + ccline.cmdpos + len, memmove(ccline.cmdbuff + ccline.cmdpos + len,

View File

@ -719,13 +719,10 @@ restofline:
if (qfprev == NULL) if (qfprev == NULL)
goto error2; goto error2;
if (*errmsg && !multiignore) { if (*errmsg && !multiignore) {
len = (int)STRLEN(qfprev->qf_text); size_t len = STRLEN(qfprev->qf_text);
ptr = xmalloc(len + STRLEN(errmsg) + 2); qfprev->qf_text = xrealloc(qfprev->qf_text, len + STRLEN(errmsg) + 2);
STRCPY(ptr, qfprev->qf_text); qfprev->qf_text[len] = '\n';
free(qfprev->qf_text); STRCPY(qfprev->qf_text + len + 1, errmsg);
qfprev->qf_text = ptr;
*(ptr += len) = '\n';
STRCPY(++ptr, errmsg);
} }
if (qfprev->qf_nr == -1) if (qfprev->qf_nr == -1)
qfprev->qf_nr = enr; qfprev->qf_nr = enr;

View File

@ -4691,10 +4691,7 @@ win_redr_status_matches (
if (matches == NULL) /* interrupted completion? */ if (matches == NULL) /* interrupted completion? */
return; return;
if (has_mbyte) buf = xmalloc(has_mbyte ? Columns * MB_MAXBYTES + 1 : Columns + 1);
buf = xmalloc(Columns * MB_MAXBYTES + 1);
else
buf = xmalloc(Columns + 1);
if (match == -1) { /* don't show match but original text */ if (match == -1) { /* don't show match but original text */
match = 0; match = 0;

View File

@ -4304,14 +4304,9 @@ replace_termcodes (
} }
result[dlen] = NUL; result[dlen] = NUL;
/* *bufp = xrealloc(result, dlen + 1);
* Copy the new string to allocated memory.
* If this fails, just return from. return *bufp;
*/
*bufp = vim_strsave(result);
from = *bufp;
free(result);
return from;
} }
/* /*