##// END OF EJS Templates
dirs: fix trivial over-read of input data...
Augie Fackler -
r43419:2a0774e9 default
parent child Browse files
Show More
@@ -68,26 +68,41 b' static int _addpath(PyObject *dirs, PyOb'
68 while ((pos = _finddir(cpath, pos - 1)) != -1) {
68 while ((pos = _finddir(cpath, pos - 1)) != -1) {
69 PyObject *val;
69 PyObject *val;
70
70
71 if (pos < 2) {
72 key = PyBytes_FromStringAndSize(cpath, pos);
73 if (key == NULL)
74 goto bail;
75 } else {
71 /* It's likely that every prefix already has an entry
76 /* It's likely that every prefix already has an entry
72 in our dict. Try to avoid allocating and
77 in our dict. Try to avoid allocating and
73 deallocating a string for each prefix we check. */
78 deallocating a string for each prefix we check. */
74 if (key != NULL)
79 if (key != NULL)
75 ((PyBytesObject *)key)->ob_shash = -1;
80 ((PyBytesObject *)key)->ob_shash = -1;
76 else {
81 else {
77 /* Force Python to not reuse a small shared string. */
82 /* We know pos >= 2, so we won't get a small
78 key = PyBytes_FromStringAndSize(cpath,
83 * shared string. */
79 pos < 2 ? 2 : pos);
84 key = PyBytes_FromStringAndSize(cpath, pos);
80 if (key == NULL)
85 if (key == NULL)
81 goto bail;
86 goto bail;
82 }
87 }
83 /* Py_SIZE(o) refers to the ob_size member of the struct. Yes,
88 /* Py_SIZE(o) refers to the ob_size member of
84 * assigning to what looks like a function seems wrong. */
89 * the struct. Yes, assigning to what looks
90 * like a function seems wrong. */
85 Py_SIZE(key) = pos;
91 Py_SIZE(key) = pos;
86 ((PyBytesObject *)key)->ob_sval[pos] = '\0';
92 ((PyBytesObject *)key)->ob_sval[pos] = '\0';
93 }
87
94
88 val = PyDict_GetItem(dirs, key);
95 val = PyDict_GetItem(dirs, key);
89 if (val != NULL) {
96 if (val != NULL) {
90 PYLONG_VALUE(val) += 1;
97 PYLONG_VALUE(val) += 1;
98 if (pos < 2) {
99 /* This was a short string, so we
100 * probably got a small shared string
101 * we can't mutate on the next loop
102 * iteration. Clear it.
103 */
104 Py_CLEAR(key);
105 }
91 break;
106 break;
92 }
107 }
93
108
General Comments 0
You need to be logged in to leave comments. Login now