File: overlap.c

package info (click to toggle)
valgrind 1%3A3.12.0~svn20160714-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 120,428 kB
  • ctags: 70,855
  • sloc: ansic: 674,645; exp: 26,134; xml: 21,574; asm: 7,570; cpp: 7,567; makefile: 7,380; sh: 6,188; perl: 5,855; haskell: 195
file content (125 lines) | stat: -rw-r--r-- 2,373 bytes parent folder | download | duplicates (12)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <string.h>
#include <stdio.h>

char b[50];

void reset_b(void)
{
   int i;

   for (i = 0; i < 50; i++)
      b[i] = '_';
   b[49] = '\0';
}

void reset_b2(void)
{
   reset_b();
   strcpy(b, "ABCDEFG");
}

int main(void)
{
   char x[100];
   char a[] = "abcdefghijklmnopqrstuvwxyz";
   int  i;

   /* testing memcpy/strcpy overlap */

   for (i = 0; i < 50; i++) {
      x[i] = i+1;    // don't put any zeroes in there
   }
   for (i = 50; i < 100; i++) {
      // because of the errors, the strcpy's will overrun, so put some
      // zeroes in the second half to stop them eventually
      x[i] = 0;  
               
   }

   memcpy(x+20, x, 20);    // ok
   memcpy(x+20, x, 21);    // overlap
   memcpy(x, x+20, 20);    // ok
   memcpy(x, x+20, 21);    // overlap

   strncpy(x+20, x, 20);    // ok
   strncpy(x+20, x, 21);    // overlap
   strncpy(x, x+20, 20);    // ok
   strncpy(x, x+20, 21);    // overlap
   
   x[39] = '\0';
   strcpy(x, x+20);    // ok

   x[39] = 39;
   x[40] = '\0';
   strcpy(x, x+20);    // overlap

   x[19] = '\0';
   strcpy(x+20, x);    // ok

/*
   x[19] = 19;
   x[20] = '\0';
   strcpy(x+20, x);    // overlap, but runs forever (or until it seg faults)
*/

   /* testing strcpy, strncpy() */

   reset_b();
   printf("`%s'\n", b);

   strcpy(b, a);
   printf("`%s'\n", b);
   
   reset_b();
   strncpy(b, a, 25);
   printf("`%s'\n", b);

   reset_b();
   strncpy(b, a, 26);
   printf("`%s'\n", b);

   reset_b();
   strncpy(b, a, 27);
   printf("`%s'\n", b);

   printf("\n");

   /* testing strncat() */

   reset_b2();
   printf("`%s'\n", b);
   
   reset_b2();
   strcat(b, a);
   printf("`%s'\n", b);
   
   reset_b2();
   strncat(b, a, 25);
   printf("`%s'\n", b);
   
   reset_b2();
   strncat(b, a, 26);
   printf("`%s'\n", b);
   
   reset_b2();
   strncat(b, a, 27);
   printf("`%s'\n", b);

   /* Nb: can't actually get strcat warning -- if any overlap occurs, it will
      always run forever, I think... */

   for ( i = 0; i < 2; i++) 
      strncat(a+20, a, 21);    // run twice to check 2nd error isn't shown
   strncat(a, a+20, 21);

   /* This is ok, but once gave a warning when strncpy() was wrong,
      and used 'n' for the length, even when the src was shorter than 'n' */
   {
      char dest[64];
      char src [16];
      strcpy( src, "short" );
      strncpy( dest, src, 20 );
   }

   return 0;
}