forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commitc2d4eb1
committed
Fix actual and potential double-frees around tuplesort usage.
tuplesort_gettupleslot() passed back tuples allocated in the tuplesort'sown memory context, even when the caller was responsible to free them.This created a double-free hazard, because some callers might destroythe tuplesort object (via tuplesort_end) before trying to clean up thelast returned tuple. To avoid this, change the API to specify that thetuple is allocated in the caller's memory context. v10 and HEAD alreadydid things that way, but in 9.5 and 9.6 this is a live bug that candemonstrably cause crashes with some grouping-set usages.In 9.5 and 9.6, this requires doing an extra tuple copy in some cases,which is unfortunate. But the amount of refactoring needed to avoid itseems excessive for a back-patched change, especially since the caseswhere an extra copy happens are less performance-critical.Likewise change tuplesort_getdatum() to return pass-by-reference Datumsin the caller's context not the tuplesort's context. There seem to beno live bugs among its callers, but clearly the same sort of situationcould happen in future.For other tuplesort fetch routines, continue to allocate the memory inthe tuplesort's context. This is a little inconsistent with what we nowdo for tuplesort_gettupleslot() and tuplesort_getdatum(), but that'spreferable to adding new copy overhead in the back branches where it'sclearly unnecessary. These other fetch routines provide the weakestpossible guarantees about tuple memory lifespan from v10 on, anyway,so this actually seems more consistent overall.Adjust relevant comments to reflect these API redefinitions.Arguably, we should change the pre-9.5 branches as well, but sincethere are no known failure cases there, it seems not worth the risk.Peter Geoghegan, per report from Bernd Helmle. Reviewed by KyotaroHoriguchi; thanks also to Andreas Seltenreich for extracting aself-contained test case.Discussion:https://postgr.es/m/1512661638.9720.34.camel@oopsware.de1 parent1eb6d65 commitc2d4eb1
2 files changed
+13
-14
lines changedLines changed: 1 addition & 4 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
329 | 329 |
| |
330 | 330 |
| |
331 | 331 |
| |
332 |
| - | |
333 |
| - | |
334 |
| - | |
335 |
| - | |
| 332 | + | |
336 | 333 |
| |
337 | 334 |
| |
338 | 335 |
| |
|
Lines changed: 12 additions & 10 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2147 | 2147 |
| |
2148 | 2148 |
| |
2149 | 2149 |
| |
2150 |
| - | |
2151 |
| - | |
2152 |
| - | |
2153 |
| - | |
2154 |
| - | |
2155 |
| - | |
| 2150 | + | |
| 2151 | + | |
| 2152 | + | |
| 2153 | + | |
| 2154 | + | |
| 2155 | + | |
| 2156 | + | |
2156 | 2157 |
| |
2157 | 2158 |
| |
2158 | 2159 |
| |
| |||
2230 | 2231 |
| |
2231 | 2232 |
| |
2232 | 2233 |
| |
2233 |
| - | |
2234 |
| - | |
| 2234 | + | |
| 2235 | + | |
2235 | 2236 |
| |
2236 | 2237 |
| |
2237 | 2238 |
| |
| |||
2253 | 2254 |
| |
2254 | 2255 |
| |
2255 | 2256 |
| |
| 2257 | + | |
| 2258 | + | |
| 2259 | + | |
2256 | 2260 |
| |
2257 | 2261 |
| |
2258 | 2262 |
| |
| |||
2269 | 2273 |
| |
2270 | 2274 |
| |
2271 | 2275 |
| |
2272 |
| - | |
2273 |
| - | |
2274 | 2276 |
| |
2275 | 2277 |
| |
2276 | 2278 |
| |
|
0 commit comments
Comments
(0)