mirror of
https://github.com/neovim/neovim.git
synced 2025-09-27 13:38:34 +00:00
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:

committed by
Thiago de Arruda

parent
471da2de81
commit
1befc49414
53
src/memory.c
53
src/memory.c
@@ -43,17 +43,6 @@
|
||||
#include "os/os.h"
|
||||
|
||||
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().
|
||||
@@ -155,6 +144,27 @@ void *xrealloc(void *ptr, size_t size)
|
||||
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 *ret = strdup(str);
|
||||
@@ -350,24 +360,3 @@ void free_all_mem(void)
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user