Use xmalloc() and xmemdupz() in file_pat_to_reg_pat()

Unfortunately there's still a case where NULL can be returned from
file_pat_to_reg_pat().

xmemdupz() and xmallocz() aren't static anymore. There are many use cases for
these function.
This commit is contained in:
Felipe Oliveira Carvalho 2014-04-18 01:01:04 -03:00 committed by Thiago de Arruda
parent 471da2de81
commit 1befc49414
3 changed files with 43 additions and 41 deletions

View File

@ -8048,7 +8048,7 @@ int match_file_list(char_u *list, char_u *sfname, char_u *ffname)
* If FEAT_OSFILETYPE defined then pass initial <type> through unchanged. Eg: * If FEAT_OSFILETYPE defined then pass initial <type> through unchanged. Eg:
* '<html>myfile' becomes '<html>^myfile$' -- leonard. * '<html>myfile' becomes '<html>^myfile$' -- leonard.
* *
* Returns NULL when out of memory. * Returns NULL on failure.
*/ */
char_u * char_u *
file_pat_to_reg_pat ( file_pat_to_reg_pat (
@ -8058,7 +8058,7 @@ file_pat_to_reg_pat (
int no_bslash /* Don't use a backward slash as pathsep */ int no_bslash /* Don't use a backward slash as pathsep */
) )
{ {
int size; size_t size;
char_u *endp; char_u *endp;
char_u *reg_pat; char_u *reg_pat;
char_u *p; char_u *p;
@ -8085,9 +8085,7 @@ file_pat_to_reg_pat (
check_length = p - pat + 1; check_length = p - pat + 1;
if (p + 1 >= pat_end) { if (p + 1 >= pat_end) {
/* The 'pattern' is a filetype check ONLY */ /* The 'pattern' is a filetype check ONLY */
reg_pat = (char_u *)alloc(check_length + 1); reg_pat = xmemdupz(pat, (size_t)check_length);
memmove(reg_pat, pat, (size_t)check_length);
reg_pat[check_length] = NUL;
return reg_pat; return reg_pat;
} }
} }
@ -8095,7 +8093,7 @@ file_pat_to_reg_pat (
} }
pat += check_length; pat += check_length;
size = 2 + check_length; size = 2 + (size_t)check_length;
#else #else
size = 2; /* '^' at start, '$' at end */ size = 2; /* '^' at start, '$' at end */
#endif #endif
@ -8125,9 +8123,7 @@ file_pat_to_reg_pat (
break; break;
} }
} }
reg_pat = alloc(size + 1); reg_pat = xmalloc(size + 1);
if (reg_pat == NULL)
return NULL;
#ifdef FEAT_OSFILETYPE #ifdef FEAT_OSFILETYPE
/* Copy the type check in to the start. */ /* Copy the type check in to the start. */

View File

@ -43,17 +43,6 @@
#include "os/os.h" #include "os/os.h"
static void try_to_free_memory(); static void try_to_free_memory();
static void *xmallocz(size_t size);
/// Allocates (len + 1) bytes of memory, duplicates `len` bytes of
/// `data` to the allocated memory, zero terminates the allocated memory,
/// and returns a pointer to the allocated memory. If the allocation fails,
/// the program dies.
///
/// @see {xmalloc}
/// @param data Pointer to the data that will be copied
/// @param len number of bytes that will be copied
static void *xmemdupz(const void *data, size_t len);
/* /*
* Note: if unsigned is 16 bits we can only allocate up to 64K with alloc(). * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
@ -155,6 +144,27 @@ void *xrealloc(void *ptr, size_t size)
return ret; return ret;
} }
void *xmallocz(size_t size)
{
size_t total_size = size + 1;
void *ret;
if (total_size < size) {
OUT_STR("Vim: Data too large to fit into virtual memory space\n");
preserve_exit();
}
ret = xmalloc(total_size);
((char*)ret)[size] = 0;
return ret;
}
void *xmemdupz(const void *data, size_t len)
{
return memcpy(xmallocz(len), data, len);
}
char * xstrdup(const char *str) char * xstrdup(const char *str)
{ {
char *ret = strdup(str); char *ret = strdup(str);
@ -350,24 +360,3 @@ void free_all_mem(void)
#endif #endif
static void *xmallocz(size_t size)
{
size_t total_size = size + 1;
void *ret;
if (total_size < size) {
OUT_STR("Vim: Data too large to fit into virtual memory space\n");
preserve_exit();
}
ret = xmalloc(total_size);
((char*)ret)[size] = 0;
return ret;
}
static void *xmemdupz(const void *data, size_t len)
{
return memcpy(xmallocz(len), data, len);
}

View File

@ -36,6 +36,23 @@ void *xcalloc(size_t count, size_t size)
void *xrealloc(void *ptr, size_t size) void *xrealloc(void *ptr, size_t size)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALLOC_SIZE(2) FUNC_ATTR_NONNULL_RET; FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALLOC_SIZE(2) FUNC_ATTR_NONNULL_RET;
/// xmalloc() wrapper that allocates size + 1 bytes and zeroes the last byte
///
/// @see {xmalloc}
/// @param size
/// @return pointer to allocated space. Never NULL
void *xmallocz(size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_RET;
/// Allocates (len + 1) bytes of memory, duplicates `len` bytes of
/// `data` to the allocated memory, zero terminates the allocated memory,
/// and returns a pointer to the allocated memory. If the allocation fails,
/// the program dies.
///
/// @see {xmalloc}
/// @param data Pointer to the data that will be copied
/// @param len number of bytes that will be copied
void *xmemdupz(const void *data, size_t len);
/// strdup() wrapper /// strdup() wrapper
/// ///
/// @see {xmalloc} /// @see {xmalloc}