neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

commit 1da7e2b8ca1c89d69aec0830ce5ff1885cb75905
parent eb0aa8bb0ebc3cc233af6a5281c553d14ee57183
Author: James McCoy <jamessan@jamessan.com>
Date:   Thu, 19 May 2022 20:59:44 -0400

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.

Diffstat:
Msrc/coverity-model.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 69 insertions(+), 0 deletions(-)

diff --git a/src/coverity-model.c b/src/coverity-model.c @@ -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; +}