3 * Copyright (C) 2014 Red Hat, Inc.
6 * Markus Armbruster <armbru@redhat.com>
7 * Paolo Bonzini <pbonzini@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or, at your
10 * option, any later version. See the COPYING file in the top-level directory.
15 * This is the source code for our Coverity user model file. The
16 * purpose of user models is to increase scanning accuracy by explaining
17 * code Coverity can't see (out of tree libraries) or doesn't
18 * sufficiently understand. Better accuracy means both fewer false
19 * positives and more true defects. Memory leaks in particular.
21 * - A model file can't import any header files. Some built-in primitives are
22 * available but not wchar_t, NULL etc.
23 * - Modeling doesn't need full structs and typedefs. Rudimentary structs
24 * and similar types are sufficient.
25 * - An uninitialized local variable signifies that the variable could be
28 * The model file must be uploaded by an admin in the analysis settings of
29 * http://scan.coverity.com/projects/378
32 #define NULL ((void *)0)
34 typedef unsigned char uint8_t;
36 typedef unsigned int uint32_t;
39 typedef unsigned long long uint64_t;
40 typedef long long int64_t;
45 typedef struct AddressSpace AddressSpace
;
46 typedef uint64_t hwaddr
;
48 static void __write(uint8_t *buf
, ssize_t len
)
51 __coverity_negative_sink__(len
);
55 __coverity_writeall__(buf
);
58 static void __read(uint8_t *buf
, ssize_t len
)
60 __coverity_negative_sink__(len
);
63 int last
= buf
[len
-1];
66 bool address_space_rw(AddressSpace
*as
, hwaddr addr
, uint8_t *buf
,
67 int len
, bool is_write
)
71 // TODO: investigate impact of treating reads as producing
72 // tainted data, with __coverity_tainted_data_argument__(buf).
73 if (is_write
) __write(buf
, len
); else __read(buf
, len
);
80 typedef struct {} name2keysym_t
;
81 static int get_keysym(const name2keysym_t
*table
,
86 __coverity_tainted_string_sanitize_content__(name
);
93 /* glib memory allocation functions.
95 * Note that we ignore the fact that g_malloc of 0 bytes returns NULL,
96 * and g_realloc of 0 bytes frees the pointer.
98 * Modeling this would result in Coverity flagging a lot of memory
99 * allocations as potentially returning NULL, and asking us to check
100 * whether the result of the allocation is NULL or not. However, the
101 * resulting pointer should never be dereferenced anyway, and in fact
102 * it is not in the vast majority of cases.
104 * If a dereference did happen, this would suppress a defect report
105 * for an actual null pointer dereference. But it's too unlikely to
106 * be worth wading through the false positives, and with some luck
107 * we'll get a buffer overflow reported anyway.
110 void *malloc(size_t);
111 void *calloc(size_t, size_t);
112 void *realloc(void *, size_t);
116 g_malloc(size_t n_bytes
)
119 __coverity_negative_sink__(n_bytes
);
120 mem
= malloc(n_bytes
== 0 ? 1 : n_bytes
);
121 if (!mem
) __coverity_panic__();
126 g_malloc0(size_t n_bytes
)
129 __coverity_negative_sink__(n_bytes
);
130 mem
= calloc(1, n_bytes
== 0 ? 1 : n_bytes
);
131 if (!mem
) __coverity_panic__();
135 void g_free(void *mem
)
140 void *g_realloc(void * mem
, size_t n_bytes
)
142 __coverity_negative_sink__(n_bytes
);
143 mem
= realloc(mem
, n_bytes
== 0 ? 1 : n_bytes
);
144 if (!mem
) __coverity_panic__();
148 void *g_try_malloc(size_t n_bytes
)
150 __coverity_negative_sink__(n_bytes
);
151 return malloc(n_bytes
== 0 ? 1 : n_bytes
);
154 void *g_try_malloc0(size_t n_bytes
)
156 __coverity_negative_sink__(n_bytes
);
157 return calloc(1, n_bytes
== 0 ? 1 : n_bytes
);
160 void *g_try_realloc(void *mem
, size_t n_bytes
)
162 __coverity_negative_sink__(n_bytes
);
163 return realloc(mem
, n_bytes
== 0 ? 1 : n_bytes
);
166 /* Other glib functions */
168 typedef struct _GIOChannel GIOChannel
;
169 GIOChannel
*g_io_channel_unix_new(int fd
)
171 GIOChannel
*c
= g_malloc0(sizeof(GIOChannel
));
172 __coverity_escape__(fd
);
176 void g_assertion_message_expr(const char *domain
,
182 __coverity_panic__();