fix [#33305] Bevel tool crashes Blender if the number of segments exceeds 28
authorCampbell Barton <ideasman42@gmail.com>
Mon, 26 Nov 2012 04:58:33 +0000 (04:58 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 26 Nov 2012 04:58:33 +0000 (04:58 +0000)
this was infact a general bug in BLI_array_grow_items(), surprising we didnt run into it before.
- growing the array for the first time would use the static var even if it wasn't big enough.

source/blender/blenlib/BLI_array.h

index 84cfe89f1b8fd21553599800d4ccaf89fde13214..a21778307c193c047d74dd90ee56d2c496649ac1 100644 (file)
            MEM_allocN_len(arr) / sizeof(*arr)                                    \
 )
 
+#define _bli_array_totalsize_static(arr)  \
+       (sizeof(_##arr##_static) / sizeof(*arr))
 
 #define BLI_array_totalsize(arr)  (                                           \
        (size_t)                                                                  \
        (((void *)(arr) == (void *)_##arr##_static && (void *)(arr) != NULL) ?    \
-           (sizeof(_##arr##_static) / sizeof(*arr)) :                            \
+           _bli_array_totalsize_static(arr) :                                    \
            BLI_array_totalsize_dyn(arr))                                         \
 )
 
  *
  * Allow for a large 'num' value when the new size is more then double
  * to allocate the exact sized array. */
-#define _bli_array_grow_items(arr, num)  (                                    \
-       (BLI_array_totalsize(arr) >= _##arr##_count + num) ?                      \
+
+/* grow an array by a specified number of items */
+#define BLI_array_grow_items(arr, num)  (                                     \
+       (((void *)(arr) == NULL) &&                                               \
+        ((void *)(_##arr##_static) != NULL) &&                                   \
+       /* dont add _##arr##_count below because it must be zero */               \
+        (_bli_array_totalsize_static(arr) >= _##arr##_count + num)) ?            \
+       /* we have an empty array and a static var big enough */                  \
+       ((arr = (void *)_##arr##_static), (_##arr##_count += (num)))              \
+           :                                                                     \
+       /* use existing static array or allocate */                               \
+       ((BLI_array_totalsize(arr) >= _##arr##_count + num) ?                     \
            (_##arr##_count += num) :                                             \
            (                                                                     \
                (void) (_##arr##_tmp = MEM_callocN(                               \
                (void) (arr = _##arr##_tmp                                        \
                        ),                                                        \
                (_##arr##_count += num)                                           \
-           )                                                                     \
-)
-
-/* grow an array by a specified number of items */
-#define BLI_array_grow_items(arr, num)  (                                     \
-       ((void *)(arr) == NULL && (void *)(_##arr##_static) != NULL) ?            \
-           ((arr = (void *)_##arr##_static), (_##arr##_count += num)) :          \
-           _bli_array_grow_items(arr, num)                                       \
+           ))                                                                    \
 )
 
 /* returns length of array */