neovim

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

coverity-model.c (2953B)


      1 // Coverity Scan model
      2 //
      3 // This is a modeling file for Coverity Scan. Modeling helps to avoid false
      4 // positives.
      5 //
      6 // - A model file can't import any header files.
      7 // - Therefore only some built-in primitives like int, char and void are
      8 //   available but not wchar_t, NULL etc.
      9 // - Modeling doesn't need full structs and typedefs. Rudimentary structs
     10 //   and similar types are sufficient.
     11 // - An uninitialized local pointer is not an error. It signifies that the
     12 //   variable could be either NULL or have some data.
     13 //
     14 // Coverity Scan doesn't pick up modifications automatically. The model file
     15 // must be uploaded by an admin in the analysis settings of
     16 // http://scan.coverity.com/projects/neovim-neovim
     17 //
     18 
     19 // Issue 105985
     20 //
     21 // Teach coverity that uv_pipe_open saves fd on success (0 return value)
     22 // and doesn't save it on failure (return value != 0).
     23 
     24 struct uv_pipe_s {
     25  int something;
     26 };
     27 
     28 int uv_pipe_open(struct uv_pipe_s *handle, int fd)
     29 {
     30  int result;
     31  if (result == 0) {
     32    __coverity_escape__(fd);
     33  }
     34  return result;
     35 }
     36 
     37 // Hint Coverity that adding item to d avoids losing track
     38 // of the memory allocated for item.
     39 typedef struct {} dictitem_T;
     40 typedef struct {} dict_T;
     41 int tv_dict_add(dict_T *const d, dictitem_T *const item)
     42 {
     43  __coverity_escape__(item);
     44 }
     45 
     46 void *malloc(size_t size)
     47 {
     48  int has_mem;
     49  if (has_mem)
     50    return __coverity_alloc__(size);
     51  else
     52    return 0;
     53 }
     54 
     55 void *try_malloc(size_t size)
     56 {
     57  size_t allocated_size = size ? size : 1;
     58  return malloc(allocated_size);
     59 }
     60 
     61 void *xmalloc(size_t size)
     62 {
     63  void *p = malloc(size);
     64  if (!p)
     65    __coverity_panic__();
     66  return p;
     67 }
     68 
     69 void xfree(void * ptr)
     70 {
     71  __coverity_free__(ptr);
     72 }
     73 
     74 void *xcalloc(size_t count, size_t size)
     75 {
     76  size_t allocated_count = count && size ? count : 1;
     77  size_t allocated_size = count && size ? size : 1;
     78  void *p = try_malloc(allocated_count * allocated_size);
     79  if (!p)
     80    __coverity_panic__();
     81  __coverity_writeall0__(p);
     82  return p;
     83 }
     84 
     85 void *xrealloc(void *ptr, size_t size)
     86 {
     87  __coverity_escape__(ptr);
     88  void * p = xmalloc(size);
     89  __coverity_writeall__(p);
     90  return p;
     91 }
     92 
     93 void *xmallocz(size_t size)
     94 {
     95  void * p = malloc(size + 1);
     96  ((char*)p)[size] = 0;
     97  return p;
     98 }
     99 
    100 void * xmemdupz(const void * data, size_t len)
    101 {
    102  void * p = xmallocz(len);
    103  __coverity_writeall__(p);
    104  ((char*)p)[len] = 0;
    105  return p;
    106 }
    107 
    108 void * xmemdup(const void *data, size_t len)
    109 {
    110  void * p = xmalloc(len);
    111  __coverity_writeall__(p);
    112  return p;
    113 }
    114 
    115 // Teach coverity that lua errors are noreturn
    116 
    117 typedef struct {} lua_State;
    118 
    119 int luaL_typerror(lua_State *L, int narg, const char *tname)
    120 {
    121  __coverity_panic__();
    122  return 0;
    123 }
    124 
    125 int luaL_error(lua_State *L, const char *fmt, ...)
    126 {
    127  __coverity_panic__();
    128  return 0;
    129 }
    130 
    131 int luaL_argerror(lua_State *L, int numarg, const char *extramsg)
    132 {
    133  __coverity_panic__();
    134  return 0;
    135 }
    136 
    137 void *luaL_checkudata(lua_State *L, int ud, const char *tname)
    138 {
    139  return __coverity_alloc_nosize__()
    140 }