aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames McCoy <jamessan@jamessan.com>2022-05-19 20:59:44 -0400
committerJames McCoy <jamessan@jamessan.com>2022-05-20 08:14:20 -0400
commit1da7e2b8ca1c89d69aec0830ce5ff1885cb75905 (patch)
tree1f685d40dd8b5f182ad56b81f1757eba9c3ea9ca
parenteb0aa8bb0ebc3cc233af6a5281c553d14ee57183 (diff)
downloadrneovim-1da7e2b8ca1c89d69aec0830ce5ff1885cb75905.tar.gz
rneovim-1da7e2b8ca1c89d69aec0830ce5ff1885cb75905.tar.bz2
rneovim-1da7e2b8ca1c89d69aec0830ce5ff1885cb75905.zip
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.
-rw-r--r--src/coverity-model.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/coverity-model.c b/src/coverity-model.c
index 2fd55c332c..4338b75ea2 100644
--- 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;
+}