ci(coverity): model our allocation functions

Coverity was reporting false positives, particularly around for non-NUL
terminated strings around uses of xmemdupz().  The updated model
ensures Coverity understands xmemdupz allocates an extra byte and sets it
to NUL as well as the main details of our other allocation related
wrappers.
This commit is contained in:
James McCoy
2022-05-19 20:59:44 -04:00
parent eb0aa8bb0e
commit 1da7e2b8ca

View File

@ -42,3 +42,72 @@ int tv_dict_add(dict_T *const d, dictitem_T *const item)
{
__coverity_escape__(item);
}
void *malloc(size_t size)
{
int has_mem;
if (has_mem)
return __coverity_alloc__(size);
else
return 0;
}
void *try_malloc(size_t size)
{
size_t allocated_size = size ? size : 1;
return malloc(allocated_size);
}
void *xmalloc(size_t size)
{
void *p = malloc(size);
if (!p)
__coverity_panic__();
return p;
}
void xfree(void * ptr)
{
__coverity_free__(ptr);
}
void *xcalloc(size_t count, size_t size)
{
size_t allocated_count = count && size ? count : 1;
size_t allocated_size = count && size ? size : 1;
void *p = try_malloc(allocated_count * allocated_size);
if (!p)
__coverity_panic__();
__coverity_writeall0__(p);
return p;
}
void *xrealloc(void *ptr, size_t size)
{
__coverity_escape__(ptr);
void * p = xmalloc(size);
__coverity_writeall__(p);
return p;
}
void *xmallocz(size_t size)
{
void * p = malloc(size + 1);
((char*)p)[size] = 0;
return p;
}
void * xmemdupz(const void * data, size_t len)
{
void * p = xmallocz(len);
__coverity_writeall__(p);
((char*)p)[len] = 0;
return p;
}
void * xmemdup(const void *data, size_t len)
{
void * p = xmalloc(len);
__coverity_writeall__(p);
return p;
}