diff options
author | wm4 <wm4@nowhere> | 2014-01-02 23:54:59 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2014-01-02 23:54:59 +0100 |
commit | 2cad237f8bea862923bf9e7e2d7c55544f6248e0 (patch) | |
tree | 907febddd4fb439bfa8b1058140000a7516f8955 /ta/ta_utils.c | |
parent | d4f37174a16afad6394e48e203abb41252e382d2 (diff) | |
download | mpv-2cad237f8bea862923bf9e7e2d7c55544f6248e0.tar.bz2 mpv-2cad237f8bea862923bf9e7e2d7c55544f6248e0.tar.xz |
ta: check overflow in array realloc macros
Diffstat (limited to 'ta/ta_utils.c')
-rw-r--r-- | ta/ta_utils.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/ta/ta_utils.c b/ta/ta_utils.c index 4d1e73fab6..a6b59f2e3d 100644 --- a/ta/ta_utils.c +++ b/ta/ta_utils.c @@ -30,6 +30,17 @@ size_t ta_calc_array_size(size_t element_size, size_t count) return element_size * count; } +// This is used when an array has to be enlarged for appending new elements. +// Return a "good" size for the new array (in number of elements). This returns +// a value >= nextidx, unless the calculation overflows, in which case SIZE_MAX +// is returned. +size_t ta_calc_prealloc_elems(size_t nextidx) +{ + if (nextidx >= ((size_t)-1) / 2 - 1) + return (size_t)-1; + return (nextidx + 1) * 2; +} + static void dummy_dtor(void *p){} /* Create an empty (size 0) TA allocation, which is prepared in a way such that |