From 1d7ed6b295a5a9509d8194ce2a03255f0fb5a5fc Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Fri, 6 Nov 2026 21:16:22 +0100 Subject: fix a theoretical bug where a shrinking realloc failure would leave a vector with pointer NULL and size > 0 --- src/ot_vector.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src') 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) { memmove(match, match + 1, sizeof(ot_torrent) * (end - match - 1)); if ((--vector->size * OT_VECTOR_SHRINK_THRESH < vector->space) && (vector->space >= OT_VECTOR_SHRINK_RATIO * OT_VECTOR_MIN_MEMBERS)) { - vector->space /= OT_VECTOR_SHRINK_RATIO; - vector->data = realloc(vector->data, vector->space * sizeof(ot_torrent)); + void *new_data = realloc(vector->data, (vector->space / OT_VECTOR_SHRINK_RATIO) * sizeof(ot_torrent)); + if (new_data) { + vector->space /= OT_VECTOR_SHRINK_RATIO; + vector->data = new_data; + } } } @@ -282,6 +285,9 @@ void vector_fixup_peers(ot_vector *vector, size_t peer_size) { vector->space /= OT_VECTOR_SHRINK_RATIO; need_fix++; } - if (need_fix) - vector->data = realloc(vector->data, vector->space * peer_size); + if (need_fix) { + void *new_data = realloc(vector->data, vector->space * peer_size); + if (new_data) + vector->data = new_data; + } } -- cgit v1.3