diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-11-06 21:16:22 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-11-06 21:16:22 +0100 |
| commit | 1d7ed6b295a5a9509d8194ce2a03255f0fb5a5fc (patch) | |
| tree | c21f18f7f3dfd5268db816aabc7e633f9dd4c36a /src | |
| parent | 7792b7117bda76873ad8c5339d7441191c132703 (diff) | |
fix a theoretical bug where a shrinking realloc failure would leave a vector with pointer NULL and size > 0
Diffstat (limited to 'src')
| -rw-r--r-- | src/ot_vector.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/ot_vector.c b/src/ot_vector.c index ab0566c..23dfb10 100644 --- a/src/ot_vector.c +++ b/src/ot_vector.c | |||
| @@ -165,8 +165,11 @@ void vector_remove_torrent(ot_vector *vector, ot_torrent *match) { | |||
| 165 | 165 | ||
| 166 | memmove(match, match + 1, sizeof(ot_torrent) * (end - match - 1)); | 166 | memmove(match, match + 1, sizeof(ot_torrent) * (end - match - 1)); |
| 167 | if ((--vector->size * OT_VECTOR_SHRINK_THRESH < vector->space) && (vector->space >= OT_VECTOR_SHRINK_RATIO * OT_VECTOR_MIN_MEMBERS)) { | 167 | if ((--vector->size * OT_VECTOR_SHRINK_THRESH < vector->space) && (vector->space >= OT_VECTOR_SHRINK_RATIO * OT_VECTOR_MIN_MEMBERS)) { |
| 168 | vector->space /= OT_VECTOR_SHRINK_RATIO; | 168 | void *new_data = realloc(vector->data, (vector->space / OT_VECTOR_SHRINK_RATIO) * sizeof(ot_torrent)); |
| 169 | vector->data = realloc(vector->data, vector->space * sizeof(ot_torrent)); | 169 | if (new_data) { |
| 170 | vector->space /= OT_VECTOR_SHRINK_RATIO; | ||
| 171 | vector->data = new_data; | ||
| 172 | } | ||
| 170 | } | 173 | } |
| 171 | } | 174 | } |
| 172 | 175 | ||
| @@ -282,6 +285,9 @@ void vector_fixup_peers(ot_vector *vector, size_t peer_size) { | |||
| 282 | vector->space /= OT_VECTOR_SHRINK_RATIO; | 285 | vector->space /= OT_VECTOR_SHRINK_RATIO; |
| 283 | need_fix++; | 286 | need_fix++; |
| 284 | } | 287 | } |
| 285 | if (need_fix) | 288 | if (need_fix) { |
| 286 | vector->data = realloc(vector->data, vector->space * peer_size); | 289 | void *new_data = realloc(vector->data, vector->space * peer_size); |
| 290 | if (new_data) | ||
| 291 | vector->data = new_data; | ||
| 292 | } | ||
| 287 | } | 293 | } |
