forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit730089d
committed
Fix a low-probability crash in our qsort implementation.
It's standard for quicksort implementations, after having partitioned theinput into two subgroups, to recurse to process the smaller partition andthen handle the larger partition by iterating. This method guaranteesthat no more than log2(N) levels of recursion can be needed. However,Bentley and McIlroy argued that checking to see which partition is smallerisn't worth the cycles, and so their code doesn't do that but just alwaysrecurses on the left partition. In most cases that's fine; but withworst-case input we might need O(N) levels of recursion, and that meansthat qsort could be driven to stack overflow. Such an overflow seems tobe the only explanation for today's report from Yiqing Jin of a SIGSEGVin med3_tuple while creating an index of a couple billion entries with avery large maintenance_work_mem setting. Therefore, let's spend the fewadditional cycles and lines of code needed to choose the smaller partitionfor recursion.Also, fix up the qsort code so that it properly uses size_t not int forsome intermediate values representing numbers of items. This would onlybe a live risk when sorting more than INT_MAX bytes (in qsort/qsort_arg)or tuples (in qsort_tuple), which I believe would never happen with anycaller in the current core code --- but perhaps it could happen withcall sites in third-party modules? In any case, this is trouble waitingto happen, and the corrected code is probably if anything shorter andfaster than before, since it removes sign-extension steps that had tohappen when converting between int and size_t.In passing, move a couple of CHECK_FOR_INTERRUPTS() calls so that it'snot necessary to preserve the value of "r" across them, and prettifythe output of gen_qsort_tuple.pl a little.Back-patch to all supported branches. The odds of hitting this issueare probably higher in 9.4 and up than before, due to the new abilityto allocate sort workspaces exceeding 1GB, but there's no good reasonto believe that it's impossible to crash older branches this way.1 parentdc5075f commit730089d
File tree
3 files changed
+156
-60
lines changed- src
- backend/utils/sort
- port
3 files changed
+156
-60
lines changedLines changed: 60 additions & 26 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
14 | 14 |
| |
15 | 15 |
| |
16 | 16 |
| |
17 |
| - | |
18 |
| - | |
19 |
| - | |
20 |
| - | |
21 |
| - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
22 | 24 |
| |
23 | 25 |
| |
24 | 26 |
| |
| |||
43 | 45 |
| |
44 | 46 |
| |
45 | 47 |
| |
| 48 | + | |
46 | 49 |
| |
47 | 50 |
| |
48 | 51 |
| |
| 52 | + | |
49 | 53 |
| |
50 | 54 |
| |
51 | 55 |
| |
52 | 56 |
| |
53 | 57 |
| |
54 | 58 |
| |
55 | 59 |
| |
56 |
| - | |
| 60 | + | |
| 61 | + | |
57 | 62 |
| |
58 | 63 |
| |
59 | 64 |
| |
| |||
78 | 83 |
| |
79 | 84 |
| |
80 | 85 |
| |
81 |
| - | |
| 86 | + | |
82 | 87 |
| |
83 | 88 |
| |
84 | 89 |
| |
| |||
92 | 97 |
| |
93 | 98 |
| |
94 | 99 |
| |
| 100 | + | |
95 | 101 |
| |
96 | 102 |
| |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
97 | 110 |
| |
98 | 111 |
| |
99 | 112 |
| |
| |||
114 | 127 |
| |
115 | 128 |
| |
116 | 129 |
| |
117 |
| - | |
| 130 | + | |
| 131 | + | |
118 | 132 |
| |
119 | 133 |
| |
120 | 134 |
| |
| |||
141 | 155 |
| |
142 | 156 |
| |
143 | 157 |
| |
144 |
| - | |
145 |
| - | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
146 | 161 |
| |
147 | 162 |
| |
148 | 163 |
| |
| |||
173 | 188 |
| |
174 | 189 |
| |
175 | 190 |
| |
176 |
| - | |
| 191 | + | |
| 192 | + | |
177 | 193 |
| |
178 | 194 |
| |
179 | 195 |
| |
| |||
187 | 203 |
| |
188 | 204 |
| |
189 | 205 |
| |
190 |
| - | |
191 | 206 |
| |
192 | 207 |
| |
193 | 208 |
| |
194 | 209 |
| |
195 | 210 |
| |
196 | 211 |
| |
| 212 | + | |
197 | 213 |
| |
198 | 214 |
| |
199 | 215 |
| |
200 |
| - | |
201 | 216 |
| |
202 | 217 |
| |
203 | 218 |
| |
204 | 219 |
| |
205 | 220 |
| |
206 | 221 |
| |
| 222 | + | |
207 | 223 |
| |
208 | 224 |
| |
209 | 225 |
| |
| |||
212 | 228 |
| |
213 | 229 |
| |
214 | 230 |
| |
215 |
| - | |
216 |
| - | |
217 |
| - | |
218 |
| - | |
219 |
| - | |
220 |
| - | |
221 |
| - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
222 | 238 |
| |
223 |
| - | |
224 |
| - | |
225 |
| - | |
226 |
| - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
227 | 263 |
| |
228 |
| - | |
229 | 264 |
| |
230 |
| - | |
231 | 265 |
| |
232 | 266 |
|
Lines changed: 49 additions & 18 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
6 | 6 |
| |
7 | 7 |
| |
8 | 8 |
| |
| 9 | + | |
9 | 10 |
| |
10 | 11 |
| |
11 | 12 |
| |
| |||
54 | 55 |
| |
55 | 56 |
| |
56 | 57 |
| |
| 58 | + | |
57 | 59 |
| |
58 | 60 |
| |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
59 | 68 |
| |
| 69 | + | |
60 | 70 |
| |
61 | 71 |
| |
62 | 72 |
| |
| |||
89 | 99 |
| |
90 | 100 |
| |
91 | 101 |
| |
92 |
| - | |
| 102 | + | |
93 | 103 |
| |
94 | 104 |
| |
95 | 105 |
| |
| |||
109 | 119 |
| |
110 | 120 |
| |
111 | 121 |
| |
112 |
| - | |
113 |
| - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
114 | 125 |
| |
115 | 126 |
| |
116 | 127 |
| |
| |||
141 | 152 |
| |
142 | 153 |
| |
143 | 154 |
| |
144 |
| - | |
| 155 | + | |
| 156 | + | |
145 | 157 |
| |
146 | 158 |
| |
147 | 159 |
| |
| |||
178 | 190 |
| |
179 | 191 |
| |
180 | 192 |
| |
181 |
| - | |
182 |
| - | |
183 |
| - | |
184 |
| - | |
185 |
| - | |
186 |
| - | |
187 |
| - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
188 | 200 |
| |
189 |
| - | |
190 |
| - | |
191 |
| - | |
192 |
| - | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
193 | 225 |
| |
194 |
| - | |
195 | 226 |
| |
196 | 227 |
| |
197 | 228 |
| |
198 |
| - | |
| 229 | + | |
199 | 230 |
| |
200 | 231 |
| |
201 | 232 |
| |
202 | 233 |
| |
203 |
| - | |
| 234 | + | |
204 | 235 |
|
0 commit comments
Comments
(0)