Commit 5878864
committed
Fix ownership and lifetime bugs in refdb backend callbacks
The refdb backend callbacks that hand a reference to libgit2 all had
the same bug class: they leaked the Python Reference returned or
yielded by user code, and borrowed the inner git_reference pointer
from it, so releasing the object without leaking would leave libgit2
with a dangling pointer.
git_reference_dup() cannot be used to make a safe copy, because it
requires the db field to be set, which libgit2 only does after the
callback returns. Instead, detach the pointer when the callback
returned a fresh object (refcount 1), and copy it with
git_reference__alloc()/git_reference__alloc_symbolic() when it
returned a shared object, e.g. one the backend caches and returns on
every call; the copy loses the cached peel, which is only a hint.
Fixed callbacks:
- lookup() and rename() now handle a cached Reference instead of
invalidating it on first use (the second lookup handed libgit2 a
NULL pointer).
- The iterator next() callback leaked every yielded Reference and
borrowed its pointer, a double free waiting for the object to be
collected; the glob filter also leaked every non-matching yield.
The next_name() callback leaked as well, and now keeps the last
yielded Reference alive so the borrowed name stays valid until the
next call.
- The iterator constructor crashed on a NULL glob, which is exactly
what git_reference_iterator_new() passes; it had never been
exercised through libgit2 before.
Also fix Reference(name, oid, None) passing an uninitialized peel OID
to git_reference__alloc(), so every backend-constructed direct
reference carried a garbage peel into libgit2.
Add test_lookup_cached_callback, test_iterator_callback and
test_iterator_callback_no_leak, driving cached Reference objects
through libgit2; each crashes or fails against the old code.
Fixes #1475
Assisted-by: Kimi Code1 parent 2efb706 commit 5878864
4 files changed
Lines changed: 176 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| 70 | + | |
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
72 | 124 | | |
73 | 125 | | |
74 | 126 | | |
75 | 127 | | |
76 | 128 | | |
77 | | - | |
78 | | - | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
79 | 140 | | |
80 | | - | |
81 | | - | |
| 141 | + | |
| 142 | + | |
82 | 143 | | |
83 | 144 | | |
| 145 | + | |
84 | 146 | | |
85 | 147 | | |
86 | 148 | | |
| |||
91 | 153 | | |
92 | 154 | | |
93 | 155 | | |
| 156 | + | |
| 157 | + | |
94 | 158 | | |
95 | 159 | | |
96 | 160 | | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
| 161 | + | |
104 | 162 | | |
105 | 163 | | |
106 | 164 | | |
| |||
109 | 167 | | |
110 | 168 | | |
111 | 169 | | |
| 170 | + | |
| 171 | + | |
112 | 172 | | |
113 | 173 | | |
114 | 174 | | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
120 | 179 | | |
121 | 180 | | |
122 | 181 | | |
| |||
125 | 184 | | |
126 | 185 | | |
127 | 186 | | |
| 187 | + | |
128 | 188 | | |
129 | 189 | | |
130 | 190 | | |
| |||
144 | 204 | | |
145 | 205 | | |
146 | 206 | | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
147 | 213 | | |
148 | 214 | | |
149 | 215 | | |
150 | 216 | | |
151 | 217 | | |
152 | | - | |
153 | 218 | | |
154 | 219 | | |
155 | 220 | | |
| |||
207 | 272 | | |
208 | 273 | | |
209 | 274 | | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
| 275 | + | |
217 | 276 | | |
218 | 277 | | |
219 | 278 | | |
| |||
321 | 380 | | |
322 | 381 | | |
323 | 382 | | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
| 383 | + | |
331 | 384 | | |
332 | 385 | | |
333 | 386 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
173 | | - | |
| 173 | + | |
| 174 | + | |
174 | 175 | | |
175 | 176 | | |
176 | 177 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| 37 | + | |
| 38 | + | |
36 | 39 | | |
37 | 40 | | |
38 | 41 | | |
| |||
96 | 99 | | |
97 | 100 | | |
98 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
99 | 137 | | |
100 | 138 | | |
101 | 139 | | |
| |||
106 | 144 | | |
107 | 145 | | |
108 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
109 | 196 | | |
110 | 197 | | |
111 | 198 | | |
| |||
0 commit comments