File: realloc-test.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (45 lines) | stat: -rw-r--r-- 1,263 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Test basic realloc functionality.
// RUN: %clang_hwasan %s -o %t && %run %t
// RUN: %clang_hwasan %s -DREALLOCARRAY -o %t && %run %t

#include <assert.h>
#include <sanitizer/hwasan_interface.h>
#include <stdlib.h>

#ifdef REALLOCARRAY
extern "C" void *reallocarray(void *, size_t nmemb, size_t size);
#define REALLOC(p, s) reallocarray(p, 1, s)
#else
#define REALLOC(p, s) realloc(p, s)
#endif

int main() {
  __hwasan_enable_allocator_tagging();
  char *x = (char*)REALLOC(nullptr, 4);
  x[0] = 10;
  x[1] = 20;
  x[2] = 30;
  x[3] = 40;
  char *x1 = (char*)REALLOC(x, 5);
  assert(x1 != x);  // not necessary true for C,
                    // but true today for hwasan.
  assert(x1[0] == 10 && x1[1] == 20 && x1[2] == 30 && x1[3] == 40);
  x1[4] = 50;

  char *x2 = (char*)REALLOC(x1, 6);
  x2[5] = 60;
  assert(x2 != x1);
  assert(x2[0] == 10 && x2[1] == 20 && x2[2] == 30 && x2[3] == 40 &&
         x2[4] == 50 && x2[5] == 60);

  char *x3 = (char*)REALLOC(x2, 6);
  assert(x3 != x2);
  assert(x3[0] == 10 && x3[1] == 20 && x3[2] == 30 && x3[3] == 40 &&
         x3[4] == 50 && x3[5] == 60);

  char *x4 = (char*)REALLOC(x3, 5);
  assert(x4 != x3);
  assert(x4[0] == 10 && x4[1] == 20 && x4[2] == 30 && x4[3] == 40 &&
         x4[4] == 50);
  free(x4);
}