-
Notifications
You must be signed in to change notification settings - Fork 64
/
free_null_pointer.c
639 lines (585 loc) · 12.9 KB
/
free_null_pointer.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/********Software Analysis - FY2013*************/
/*
* File Name: free_null_pointer.c
* Defect Classification
* ---------------------
* Defect Type: Pointer related defects
* Defect Sub-type: Free NULL pointer
*
*/
#include "HeaderFile.h"
int *free_null_pointer_002_gbl_ptr = NULL;
char **free_null_pointer_010_gbl_dst=NULL;
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a pointer to char
*/
void free_null_pointer_001 ()
{
char* buf= NULL;
free(buf);/* Tool should detect this line as error */ /*ERROR:Freeing a NULL pointer*/
buf = NULL;
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a pointer to int that is global file scope , inside a if statement
*/
void free_null_pointer_002 ()
{
int a=20;
if(a>0)
{
free(free_null_pointer_002_gbl_ptr);/* Tool should detect this line as error *//*ERROR:Freeing a NULL pointer*/
free_null_pointer_002_gbl_ptr = NULL;
}
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a pointer to long in an infinite for loop , memory allocated and assigned inside a if condition
*/
void free_null_pointer_003 ()
{
int i;
long *buf=NULL;
for (i=0;;i++)
{
if(i>=10)
{
buf=(long*) calloc(5,sizeof(long));
buf[0]=i;
}
free (buf);/*ERROR:Freeing a NULL pointer*/
buf = NULL;
if(i>=10)
break;
}
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a double pointer to float , memory allocated in an do - while loop
*/
void free_null_pointer_004 ()
{
float **fptr = (float**) malloc(5*sizeof(float*));
int i=0,j=0;
do
{
fptr[i]=NULL;
i++;
}while (i<5);
i=0;
do
{
for(j=0;j<5;j++)
{
/**(*(fptr+i)+j)=i+0.5;*/
}
free(fptr[i]); /* Tool should detect this line as error *//*ERROR:Freeing a NULL pointer*/
i++;
}while (i<5);
free(fptr);
}
/*
* Types of defects: Freeing a NULL pointer
* Memory allocated in a function and Memory used in another function
*/
# define INDEX 'a'
static unsigned char a =INDEX;
char * free_null_pointer_005_gbl_ptr;
void free_null_pointer_005_func_001 (int len)
{
free_null_pointer_005_gbl_ptr=NULL;
if(a != INDEX)
free_null_pointer_005_gbl_ptr= malloc(sizeof(char) * (len+1));
}
void free_null_pointer_005 ()
{
char *str = "This is a string";
free_null_pointer_005_func_001(strlen(str));
strcpy(free_null_pointer_005_gbl_ptr,str);
free(free_null_pointer_005_gbl_ptr); /* Tool should detect this line as error *//*ERROR:Freeing a NULL pointer*/
free_null_pointer_005_gbl_ptr = NULL;
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a pointer to structure ,memory allocated based on function return value
*/
int free_null_pointer_006_func_001(int flag)
{
int ret;
if (flag ==0)
ret = 0;
else
ret=1;
return ret;
}
typedef struct {
int a;
int b;
char *buf;
} free_null_pointer_006_s_001;
void free_null_pointer_006 ()
{
int flag=0,i;
char *s1="This is a string";
free_null_pointer_006_s_001* s=(free_null_pointer_006_s_001*) calloc(5,sizeof(free_null_pointer_006_s_001)) ;
if(free_null_pointer_006_func_001(flag)==0)
{
for(i= 0; i<5 ;i++)
{
(s+i)->buf = NULL;
}
strcpy((s+4)->buf,s1);
}
if(free_null_pointer_006_func_001(flag)==0)
{
for(i= 0; i<5 ;i++)
free((s+i)->buf);/* Tool should detect this line as error *//*ERROR:Freeing a NULL pointer*/
}
free(s);
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a double pointer to long , memory allocated in another function inside goto label and if condition
*/
long ** free_null_pointer_007_gbl_doubleptr;
int free_null_pointer_007_func_001(int flag)
{
int ret ;
if (flag ==0)
ret = 0;
else
ret=1;
return ret;
}
void free_null_pointer_007_func_002()
{
int i,j;
free_null_pointer_007_gbl_doubleptr=(long**) malloc(10*sizeof(long*));
for(i=0;i<10;i++)
{
free_null_pointer_007_gbl_doubleptr[i]=(long*) malloc(10*sizeof(long));
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
free_null_pointer_007_gbl_doubleptr[i][j]=i;
}
}
}
#define ZERO 0
void free_null_pointer_007()
{
int flag=0,i,j;
free_null_pointer_007_gbl_doubleptr=NULL;
goto label;
if(free_null_pointer_007_func_001(flag)!=ZERO)
{
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
free_null_pointer_007_gbl_doubleptr[i][j] += 1;
}
free (free_null_pointer_007_gbl_doubleptr[i]);
free_null_pointer_007_gbl_doubleptr[i] = NULL;
}
}
else
{
free(free_null_pointer_007_gbl_doubleptr);/* Tool should detect this line as error *//*ERROR:Freeing a NULL pointer*/
free_null_pointer_007_gbl_doubleptr = NULL;
}
label:
if(free_null_pointer_007_func_001(flag)!=ZERO)
{
free_null_pointer_007_func_002();
}
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a double pointer to char , and initialized in another function
*/
enum {min_buffer = 0 , max_buffer = 5 };
static unsigned int min=min_buffer+2;
static unsigned int max=max_buffer+2;
void free_null_pointer_008_func_001 (int len,char **stringPtr)
{
char * p = NULL;
if(min <= min_buffer && max <= max_buffer)
{
p = malloc(sizeof(char) * (len+1));
*stringPtr = p;
}
}
void free_null_pointer_008 ()
{
char *str = "This is a string";
char *str1=NULL;
free_null_pointer_008_func_001(strlen(str),&str1);
strcpy(str1,str);
free(str1);/* Tool should detect this line as error *//*ERROR:Freeing a NULL pointer*/
str1 = NULL;
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a double pointer to char in an infinite while loop
*/
/*Allocate Memory */
char **free_null_pointer_009dst;
void free_null_pointer_009_func_001()
{
free_null_pointer_009dst = NULL;
int i;
{
while(0)
{
free_null_pointer_009dst = (char**) malloc(5*sizeof(char*));
for(i=0;i<5;i++)
{
free_null_pointer_009dst[i]=(char*) malloc(15*sizeof(char));
}
break;
}
}
}
void free_null_pointer_009 ()
{
int i;
free_null_pointer_009_func_001();
for(i=0;i<5;i++)
{
strcpy (free_null_pointer_009dst[i],"STRING");
}
while(1)
{
for(i=0;i<5;i++)
{
free(free_null_pointer_009dst[i]);
free_null_pointer_009dst[i] = NULL;
}
break;
}
while(1)
{
free(free_null_pointer_009dst);/* Tool should detect this line as error *//*ERROR:Freeing a NULL pointer*/
free_null_pointer_009dst = NULL;
break;
}
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a double pointer to char in an infinite while loop,memory allocated in one function ,
* set in another function and freed in another function
*/
#define SET_MEM 0
void free_null_pointer_010_func_001()
{
int i;
{
while(SET_MEM)
{
free_null_pointer_010_gbl_dst = (char**) malloc(5*sizeof(char*));
for(i=0;i<5;i++)
{
free_null_pointer_010_gbl_dst[i]=(char*) malloc(15*sizeof(char));
}
break;
}
}
}
void free_null_pointer_010_func_002(char **dst, char (*src)[15])
{
int i;
for(i=0;i<5;i++)
{
strcpy(*(dst+i),src[i]);
}
}
void free_null_pointer_010_func_003()
{
int i;
while(SET_MEM)
{
for(i=0;i<5;i++)
{
free(free_null_pointer_010_gbl_dst[i]);
free_null_pointer_010_gbl_dst[i] = NULL;
}
break;
}
while(SET_MEM == 0)
{
free(free_null_pointer_010_gbl_dst);
free_null_pointer_010_gbl_dst = NULL;/*ERROR:Freeing a NULL pointer*/
break;
}
}
void free_null_pointer_010 ()
{
char str2[][15] = {{"STRING"},
{"TEST"},{"STRING#"},{"TEST!"},{"TRIAL"}};
free_null_pointer_010_func_001();
free_null_pointer_010_func_002(free_null_pointer_010_gbl_dst,str2);
free_null_pointer_010_func_003();
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a pointer to union in an switch case and random function and static pointer to union
*/
typedef union {
int a;
int b;
} free_null_pointer_011_u_001;
static free_null_pointer_011_u_001 *u;
free_null_pointer_011_u_001 * free_null_pointer_011_func_001 ()
{
int flag = 0;
switch (flag)
{
case 1:
{
u = (free_null_pointer_011_u_001 *)calloc(1,sizeof(free_null_pointer_011_u_001));
u->a = 40;
return u;
}
case 2:
{
u = (free_null_pointer_011_u_001 *)calloc(2,sizeof(free_null_pointer_011_u_001));
u->a = 20;
return u;
}
case 3:
{
u = (free_null_pointer_011_u_001 *)calloc(3,sizeof(free_null_pointer_011_u_001));
u->a = 30;
return u;
}
default:
return (free_null_pointer_011_u_001 *)(-1);
}
}
free_null_pointer_011_u_001 * free_null_pointer_011_func_002 ()
{
int flag = rand();
switch (flag)
{
case 1:
{
free(u); /* Tool should detect this line as error *//*ERROR:Freeing a NULL pointer*/
u= NULL;
return u;
}
case 2:
{
return u;
}
case 3:
{
return u;
}
default:
return (free_null_pointer_011_u_001 *)(-1);
}
}
void free_null_pointer_011 ()
{
int ret;
free_null_pointer_011_u_001 *p;
p = free_null_pointer_011_func_001 ();
ret = p->b;
p = free_null_pointer_011_func_002 ();
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a pointer to double in an ternary conditional operator
*/
#define ZERO 0
#define MAX 1
int free_null_pointer_012_func_001(int flag)
{
int ret;
if (flag ==0)
ret = 0;
else
ret=1;
return ret;
}
void free_null_pointer_012 ()
{
double *ptr =NULL, a;
int flag=10;
if (free_null_pointer_012_func_001(1) == ZERO && MAX ==1)
{
(flag == 10)? (ptr= (double*) malloc(10*sizeof(double))) : ( ptr= NULL);
(flag == 10)? (*(ptr+1) = 10):(a=100);
}
if (free_null_pointer_012_func_001(0) == ZERO && MAX ==1)
{
if(flag == 10)
a = *(ptr+1);
}
if (free_null_pointer_012_func_001(0) == ZERO && MAX ==1)
{
if(flag == 10)
{
free(ptr); /* Tool should detect this line as error *//*ERROR:Freeing a NULL pointer*/
ptr = NULL;
}
}
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a pointer to double in an while loop, pointer aliasing
*/
void free_null_pointer_013 ()
{
double * fptr;
double **fp1 = &fptr;
double **fp2 = &fptr;
fptr = NULL;
int i=0;
do
{
double * fptr = *fp1;
if(i>=10 && i<=100)
{
fptr = (double *)calloc(10, sizeof(double));
}
*(fptr+3) = 50.5;
*fp1 = fptr;
i++;
}while(i>=0 && i<=1);
do
{
double * fptr = *fp2;
free(fptr);/* Tool should detect this line as error *//*ERROR:Freeing a NULL pointer*/
fptr = NULL;
}while(i>=0 && i<=1);
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: When using a pointer to int element in structure
*/
typedef struct {
double *p1;
double *p2;
double *p3;
}free_null_pointer_014_s_001 ;
free_null_pointer_014_s_001 *free_null_pointer_014_gbl_s=NULL;
void free_null_pointer_014_func_001(int flag)
{
int i;
if(flag ==1)
{
for (i=1;i<1;i++)
{
free_null_pointer_014_gbl_s = malloc(sizeof(free_null_pointer_014_s_001));
free_null_pointer_014_gbl_s->p1 = malloc(sizeof(double));
free_null_pointer_014_gbl_s->p2 = malloc(sizeof(double));
free_null_pointer_014_gbl_s->p3 = malloc(sizeof(double));
}
}
}
void free_null_pointer_014_func_002(int flag)
{
static double arr[3]={10.0,20.0,30.0};
int i;
if (flag ==1)
{
for (i=0;i<1;i++)
{
*(free_null_pointer_014_gbl_s->p1) = arr[0];
*(free_null_pointer_014_gbl_s->p2) = arr[1];
*(free_null_pointer_014_gbl_s->p3) = arr[2];
}
}
}
void free_null_pointer_014_func_003(int flag)
{
int i;
if (flag ==1)
{
for (i=1;i<1;i++)
{
free(free_null_pointer_014_gbl_s->p1);
free(free_null_pointer_014_gbl_s->p2);
free(free_null_pointer_014_gbl_s->p3);
}
for (i=0;i<1;i++)
{
free(free_null_pointer_014_gbl_s);/* Tool should detect this line as error *//*ERROR:Freeing a NULL pointer*/
}
}
}
void free_null_pointer_014 ()
{
free_null_pointer_014_func_001(1);
free_null_pointer_014_func_002(1);
free_null_pointer_014_func_003(1);
}
/*
* Types of defects: Freeing a NULL pointer
* Complexity: Free Null Pointer main function
*/
extern volatile int vflag;
void free_null_pointer_main ()
{
if (vflag == 1 || vflag ==888)
{
free_null_pointer_001();
}
if (vflag == 2 || vflag ==888)
{
free_null_pointer_002();
}
if (vflag == 3 || vflag ==888)
{
free_null_pointer_003();
}
if (vflag == 4 || vflag ==888)
{
free_null_pointer_004();
}
if (vflag == 5 || vflag ==888)
{
free_null_pointer_005();
}
if (vflag == 6 || vflag ==888)
{
free_null_pointer_006();
}
if (vflag == 7 || vflag ==888)
{
free_null_pointer_007();
}
if (vflag == 8 || vflag ==888)
{
free_null_pointer_008();
}
if (vflag == 9 || vflag ==888)
{
free_null_pointer_009();
}
if (vflag == 10 || vflag ==888)
{
free_null_pointer_010();
}
if (vflag == 11 || vflag ==888)
{
free_null_pointer_011();
}
if (vflag == 12 || vflag ==888)
{
free_null_pointer_012();
}
if (vflag == 13 || vflag ==888)
{
free_null_pointer_013();
}
if (vflag == 14 || vflag ==888)
{
free_null_pointer_014();
}
}