Versions Compared

Key

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

...

Code Block
bgColor#FFcccc
langcpp

struct A {
  unsigned long i;
};

struct S {
  unsigned char x;
  unsigned char B[sizeof(A)];
} s;


int main() {
  A *a = new(&s.B[0]) A;
  unsigned long val = 0xaabbccdd;
  a->i = val;
  return 0;
}

...

Code Block
bgColor#ccccff
langcpp

struct A {
  unsigned long i;
};

union AlignedUnion {
  unsigned char B[sizeof(A)];

private:
  unsigned long _align_;
} algn;

int main() {
  A *a = new(&algn.B[0]) A;
  unsigned long val = 0xaabbccdd;
  a->i = val;
  return (0);
}

...

Code Block
bgColor#ccccff
langcpp

#define ALIGN(X) __attribute__((aligned(X)))


struct A {
  unsigned long i;
};

struct S {
  unsigned char x;
  ALIGN(4) unsigned char B[sizeof(A)];
} s;


int main() {
  A *a = new(&s.B[0]) A;
  unsigned long val = 0xaabbccdd;
  a->i = val;
  return 0;
}

...

Code Block
bgColor#ccccff
langcpp

#define ALIGN(X) _declspec(align(X))


struct A {
  unsigned long i;
};

struct S {
  unsigned char x;
  ALIGN(4) unsigned char B[sizeof(A)];
} s;


int main() {
  A *a = new(&s.B[0]) A;
  unsigned long val = 0xaabbccdd;
  a->i = val;
  return 0;
}

Risk Assessment

Providing improperly-aligned pointers to placement new can result in undefined behavior, including abnormal termination.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MEM45-CPP

low

probable

low

P6

L2

...