2828 * See header file: array.h (includes lots of macros)
2929 * HISTORY:
3030 * Last edited: Oct 8 21:56 2014 (rd)
31- * * Sep 19 15:41 2014 (rd): switch to long indices to avoid overflow
31+ * * Sep 19 15:41 2014 (rd): switch to long indices to avoid overflow
3232 * * May 5 10:55 2013 (rd): change RD address to rd@sanger.ac.uk
3333 * * Feb 14 11:21 2011 (rd): modified in 2009/10 by RD for stand-alone use
3434 * Created: Thu Dec 12 15:43:25 1989 (mieg)
@@ -122,13 +122,13 @@ void arrayDestroy (Array a)
122122
123123/**************/
124124
125- Array arrayCopy (Array a )
125+ Array arrayCopy (Array a )
126126{
127127 Array new ;
128128
129- if (!arrayExists (a ))
129+ if (!arrayExists (a ))
130130 die ("arrayCopy called on bad array %lx" , (long unsigned int ) a ) ;
131-
131+
132132 new = uArrayCreate (a -> dim , a -> size ) ;
133133 memcpy (new -> base , a -> base , a -> dim * a -> size ) ;
134134 new -> max = a -> max ;
@@ -137,7 +137,7 @@ Array arrayCopy (Array a)
137137
138138/******************************/
139139
140- void arrayExtend (Array a , long n )
140+ void arrayExtend (Array a , long n )
141141{
142142 char * new ;
143143
@@ -148,13 +148,30 @@ void arrayExtend (Array a, long n)
148148 return ;
149149
150150 totalAllocatedMemory -= a -> dim * a -> size ;
151- if (a -> dim * a -> size < 1 << 26 ) /* 64MB */
151+ if (a -> dim * a -> size < 67108864 ) /* 64MB */
152152 a -> dim *= 2 ;
153153 else
154- a -> dim += 1024 + (( 1 << 26 ) / a -> size ) ;
154+ a -> dim += 1024 + (67108864 / a -> size ) ;
155155 if (n >= a -> dim )
156156 a -> dim = n + 1 ;
157157
158+ /* Check for integer overflow before allocation */
159+ /* _mycalloc takes (long number, int size), but calloc expects size_t */
160+ /* Check if a->dim * a->size would overflow size_t or cause calloc to fail */
161+ if (a -> dim < 0 || a -> size <= 0 )
162+ die ("arrayExtend: invalid dimensions: dim=%ld, size=%d" , a -> dim , a -> size );
163+ /* Check for overflow: if a->dim * a->size would exceed size_t limits */
164+ /* Use size_t for the check to match what calloc expects */
165+ if (a -> dim > 0 ) {
166+ size_t dim_size = (size_t )a -> dim ;
167+ size_t elem_size = (size_t )a -> size ;
168+ size_t total_size = dim_size * elem_size ;
169+ /* Check for multiplication overflow */
170+ if (elem_size > 0 && total_size / elem_size != dim_size )
171+ die ("arrayExtend: size overflow: dim=%ld, size=%d (multiplication overflow)" ,
172+ a -> dim , a -> size );
173+ }
174+
158175 totalAllocatedMemory += a -> dim * a -> size ;
159176
160177 new = _mycalloc (a -> dim , a -> size ) ;
@@ -211,7 +228,7 @@ BOOL arrayFind(Array a, void *s, long *ip, ArrayOrder *order)
211228 int ord ;
212229 long i = 0 , j , k ;
213230
214- if (!arrayExists (a ))
231+ if (!arrayExists (a ))
215232 die ("arrayFind called on bad array %lx" , (long unsigned int ) a ) ;
216233
217234 j = arrayMax (a ) ;
@@ -229,7 +246,7 @@ BOOL arrayFind(Array a, void *s, long *ip, ArrayOrder *order)
229246 { if (ip ) * ip = j ;
230247 return FALSE ;
231248 }
232-
249+
233250 if (ord == 0 )
234251 { if (ip ) * ip = j ;
235252 return TRUE ;
@@ -306,19 +323,19 @@ void arrayCompress(Array a)
306323 if (arrayMax (a ) < 2 )
307324 return ;
308325
309- ab = a -> base ;
326+ ab = a -> base ;
310327 as = a -> size ;
311328 for (i = 1 , j = 0 ; i < arrayMax (a ) ; i ++ )
312329 { x = ab + i * as ; y = ab + j * as ;
313- for (k = a -> size ; k -- ;)
314- if (* x ++ != * y ++ )
330+ for (k = a -> size ; k -- ;)
331+ if (* x ++ != * y ++ )
315332 goto different ;
316333 continue ;
317-
334+
318335 different :
319336 if (i != ++ j )
320337 { x = ab + i * as ; y = ab + j * as ;
321- for (k = a -> size ; k -- ;)
338+ for (k = a -> size ; k -- ;)
322339 * y ++ = * x ++ ;
323340 }
324341 }
@@ -342,7 +359,7 @@ void arrayReport (int j)
342359 int i ;
343360 Array a ;
344361
345- fprintf (stderr , "Array report: %d created, %d active, %ld MB allocated\n" ,
362+ fprintf (stderr , "Array report: %d created, %d active, %ld MB allocated\n" ,
346363 totalNumberCreated , totalNumberActive , totalAllocatedMemory /(1024 * 1024 )) ;
347364
348365 if (reportArray )
@@ -357,13 +374,13 @@ void arrayReport (int j)
357374
358375/**************/
359376
360- void arrayStatus (int * nmadep , int * nusedp ,
377+ void arrayStatus (int * nmadep , int * nusedp ,
361378 long * memAllocp , long * memUsedp )
362- {
379+ {
363380 int i ;
364381 Array a ;
365382
366- * nmadep = totalNumberCreated ;
383+ * nmadep = totalNumberCreated ;
367384 * nusedp = totalNumberActive ;
368385 * memAllocp = totalAllocatedMemory ;
369386 * memUsedp = 0 ;
@@ -376,4 +393,3 @@ void arrayStatus (int *nmadep, int *nusedp,
376393
377394/************************ end of file ********************************/
378395/**********************************************************************/
379-
0 commit comments