- Notifications
You must be signed in to change notification settings - Fork5
Commitec91ee8
committed
Fix access-to-already-freed-memory issue in plpython's error handling.
PLy_elog() could attempt to access strings that Python had already freed,because the strings that PLy_get_spi_error_data() returns are simplypointers into storage associated with the error "val" PyObject. That'sfine at the instant PLy_get_spi_error_data() returns them, but just afterthat PLy_traceback() intentionally releases the only refcount on thatobject, allowing it to be freed --- so that the strings we pass toereport() are dangling pointers.In principle this could result in garbage output or a coredump. Inpractice, I think the risk is pretty low, because there are no Pythonoperations between where we decrement that refcount and where we use thestrings (and copy them into PG storage), and thus no reason for Pythonto recycle the storage. Still, it's clearly hazardous, and it leads toValgrind complaints when running under a Valgrind that hasn't beenlobotomized to ignore Python memory allocations.The code was a mess anyway: we fetched the error data out of Python(clearing Python's error indicator) with PyErr_Fetch, examined it, pushedit back into Python with PyErr_Restore (re-setting the error indicator),then immediately pulled it back out with another PyErr_Fetch. Just toconfuse matters even more, there were some gratuitous-and-yet-hazardousPyErr_Clear calls in the "examine" step, and we didn't get around to doingPyErr_NormalizeException until after the second PyErr_Fetch, making it evenless clear which object was being manipulated where and whether we stillhad a refcount on it. (If PyErr_NormalizeException did substitute adifferent "val" object, it's possible that the problem could manifest forreal, because then we'd be doing assorted Python stuff with no refcounton the object we have string pointers into.)So, rearrange all that into some semblance of sanity, and don't decrementthe refcount on the Python error objects until the end of PLy_elog().In HEAD, I failed to resist the temptation to reformat some messy bitsfrom5c3c3cd along the way.Back-patch as far as 9.2, because the code is substantially the samethat far back. I believe that 9.1 has the bug as well; but the codearound it is rather different and I don't want to take a chance onbreaking something for what seems a low-probability problem.1 parentbf73016 commitec91ee8
1 file changed
+21
-21
lines changedLines changed: 21 additions & 21 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
21 | 21 |
| |
22 | 22 |
| |
23 | 23 |
| |
24 |
| - | |
| 24 | + | |
| 25 | + | |
25 | 26 |
| |
26 | 27 |
| |
27 | 28 |
| |
| |||
53 | 54 |
| |
54 | 55 |
| |
55 | 56 |
| |
| 57 | + | |
56 | 58 |
| |
57 | 59 |
| |
| 60 | + | |
| 61 | + | |
58 | 62 |
| |
59 | 63 |
| |
60 | 64 |
| |
61 | 65 |
| |
62 | 66 |
| |
63 |
| - | |
64 | 67 |
| |
65 |
| - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
66 | 71 |
| |
67 | 72 |
| |
68 | 73 |
| |
| |||
113 | 118 |
| |
114 | 119 |
| |
115 | 120 |
| |
| 121 | + | |
| 122 | + | |
| 123 | + | |
116 | 124 |
| |
117 | 125 |
| |
118 | 126 |
| |
| |||
123 | 131 |
| |
124 | 132 |
| |
125 | 133 |
| |
| 134 | + | |
| 135 | + | |
126 | 136 |
| |
127 | 137 |
| |
128 | 138 |
| |
129 |
| - | |
| 139 | + | |
130 | 140 |
| |
131 | 141 |
| |
132 | 142 |
| |
133 | 143 |
| |
| 144 | + | |
| 145 | + | |
| 146 | + | |
134 | 147 |
| |
135 | 148 |
| |
136 |
| - | |
| 149 | + | |
| 150 | + | |
137 | 151 |
| |
138 |
| - | |
139 |
| - | |
140 |
| - | |
141 | 152 |
| |
142 | 153 |
| |
143 | 154 |
| |
| |||
148 | 159 |
| |
149 | 160 |
| |
150 | 161 |
| |
151 |
| - | |
152 |
| - | |
153 |
| - | |
154 |
| - | |
155 |
| - | |
156 |
| - | |
| 162 | + | |
157 | 163 |
| |
158 | 164 |
| |
159 | 165 |
| |
| |||
164 | 170 |
| |
165 | 171 |
| |
166 | 172 |
| |
167 |
| - | |
168 |
| - | |
169 | 173 |
| |
170 | 174 |
| |
171 | 175 |
| |
| |||
332 | 336 |
| |
333 | 337 |
| |
334 | 338 |
| |
335 |
| - | |
336 |
| - | |
337 | 339 |
| |
338 | 340 |
| |
339 | 341 |
| |
| |||
367 | 369 |
| |
368 | 370 |
| |
369 | 371 |
| |
370 |
| - | |
| 372 | + | |
371 | 373 |
| |
372 | 374 |
| |
373 | 375 |
| |
| |||
384 | 386 |
| |
385 | 387 |
| |
386 | 388 |
| |
387 |
| - | |
388 |
| - | |
389 | 389 |
| |
390 | 390 |
| |
391 | 391 |
| |
|
0 commit comments
Comments
(0)