blob: 8960362a6d74a05d98aaa5dea4b2c817b67ea2c0 [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
Christian Heimesb911cfd2013-07-23 01:31:15 +020095/* Python/marshal.c */
96
97static Py_ssize_t r_string(char *s, Py_ssize_t n, RFILE *p)
98{
99 __coverity_tainted_string_argument__(s);
100 return 0;
101}
102
103static long r_long(RFILE *p)
104{
105 long l;
106 unsigned char buffer[4];
107
108 r_string((char *)buffer, 4, p);
109 __coverity_tainted_string_sanitize_content__(buffer);
110 l = (long)buffer;
111 return l;
112}
113
114/* Coverity doesn't understand that fdopendir() may take ownership of fd. */
115
Christian Heimesa8e3f7a2015-04-16 20:25:03 +0200116DIR *fdopendir(int fd)
117{
Christian Heimesb911cfd2013-07-23 01:31:15 +0200118 DIR *d;
119 if (d) {
120 __coverity_close__(fd);
121 }
122 return d;
123}
124
Christian Heimesa8e3f7a2015-04-16 20:25:03 +0200125/* Modules/_datetime.c
126 *
127 * Coverity thinks that the input values for these function come from a
128 * tainted source PyDateTime_DATE_GET_* macros use bit shifting.
129 */
130static PyObject *
131build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag)
132{
133 PyObject *result;
Benjamin Peterson918aa892016-09-19 22:16:36 -0700134
Christian Heimesa8e3f7a2015-04-16 20:25:03 +0200135 __coverity_tainted_data_sanitize__(y);
136 __coverity_tainted_data_sanitize__(m);
137 __coverity_tainted_data_sanitize__(d);
138 __coverity_tainted_data_sanitize__(hh);
139 __coverity_tainted_data_sanitize__(mm);
140 __coverity_tainted_data_sanitize__(ss);
141 __coverity_tainted_data_sanitize__(dstflag);
142
143 return result;
144}
145
146static int
147ymd_to_ord(int year, int month, int day)
148{
149 int ord = 0;
150
151 __coverity_tainted_data_sanitize__(year);
152 __coverity_tainted_data_sanitize__(month);
153 __coverity_tainted_data_sanitize__(day);
154
155 return ord;
156}
157
158static int
159normalize_date(int *year, int *month, int *day)
160{
161 __coverity_tainted_data_sanitize__(*year);
162 __coverity_tainted_data_sanitize__(*month);
163 __coverity_tainted_data_sanitize__(*day);
164
165 return 0;
166}
167
168static int
169weekday(int year, int month, int day)
170{
171 int w = 0;
172
173 __coverity_tainted_data_sanitize__(year);
174 __coverity_tainted_data_sanitize__(month);
175 __coverity_tainted_data_sanitize__(day);
176
177 return w;
178}
179