Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ffcccc
size_t size = 16;
size_t resize = 1024;
size_t alignment = 1 << 12;
int *ptr;
int *ptr1;

if((ptr = aligned_alloc(alignalignment , size)) == NULL) {
  /* handle  exit(0);error */
}

if((ptr1 = realloc(ptr, resize)) == NULL) {
  exit(0); /* handle error */
}

The resulting program has undefined behavior as the alignment that realloc() enforces is different from aligned_alloc() function's alignment.

...

Code Block
bgColor#ccccff
size_t size = 16;
size_t resize = 1024;
size_t alignment = 1 << 12;
size_t newsize;
int *ptr;
int *ptr1;

if((ptr = aligned_alloc(alignalignment , size)) == NULL) {
  /* handle  exit(0);error */
}

if((ptr1 = aligned_alloc(alignalignment , resize)) == NULL) {
  exit(0);/* handle error */
}

newsize = MIN(size, resize);
if((memcpy(ptr1, ptr, newsize) == NULL) {
 exit(0); /* handle error */
}

free(ptr);

Risk Assessment

...