Python-2.7.3/Python/traceback.c

Location Tool Test ID Function Issue
/builddir/build/BUILD/Python-2.7.3/Python/traceback.c:194:19 clang-analyzer The left operand of '==' is a garbage value
/builddir/build/BUILD/Python-2.7.3/Python/traceback.c:194:19 clang-analyzer The left operand of '==' is a garbage value
  1 /* Traceback implementation */
  2 
  3 #include "Python.h"
  4 
  5 #include "code.h"
  6 #include "frameobject.h"
  7 #include "structmember.h"
  8 #include "osdefs.h"
  9 #include "traceback.h"
 10 
 11 #define OFF(x) offsetof(PyTracebackObject, x)
 12 
 13 static PyMemberDef tb_memberlist[] = {
 14     {"tb_next",         T_OBJECT,       OFF(tb_next), READONLY},
 15     {"tb_frame",        T_OBJECT,       OFF(tb_frame), READONLY},
 16     {"tb_lasti",        T_INT,          OFF(tb_lasti), READONLY},
 17     {"tb_lineno",       T_INT,          OFF(tb_lineno), READONLY},
 18     {NULL}      /* Sentinel */
 19 };
 20 
 21 static void
 22 tb_dealloc(PyTracebackObject *tb)
 23 {
 24     PyObject_GC_UnTrack(tb);
 25     Py_TRASHCAN_SAFE_BEGIN(tb)
 26     Py_XDECREF(tb->tb_next);
 27     Py_XDECREF(tb->tb_frame);
 28     PyObject_GC_Del(tb);
 29     Py_TRASHCAN_SAFE_END(tb)
 30 }
 31 
 32 static int
 33 tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
 34 {
 35     Py_VISIT(tb->tb_next);
 36     Py_VISIT(tb->tb_frame);
 37     return 0;
 38 }
 39 
 40 static void
 41 tb_clear(PyTracebackObject *tb)
 42 {
 43     Py_CLEAR(tb->tb_next);
 44     Py_CLEAR(tb->tb_frame);
 45 }
 46 
 47 PyTypeObject PyTraceBack_Type = {
 48     PyVarObject_HEAD_INIT(&PyType_Type, 0)
 49     "traceback",
 50     sizeof(PyTracebackObject),
 51     0,
 52     (destructor)tb_dealloc, /*tp_dealloc*/
 53     0,                  /*tp_print*/
 54     0,              /*tp_getattr*/
 55     0,                  /*tp_setattr*/
 56     0,                  /*tp_compare*/
 57     0,                  /*tp_repr*/
 58     0,                  /*tp_as_number*/
 59     0,                  /*tp_as_sequence*/
 60     0,                  /*tp_as_mapping*/
 61     0,                  /* tp_hash */
 62     0,                  /* tp_call */
 63     0,                  /* tp_str */
 64     0,                  /* tp_getattro */
 65     0,                  /* tp_setattro */
 66     0,                                          /* tp_as_buffer */
 67     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
 68     0,                                          /* tp_doc */
 69     (traverseproc)tb_traverse,                  /* tp_traverse */
 70     (inquiry)tb_clear,                          /* tp_clear */
 71     0,                                          /* tp_richcompare */
 72     0,                                          /* tp_weaklistoffset */
 73     0,                                          /* tp_iter */
 74     0,                                          /* tp_iternext */
 75     0,                                          /* tp_methods */
 76     tb_memberlist,                              /* tp_members */
 77     0,                                          /* tp_getset */
 78     0,                                          /* tp_base */
 79     0,                                          /* tp_dict */
 80 };
 81 
 82 static PyTracebackObject *
 83 newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
 84 {
 85     PyTracebackObject *tb;
 86     if ((next != NULL && !PyTraceBack_Check(next)) ||
 87                     frame == NULL || !PyFrame_Check(frame)) {
 88         PyErr_BadInternalCall();
 89         return NULL;
 90     }
 91     tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
 92     if (tb != NULL) {
 93         Py_XINCREF(next);
 94         tb->tb_next = next;
 95         Py_XINCREF(frame);
 96         tb->tb_frame = frame;
 97         tb->tb_lasti = frame->f_lasti;
 98         tb->tb_lineno = PyFrame_GetLineNumber(frame);
 99         PyObject_GC_Track(tb);
100     }
101     return tb;
102 }
103 
104 int
105 PyTraceBack_Here(PyFrameObject *frame)
106 {
107     PyThreadState *tstate = PyThreadState_GET();
108     PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
109     PyTracebackObject *tb = newtracebackobject(oldtb, frame);
110     if (tb == NULL)
111         return -1;
112     tstate->curexc_traceback = (PyObject *)tb;
113     Py_XDECREF(oldtb);
114     return 0;
115 }
116 
117 int
118 _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
119 {
120     int err = 0;
121     FILE *xfp = NULL;
122     char linebuf[2000];
123     int i;
124     char namebuf[MAXPATHLEN+1];
125 
126     if (filename == NULL)
127         return -1;
128     /* This is needed by Emacs' compile command */
129 #define FMT "  File \"%.500s\", line %d, in %.500s\n"
130     xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
131     if (xfp == NULL) {
132         /* Search tail of filename in sys.path before giving up */
133         PyObject *path;
134         const char *tail = strrchr(filename, SEP);
135         if (tail == NULL)
136             tail = filename;
137         else
138             tail++;
139         path = PySys_GetObject("path");
140         if (path != NULL && PyList_Check(path)) {
141             Py_ssize_t _npath = PyList_Size(path);
142             int npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int);
143             size_t taillen = strlen(tail);
144             for (i = 0; i < npath; i++) {
145                 PyObject *v = PyList_GetItem(path, i);
146                 if (v == NULL) {
147                     PyErr_Clear();
148                     break;
149                 }
150                 if (PyString_Check(v)) {
151                     size_t len;
152                     len = PyString_GET_SIZE(v);
153                     if (len + 1 + taillen >= MAXPATHLEN)
154                         continue; /* Too long */
155                     strcpy(namebuf, PyString_AsString(v));
156                     if (strlen(namebuf) != len)
157                         continue; /* v contains '\0' */
158                     if (len > 0 && namebuf[len-1] != SEP)
159                         namebuf[len++] = SEP;
160                     strcpy(namebuf+len, tail);
161                     xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE);
162                     if (xfp != NULL) {
163                         break;
164                     }
165                 }
166             }
167         }
168     }
169 
170     if (xfp == NULL)
171         return err;
172     if (err != 0) {
173         fclose(xfp);
174         return err;
175     }
176 
177     for (i = 0; i < lineno; i++) {
178         char* pLastChar = &linebuf[sizeof(linebuf)-2];
179         do {
180             *pLastChar = '\0';
181             if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL)
182                 break;
183             /* fgets read *something*; if it didn't get as
184                far as pLastChar, it must have found a newline
185                or hit the end of the file;              if pLastChar is \n,
186                it obviously found a newline; else we haven't
187                yet seen a newline, so must continue */
188         } while (*pLastChar != '\0' && *pLastChar != '\n');
189     }
190     if (i == lineno) {
191         char buf[11];
192         char *p = linebuf;
193         while (*p == ' ' || *p == '\t' || *p == '\014')
194             p++;
The left operand of '==' is a garbage value
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

The left operand of '==' is a garbage value
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

195 196 /* Write some spaces before the line */ 197 strcpy(buf, " "); 198 assert (strlen(buf) == 10); 199 while (indent > 0) { 200 if(indent < 10) 201 buf[indent] = '\0'; 202 err = PyFile_WriteString(buf, f); 203 if (err != 0) 204 break; 205 indent -= 10; 206 } 207 208 if (err == 0) 209 err = PyFile_WriteString(p, f); 210 if (err == 0 && strchr(p, '\n') == NULL) 211 err = PyFile_WriteString("\n", f); 212 } 213 fclose(xfp); 214 return err; 215 } 216 217 static int 218 tb_displayline(PyObject *f, const char *filename, int lineno, const char *name) 219 { 220 int err = 0; 221 char linebuf[2000]; 222 223 if (filename == NULL || name == NULL) 224 return -1; 225 /* This is needed by Emacs' compile command */ 226 #define FMT " File \"%.500s\", line %d, in %.500s\n" 227 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name); 228 err = PyFile_WriteString(linebuf, f); 229 if (err != 0) 230 return err; 231 return _Py_DisplaySourceLine(f, filename, lineno, 4); 232 } 233 234 static int 235 tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) 236 { 237 int err = 0; 238 long depth = 0; 239 PyTracebackObject *tb1 = tb; 240 while (tb1 != NULL) { 241 depth++; 242 tb1 = tb1->tb_next; 243 } 244 while (tb != NULL && err == 0) { 245 if (depth <= limit) { 246 err = tb_displayline(f, 247 PyString_AsString( 248 tb->tb_frame->f_code->co_filename), 249 tb->tb_lineno, 250 PyString_AsString(tb->tb_frame->f_code->co_name)); 251 } 252 depth--; 253 tb = tb->tb_next; 254 if (err == 0) 255 err = PyErr_CheckSignals(); 256 } 257 return err; 258 } 259 260 int 261 PyTraceBack_Print(PyObject *v, PyObject *f) 262 { 263 int err; 264 PyObject *limitv; 265 long limit = 1000; 266 if (v == NULL) 267 return 0; 268 if (!PyTraceBack_Check(v)) { 269 PyErr_BadInternalCall(); 270 return -1; 271 } 272 limitv = PySys_GetObject("tracebacklimit"); 273 if (limitv && PyInt_Check(limitv)) { 274 limit = PyInt_AsLong(limitv); 275 if (limit <= 0) 276 return 0; 277 } 278 err = PyFile_WriteString("Traceback (most recent call last):\n", f); 279 if (!err) 280 err = tb_printinternal((PyTracebackObject *)v, f, limit); 281 return err; 282 }