blob: 1ae617911ce653226890dd94f698969a2c2a777c [file] [log] [blame]
Christian Heimesb911cfd2013-07-23 01:31:15 +02001/* 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/200
Christian Heimesb911cfd2013-07-23 01:31:15 +020017 */
18
Christian Heimesb911cfd2013-07-23 01:31:15 +020019/* dummy definitions, in most cases struct fields aren't required. */
20
21#define NULL (void *)0
Christian Heimes8cda5e62013-07-26 18:00:12 +020022#define assert(op) /* empty */
Christian Heimesb911cfd2013-07-23 01:31:15 +020023typedef int sdigit;
24typedef long Py_ssize_t;
25typedef unsigned short wchar_t;
26typedef struct {} PyObject;
27typedef struct {} grammar;
Christian Heimesb911cfd2013-07-23 01:31:15 +020028typedef struct {} DIR;
29typedef struct {} RFILE;
30
Christian Heimesb911cfd2013-07-23 01:31:15 +020031/* Python/pythonrun.c
Martin Panter69332c12016-08-04 13:07:31 +000032 * resource leak false positive */
Christian Heimesb911cfd2013-07-23 01:31:15 +020033
34void Py_FatalError(const char *msg) {
35 __coverity_panic__();
36}
37
38/* Objects/longobject.c
39 * NEGATIVE_RETURNS false positive */
40
Christian Heimesb911cfd2013-07-23 01:31:15 +020041static PyObject *get_small_int(sdigit ival)
42{
Christian Heimes8cda5e62013-07-26 18:00:12 +020043 /* Never returns NULL */
Christian Heimesb911cfd2013-07-23 01:31:15 +020044 PyObject *p;
Christian Heimes8cda5e62013-07-26 18:00:12 +020045 assert(p != NULL);
Christian Heimesb911cfd2013-07-23 01:31:15 +020046 return p;
47}
48
Christian Heimes8cda5e62013-07-26 18:00:12 +020049PyObject *PyLong_FromLong(long ival)
50{
51 PyObject *p;
52 int maybe;
53
54 if ((ival >= -5) && (ival < 257 + 5)) {
55 p = get_small_int(ival);
56 assert(p != NULL);
57 return p;
58 }
59 if (maybe)
60 return p;
61 else
62 return NULL;
63}
64
Benjamin Petersonaf580df2016-09-06 10:46:49 -070065PyObject *PyLong_FromLongLong(long long ival)
Christian Heimes8cda5e62013-07-26 18:00:12 +020066{
67 return PyLong_FromLong((long)ival);
68}
69
70PyObject *PyLong_FromSsize_t(Py_ssize_t ival)
71{
72 return PyLong_FromLong((long)ival);
73}
74
Christian Heimesb911cfd2013-07-23 01:31:15 +020075/* tainted sinks
76 *
77 * Coverity considers argv, environ, read() data etc as tained.
78 */
79
80PyObject *PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
81{
82 __coverity_tainted_data_sink__(filename);
83 return NULL;
84}
85
86/* Python/fileutils.c */
Victor Stinnerf6a271a2014-08-01 12:28:48 +020087wchar_t *Py_DecodeLocale(const char* arg, size_t *size)
Christian Heimesb911cfd2013-07-23 01:31:15 +020088{
89 wchar_t *w;
90 __coverity_tainted_data_sink__(arg);
91 __coverity_tainted_data_sink__(size);
92 return w;
93}
94
95/* Parser/pgenmain.c */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020096grammar *getgrammar(const char *filename)
Christian Heimesb911cfd2013-07-23 01:31:15 +020097{
98 grammar *g;
99 __coverity_tainted_data_sink__(filename);
100 return g;
101}
102
103/* Python/marshal.c */
104
105static Py_ssize_t r_string(char *s, Py_ssize_t n, RFILE *p)
106{
107 __coverity_tainted_string_argument__(s);
108 return 0;
109}
110
111static long r_long(RFILE *p)
112{
113 long l;
114 unsigned char buffer[4];
115
116 r_string((char *)buffer, 4, p);
117 __coverity_tainted_string_sanitize_content__(buffer);
118 l = (long)buffer;
119 return l;
120}
121
122/* Coverity doesn't understand that fdopendir() may take ownership of fd. */
123
Christian Heimesa8e3f7a2015-04-16 20:25:03 +0200124DIR *fdopendir(int fd)
125{
Christian Heimesb911cfd2013-07-23 01:31:15 +0200126 DIR *d;
127 if (d) {
128 __coverity_close__(fd);
129 }
130 return d;
131}
132
Christian Heimesa8e3f7a2015-04-16 20:25:03 +0200133/* Modules/_datetime.c
134 *
135 * Coverity thinks that the input values for these function come from a
136 * tainted source PyDateTime_DATE_GET_* macros use bit shifting.
137 */
138static PyObject *
139build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag)
140{
141 PyObject *result;
Benjamin Peterson918aa892016-09-19 22:16:36 -0700142
Christian Heimesa8e3f7a2015-04-16 20:25:03 +0200143 __coverity_tainted_data_sanitize__(y);
144 __coverity_tainted_data_sanitize__(m);
145 __coverity_tainted_data_sanitize__(d);
146 __coverity_tainted_data_sanitize__(hh);
147 __coverity_tainted_data_sanitize__(mm);
148 __coverity_tainted_data_sanitize__(ss);
149 __coverity_tainted_data_sanitize__(dstflag);
150
151 return result;
152}
153
154static int
155ymd_to_ord(int year, int month, int day)
156{
157 int ord = 0;
158
159 __coverity_tainted_data_sanitize__(year);
160 __coverity_tainted_data_sanitize__(month);
161 __coverity_tainted_data_sanitize__(day);
162
163 return ord;
164}
165
166static int
167normalize_date(int *year, int *month, int *day)
168{
169 __coverity_tainted_data_sanitize__(*year);
170 __coverity_tainted_data_sanitize__(*month);
171 __coverity_tainted_data_sanitize__(*day);
172
173 return 0;
174}
175
176static int
177weekday(int year, int month, int day)
178{
179 int w = 0;
180
181 __coverity_tainted_data_sanitize__(year);
182 __coverity_tainted_data_sanitize__(month);
183 __coverity_tainted_data_sanitize__(day);
184
185 return w;
186}
187