|
1 | 1 | typedef unsigned long size_t;
|
2 | 2 | void *malloc(size_t size);
|
3 | 3 | void free(void *ptr);
|
| 4 | +#define NULL 0 |
4 | 5 |
|
5 | 6 | void workFunction_0(char *s) {
|
6 | 7 | int intSize = 10;
|
7 | 8 | char *buf;
|
8 | 9 | buf = (char *) malloc(intSize);
|
9 |
| - free(buf); // BAD |
10 |
| - if(buf) free(buf); |
| 10 | + free(buf); // GOOD |
| 11 | + if(buf) free(buf); // BAD |
11 | 12 | }
|
12 | 13 | void workFunction_1(char *s) {
|
| 14 | + int intSize = 10; |
| 15 | + char *buf; |
| 16 | + buf = (char *) malloc(intSize); |
| 17 | + free(buf); // GOOD |
| 18 | + free(buf); // BAD |
| 19 | +} |
| 20 | +void workFunction_2(char *s) { |
| 21 | + int intSize = 10; |
| 22 | + char *buf; |
| 23 | + buf = (char *) malloc(intSize); |
| 24 | + free(buf); // GOOD |
| 25 | + buf = NULL; |
| 26 | + free(buf); |
| 27 | +} |
| 28 | +void workFunction_3(char *s) { |
| 29 | + int intSize = 10; |
| 30 | + char *buf; |
| 31 | + int intFlag; |
| 32 | + buf = (char *) malloc(intSize); |
| 33 | + if(buf[1]%5) { |
| 34 | + free(buf); |
| 35 | + buf = NULL; |
| 36 | + } |
| 37 | + free(buf); |
| 38 | +} |
| 39 | +void workFunction_4(char *s) { |
| 40 | + int intSize = 10; |
| 41 | + char *buf; |
| 42 | + char *tmpbuf; |
| 43 | + tmpbuf = (char *) malloc(intSize); |
| 44 | + buf = (char *) malloc(intSize); |
| 45 | + free(buf); // GOOD |
| 46 | + buf = tmpbuf; |
| 47 | + free(buf); // GOOD |
| 48 | +} |
| 49 | +void workFunction_5(char *s) { |
| 50 | + int intSize = 10; |
| 51 | + char *buf; |
| 52 | + int intFlag; |
| 53 | + buf = (char *) malloc(intSize); |
| 54 | + if(intFlag) { |
| 55 | + free(buf); |
| 56 | + } |
| 57 | + free(buf); // BAD |
| 58 | +} |
| 59 | +void workFunction_6(char *s) { |
| 60 | + int intSize = 10; |
| 61 | + char *buf; |
| 62 | + char *tmpbuf; |
| 63 | + int intFlag; |
| 64 | + tmpbuf = (char *) malloc(intSize); |
| 65 | + buf = (char *) malloc(intSize); |
| 66 | + if(intFlag) { |
| 67 | + free(buf); |
| 68 | + buf = tmpbuf; |
| 69 | + } |
| 70 | + free(buf); |
| 71 | +} |
| 72 | +void workFunction_7(char *s) { |
| 73 | + int intSize = 10; |
| 74 | + char *buf; |
| 75 | + char *buf1; |
| 76 | + buf = (char *) malloc(intSize); |
| 77 | + buf1 = (char *) realloc(buf,intSize*4); |
| 78 | + free(buf); // BAD |
| 79 | +} |
| 80 | +void workFunction_8(char *s) { |
| 81 | + int intSize = 10; |
| 82 | + char *buf; |
| 83 | + char *buf1; |
| 84 | + buf = (char *) malloc(intSize); |
| 85 | + buf1 = (char *) realloc(buf,intSize*4); |
| 86 | + if(!buf1) |
| 87 | + free(buf); // GOOD |
| 88 | +} |
| 89 | +void workFunction_9(char *s) { |
13 | 90 | int intSize = 10;
|
14 | 91 | char *buf;
|
15 | 92 | char *buf1;
|
16 | 93 | buf = (char *) malloc(intSize);
|
17 |
| - buf1 = (char *) realloc(buf,intSize*2); |
18 |
| - if(buf) free(buf); // GOOD |
19 |
| - buf = (char *) realloc(buf1,intSize*4); // BAD |
20 |
| - free(buf1); |
| 94 | + if(!(buf1 = (char *) realloc(buf,intSize*4))) |
| 95 | + free(buf); // GOOD |
21 | 96 | }
|
0 commit comments