File: pdb-realloc2.c

package info (click to toggle)
valgrind 1%3A3.14.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 156,980 kB
  • sloc: ansic: 728,128; exp: 26,134; xml: 22,268; cpp: 7,638; asm: 7,312; makefile: 6,102; perl: 5,910; sh: 5,717
file content (111 lines) | stat: -rw-r--r-- 2,194 bytes parent folder | download | duplicates (11)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

/* A test which involves copying (using realloc) a block containing
   some partially defined bytes.  Really this is to check that
   copy_address_range_perms in mc_main.c works.  I don't think it's a
   good test - it may well not exercise all the code in
   copy_address_range_perms. */

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "memcheck/memcheck.h"

typedef unsigned char UChar;
typedef unsigned int  UInt;


static UInt seed = 0;
static inline UInt myrand ( UInt size )
{
   /* From "Numerical Recipes in C" 2nd Edition */
   seed = 1664525UL * seed + 1013904223UL;
   return seed % size;
}

static void barf ( int size, int offset )
{
   printf("pdb-realloc2: fail: size %d, offset %d\n", size,offset);
   exit(1);
}

void do_test ( int size )
{
   int i,j,r;
   UChar* v;
   UChar* p = malloc(size);
   assert(p);
   // fill
   seed = 0;
   for (i = 0; i < size; i++) {

      j = myrand( 256 * 25 );
      //printf("%d\n", j);
      if (j >= 256 * 13) {
         // def 1s
         p[i] = 0xFF;
      } else 
      if (j >= 256 && j < 256*13) {
         // def 0s
         p[i] = 0;
      } else {
         // pdb
         p[i] &= (UChar)j;
      }

   }

   // copy
   for (i = 1; i <= 100; i++) {
      p = realloc(p, size+i);
      assert(p);
   }

   // check
   v = malloc(size+100);
   assert(v);
   r = VALGRIND_GET_VBITS(p,v, size+100);
   assert(r == 1);

   //for (i = 0; i < size+100; i++)
   //  printf("%02x ", (UInt)v[i]);
   //printf("\n");

   seed = 0;
   for (i = 0; i < size; i++) {

      j = myrand( 256 * 25 );

      if (j >= 256) {
         // expecting a defined value
         if (v[i] != 0)
            barf(size, i);
      } else {
         // expecting a PDB == j
         if (v[i] != (UChar)j)
            barf(size,i);
      }

   }

   // in the extension area, everything should be undefined
   for (i = 0; i < 100; i++) {
      if (v[size+i] != 0xFF)
         barf(size, i);
   }

   free(v);
   free(p);
}

int main ( void )
{
  int z;
  for (z = 0; z < 100; z++) {
     printf("pdb_realloc: z = %d\n", z);
     do_test(z);
     do_test(z + 173);
     do_test(z + 1731);
  }
  printf("pdb-realloc2: done\n");
  return 0;
}