From b63d2626ed9e3e38a485b9990a8e65ba59d6906a Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Thu, 8 May 2014 21:32:40 -0300 Subject: Implement vim_str(n)save using xstrdup and strncpy/xmalloc --- src/nvim/strings.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'src/nvim/strings.c') diff --git a/src/nvim/strings.c b/src/nvim/strings.c index fbad71e3c6..731afa4452 100644 --- a/src/nvim/strings.c +++ b/src/nvim/strings.c @@ -46,14 +46,7 @@ */ char_u *vim_strsave(char_u *string) { - char_u *p; - unsigned len; - - len = (unsigned)STRLEN(string) + 1; - p = alloc(len); - if (p != NULL) - memmove(p, string, (size_t)len); - return p; + return (char_u *)xstrdup((char *)string); } /* @@ -64,12 +57,7 @@ char_u *vim_strsave(char_u *string) */ char_u *vim_strnsave(char_u *string, int len) { - char_u *p; - - p = alloc((unsigned)(len + 1)); - STRNCPY(p, string, len); - p[len] = NUL; - return p; + return (char_u *)strncpy(xmallocz(len), (char *)string, len); } /* -- cgit 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/strings.c | 70 +++++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 37 deletions(-) (limited to 'src/nvim/strings.c') diff --git a/src/nvim/strings.c b/src/nvim/strings.c index 731afa4452..1cadc5df09 100644 --- a/src/nvim/strings.c +++ b/src/nvim/strings.c @@ -242,49 +242,45 @@ void vim_strup(char_u *p) /* * Make string "s" all upper-case and return it in allocated memory. * Handles multi-byte characters as well as possible. - * Returns NULL when out of memory. */ char_u *strup_save(char_u *orig) { - char_u *p; - char_u *res; + char_u *res = vim_strsave(orig); - res = p = vim_strsave(orig); - - if (res != NULL) - while (*p != NUL) { - int l; - - if (enc_utf8) { - int c, uc; - int newl; - char_u *s; - - c = utf_ptr2char(p); - uc = utf_toupper(c); - - /* Reallocate string when byte count changes. This is rare, - * thus it's OK to do another malloc()/free(). */ - l = utf_ptr2len(p); - newl = utf_char2len(uc); - if (newl != l) { - s = alloc((unsigned)STRLEN(res) + 1 + newl - l); - memmove(s, res, p - res); - STRCPY(s + (p - res) + newl, p + l); - p = s + (p - res); - free(res); - res = s; - } - - utf_char2bytes(uc, p); - p += newl; - } else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) - p += l; /* skip multi-byte character */ - else { - *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */ - p++; + char_u *p = res; + while (*p != NUL) { + int l; + + if (enc_utf8) { + int c, uc; + int newl; + char_u *s; + + c = utf_ptr2char(p); + uc = utf_toupper(c); + + /* Reallocate string when byte count changes. This is rare, + * thus it's OK to do another malloc()/free(). */ + l = utf_ptr2len(p); + newl = utf_char2len(uc); + if (newl != l) { + s = alloc((unsigned)STRLEN(res) + 1 + newl - l); + memmove(s, res, p - res); + STRCPY(s + (p - res) + newl, p + l); + p = s + (p - res); + free(res); + res = s; } + + utf_char2bytes(uc, p); + p += newl; + } else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) + p += l; /* skip multi-byte character */ + else { + *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */ + p++; } + } return res; } -- 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/strings.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/strings.c') diff --git a/src/nvim/strings.c b/src/nvim/strings.c index 1cadc5df09..aaf6a67dea 100644 --- a/src/nvim/strings.c +++ b/src/nvim/strings.c @@ -97,7 +97,7 @@ char_u *vim_strsave_escaped_ext(char_u *string, char_u *esc_chars, int cc, int b ++length; /* count a backslash */ ++length; /* count an ordinary char */ } - escaped_string = alloc(length); + escaped_string = xmalloc(length); p2 = escaped_string; for (p = string; *p; p++) { if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) { @@ -158,7 +158,7 @@ char_u *vim_strsave_shellescape(char_u *string, bool do_special, bool do_newline } /* Allocate memory for the result and fill it. */ - escaped_string = alloc(length); + escaped_string = xmalloc(length); d = escaped_string; /* add opening quote */ @@ -264,7 +264,7 @@ char_u *strup_save(char_u *orig) l = utf_ptr2len(p); newl = utf_char2len(uc); if (newl != l) { - s = alloc((unsigned)STRLEN(res) + 1 + newl - l); + s = xmalloc(STRLEN(res) + 1 + newl - l); memmove(s, res, p - res); STRCPY(s + (p - res) + newl, p + l); p = s + (p - res); -- cgit