From a80d7e86c1f088c5b68d8e8929cc72a0d9680f76 Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Thu, 8 May 2014 21:34:46 -0300 Subject: Remove NULL/non-NULL tests after calls to vim_str(n)save() --- src/nvim/quickfix.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'src/nvim/quickfix.c') diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 299a0a38db..0e6ed67351 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -929,19 +929,14 @@ qf_add_entry ( qfp->qf_fnum = bufnum; else qfp->qf_fnum = qf_get_fnum(dir, fname); - if ((qfp->qf_text = vim_strsave(mesg)) == NULL) { - free(qfp); - return FAIL; - } + qfp->qf_text = vim_strsave(mesg); qfp->qf_lnum = lnum; qfp->qf_col = col; qfp->qf_viscol = vis_col; - if (pattern == NULL || *pattern == NUL) + if (pattern == NULL || *pattern == NUL) { qfp->qf_pattern = NULL; - else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL) { - free(qfp->qf_text); - free(qfp); - return FAIL; + } else { + qfp->qf_pattern = vim_strsave(pattern); } qfp->qf_nr = nr; if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */ -- cgit From 21784aeb005e78f04f4c1d398bc486be0a65248e Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Fri, 9 May 2014 03:30:26 -0300 Subject: Replace alloc() with xmalloc() and remove immediate OOM checks --- src/nvim/quickfix.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'src/nvim/quickfix.c') diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 0e6ed67351..a026cb46f5 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -283,9 +283,9 @@ qf_init_ext ( {'s', ".\\+"} }; - namebuf = alloc(CMDBUFFSIZE + 1); - errmsg = alloc(CMDBUFFSIZE + 1); - pattern = alloc(CMDBUFFSIZE + 1); + namebuf = xmalloc(CMDBUFFSIZE + 1); + errmsg = xmalloc(CMDBUFFSIZE + 1); + pattern = xmalloc(CMDBUFFSIZE + 1); if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL) { EMSG2(_(e_openerrf), efile); @@ -321,7 +321,7 @@ qf_init_ext ( #else i += 2; /* "%f" can become two chars longer */ #endif - fmtstr = alloc(i); + fmtstr = xmalloc(i); while (efm[0] != NUL) { /* @@ -720,7 +720,7 @@ restofline: goto error2; if (*errmsg && !multiignore) { len = (int)STRLEN(qfprev->qf_text); - ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)); + ptr = xmalloc(len + STRLEN(errmsg) + 2); STRCPY(ptr, qfprev->qf_text); free(qfprev->qf_text); qfprev->qf_text = ptr; @@ -856,7 +856,7 @@ static void qf_new_list(qf_info_T *qi, char_u *qf_title) qi->qf_curlist = qi->qf_listcount++; memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T))); if (qf_title != NULL) { - char_u *p = alloc((int)STRLEN(qf_title) + 2); + char_u *p = xmalloc(STRLEN(qf_title) + 2); qi->qf_lists[qi->qf_curlist].qf_title = p; sprintf((char *)p, ":%s", (char *)qf_title); @@ -921,9 +921,7 @@ qf_add_entry ( int valid /* valid entry */ ) { - qfline_T *qfp; - - qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T)); + qfline_T *qfp = xmalloc(sizeof(qfline_T)); if (bufnum != 0) qfp->qf_fnum = bufnum; @@ -1140,11 +1138,10 @@ static int qf_get_fnum(char_u *directory, char_u *fname) */ static char_u *qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr) { - struct dir_stack_T *ds_new; struct dir_stack_T *ds_ptr; /* allocate new stack element and hook it in */ - ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T)); + struct dir_stack_T *ds_new = xmalloc(sizeof(struct dir_stack_T)); ds_new->next = *stackptr; *stackptr = ds_new; @@ -2516,7 +2513,7 @@ void ex_make(exarg_T *eap) len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1; if (*p_sp != NUL) len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3; - cmd = alloc(len); + cmd = xmalloc(len); sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg, (char *)p_shq); if (*p_sp != NUL) @@ -2591,7 +2588,7 @@ static char_u *get_mef_name(void) else off += 19; - name = alloc((unsigned)STRLEN(p_mef) + 30); + name = xmalloc(STRLEN(p_mef) + 30); STRCPY(name, p_mef); sprintf((char *)name + (p - p_mef), "%d%d", start, off); STRCAT(name, p + 2); @@ -2838,8 +2835,8 @@ void ex_vimgrep(exarg_T *eap) goto theend; } - dirname_start = alloc(MAXPATHL); - dirname_now = alloc(MAXPATHL); + dirname_start = xmalloc(MAXPATHL); + dirname_now = xmalloc(MAXPATHL); /* Remember the current directory, because a BufRead autocommand that does * ":lcd %:p:h" changes the meaning of short path names. */ @@ -3107,7 +3104,7 @@ char_u *skip_vimgrep_pat(char_u *p, char_u **s, int *flags) */ static void restore_start_dir(char_u *dirname_start) { - char_u *dirname_now = alloc(MAXPATHL); + char_u *dirname_now = xmalloc(MAXPATHL); os_dirname(dirname_now, MAXPATHL); if (STRCMP(dirname_start, dirname_now) != 0) { -- cgit From e303a11ebfc352860cce73184ece692ab4d0f01c Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Mon, 12 May 2014 16:19:50 -0300 Subject: 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() --- src/nvim/quickfix.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src/nvim/quickfix.c') diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index a026cb46f5..269a33edcc 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -719,13 +719,10 @@ restofline: if (qfprev == NULL) goto error2; if (*errmsg && !multiignore) { - len = (int)STRLEN(qfprev->qf_text); - ptr = xmalloc(len + STRLEN(errmsg) + 2); - STRCPY(ptr, qfprev->qf_text); - free(qfprev->qf_text); - qfprev->qf_text = ptr; - *(ptr += len) = '\n'; - STRCPY(++ptr, errmsg); + size_t len = STRLEN(qfprev->qf_text); + qfprev->qf_text = xrealloc(qfprev->qf_text, len + STRLEN(errmsg) + 2); + qfprev->qf_text[len] = '\n'; + STRCPY(qfprev->qf_text + len + 1, errmsg); } if (qfprev->qf_nr == -1) qfprev->qf_nr = enr; -- cgit