Print this page
4922 all calloc() implementations should check for overflow


 314 
 315                 old = (oversize_t *)(mem - OVSZ_SIZE);
 316                 (void) memcpy(new, data_ptr, MIN(bytes, old->size - shift));
 317                 free(ptr);
 318                 return (new);
 319         }
 320 
 321         cacheptr = (cache_t *)*(uintptr_t *)mem;
 322 
 323         (void) memcpy(new, data_ptr,
 324             MIN(cacheptr->mt_size - OVERHEAD - shift, bytes));
 325         free(ptr);
 326 
 327         return (new);
 328 }
 329 
 330 void *
 331 calloc(size_t nelem, size_t bytes)
 332 {
 333         void * ptr;
 334         size_t size = nelem * bytes;












 335 
 336         ptr = malloc(size);
 337         if (ptr == NULL)
 338                 return (NULL);
 339         (void) memset(ptr, 0, size);
 340 
 341         return (ptr);
 342 }
 343 
 344 void
 345 free(void * ptr)
 346 {
 347         cache_t *cacheptr;
 348         caddr_t mem;
 349         int32_t i;
 350         caddr_t freeblocks;
 351         uintptr_t offset;
 352         uchar_t mask;
 353         int32_t which_bit, num_bytes;
 354 




 314 
 315                 old = (oversize_t *)(mem - OVSZ_SIZE);
 316                 (void) memcpy(new, data_ptr, MIN(bytes, old->size - shift));
 317                 free(ptr);
 318                 return (new);
 319         }
 320 
 321         cacheptr = (cache_t *)*(uintptr_t *)mem;
 322 
 323         (void) memcpy(new, data_ptr,
 324             MIN(cacheptr->mt_size - OVERHEAD - shift, bytes));
 325         free(ptr);
 326 
 327         return (new);
 328 }
 329 
 330 void *
 331 calloc(size_t nelem, size_t bytes)
 332 {
 333         void * ptr;
 334         size_t size;
 335 
 336         if (nelem == 0 || bytes == 0) {
 337                 size = 0;
 338         } else {
 339                 size = nelem * bytes;
 340 
 341                 /* check for overflow */
 342                 if ((size / nelem) != bytes) {
 343                         errno = ENOMEM;
 344                         return (NULL);
 345                 }
 346         }
 347 
 348         ptr = malloc(size);
 349         if (ptr == NULL)
 350                 return (NULL);
 351         (void) memset(ptr, 0, size);
 352 
 353         return (ptr);
 354 }
 355 
 356 void
 357 free(void * ptr)
 358 {
 359         cache_t *cacheptr;
 360         caddr_t mem;
 361         int32_t i;
 362         caddr_t freeblocks;
 363         uintptr_t offset;
 364         uchar_t mask;
 365         int32_t which_bit, num_bytes;
 366