1 #ifndef XML_MEMORY_H_PRIVATE__ 2 #define XML_MEMORY_H_PRIVATE__ 3 4 #include "../../libxml.h" 5 6 #include <limits.h> 7 #include <stddef.h> 8 9 #ifndef SIZE_MAX 10 #define SIZE_MAX ((size_t) -1) 11 #endif 12 13 #define XML_MAX_ITEMS 1000000000 /* 1 billion */ 14 15 XML_HIDDEN void 16 xmlInitMemoryInternal(void); 17 XML_HIDDEN void 18 xmlCleanupMemoryInternal(void); 19 20 /** 21 * xmlGrowCapacity: 22 * @array: pointer to array 23 * @capacity: pointer to capacity (in/out) 24 * @elemSize: size of an element in bytes 25 * @min: elements in initial allocation 26 * @max: maximum elements in the array 27 * 28 * Grow an array by at least one element, checking for overflow. 29 * 30 * Returns the new array size on success, -1 on failure. 31 */ 32 static XML_INLINE int xmlGrowCapacity(int capacity,size_t elemSize,int min,int max)33xmlGrowCapacity(int capacity, size_t elemSize, int min, int max) { 34 int extra; 35 36 if (capacity <= 0) { 37 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 38 (void) min; 39 return(1); 40 #else 41 return(min); 42 #endif 43 } 44 45 if ((capacity >= max) || 46 ((size_t) capacity > SIZE_MAX / 2 / elemSize)) 47 return(-1); 48 49 /* Grow by 50% */ 50 extra = (capacity + 1) / 2; 51 52 if (capacity > max - extra) 53 return(max); 54 55 return(capacity + extra); 56 } 57 58 #endif /* XML_MEMORY_H_PRIVATE__ */ 59