|
1 |
| -void workFunction_0(char *s) { |
| 1 | +char * strncat(char*, const char*, unsigned); |
| 2 | +unsigned strlen(const char*); |
| 3 | +void* malloc(unsigned); |
| 4 | + |
| 5 | +void strncat_test1(char *s) { |
2 | 6 | char buf[80];
|
3 |
| - strncat(buf, s, sizeof(buf)-strlen(buf)-1); // GOOD |
4 |
| - strncat(buf, s, sizeof(buf)-strlen(buf)); // BAD |
5 |
| - strncat(buf, "fix", sizeof(buf)-strlen(buf)); // BAD [NOT DETECTED] |
| 7 | + strncat(buf, s, sizeof(buf) - strlen(buf) - 1); // GOOD |
| 8 | + strncat(buf, s, sizeof(buf) - strlen(buf)); // BAD |
| 9 | + strncat(buf, "fix", sizeof(buf)-strlen(buf)); // BAD |
6 | 10 | }
|
7 |
| -void workFunction_1(char *s) { |
| 11 | + |
8 | 12 | #define MAX_SIZE 80
|
| 13 | + |
| 14 | +void strncat_test2(char *s) { |
9 | 15 | char buf[MAX_SIZE];
|
10 |
| - strncat(buf, s, MAX_SIZE-strlen(buf)-1); // GOOD |
11 |
| - strncat(buf, s, MAX_SIZE-strlen(buf)); // BAD |
12 |
| - strncat(buf, "fix", MAX_SIZE-strlen(buf)); // BAD [NOT DETECTED] |
| 16 | + strncat(buf, s, MAX_SIZE - strlen(buf) - 1); // GOOD |
| 17 | + strncat(buf, s, MAX_SIZE - strlen(buf)); // BAD |
| 18 | + strncat(buf, "fix", MAX_SIZE - strlen(buf)); // BAD |
13 | 19 | }
|
14 |
| -void workFunction_2_0(char *s) { |
15 |
| - char * buf; |
16 |
| - int len=80; |
17 |
| - buf = (char *) malloc(len); |
18 |
| - strncat(buf, s, len-strlen(buf)-1); // GOOD |
19 |
| - strncat(buf, s, len-strlen(buf)); // BAD |
20 |
| - strncat(buf, "fix", len-strlen(buf)); // BAD [NOT DETECTED] |
| 20 | + |
| 21 | +void strncat_test3(char *s) { |
| 22 | + int len = 80; |
| 23 | + char* buf = (char *) malloc(len); |
| 24 | + strncat(buf, s, len - strlen(buf) - 1); // GOOD |
| 25 | + strncat(buf, s, len - strlen(buf)); // BAD [NOT DETECTED] |
| 26 | + strncat(buf, "fix", len - strlen(buf)); // BAD [NOT DETECTED] |
21 | 27 | }
|
22 |
| -void workFunction_2_1(char *s) { |
23 |
| - char * buf; |
24 |
| - int len=80; |
25 |
| - buf = (char *) malloc(len+1); |
26 |
| - strncat(buf, s, len-strlen(buf)-1); // GOOD |
27 |
| - strncat(buf, s, len-strlen(buf)); // GOOD |
| 28 | + |
| 29 | +void strncat_test4(char *s) { |
| 30 | + int len = 80; |
| 31 | + char* buf = (char *) malloc(len + 1); |
| 32 | + strncat(buf, s, len - strlen(buf) - 1); // GOOD |
| 33 | + strncat(buf, s, len - strlen(buf)); // GOOD |
28 | 34 | }
|
29 | 35 |
|
30 | 36 | struct buffers
|
31 | 37 | {
|
32 |
| - unsigned char buff1[50]; |
33 |
| - unsigned char *buff2; |
| 38 | + unsigned char array[50]; |
| 39 | + unsigned char *pointer; |
34 | 40 | } globalBuff1,*globalBuff2,globalBuff1_c,*globalBuff2_c;
|
35 | 41 |
|
| 42 | +void strncat_test5(char* s, struct buffers* buffers) { |
| 43 | + unsigned len_array = strlen(buffers->array); |
| 44 | + unsigned max_size = sizeof(buffers->array); |
| 45 | + unsigned free_size = max_size - len_array; |
| 46 | + strncat(buffers->array, s, free_size); // BAD |
| 47 | +} |
36 | 48 |
|
37 |
| -void badFunc0(){ |
| 49 | +void strlen_test1(){ |
38 | 50 | unsigned char buff1[12];
|
39 | 51 | struct buffers buffAll;
|
40 | 52 | struct buffers * buffAll1;
|
41 | 53 |
|
42 | 54 | buff1[strlen(buff1)]=0; // BAD
|
43 |
| - buffAll.buff1[strlen(buffAll.buff1)]=0; // BAD |
44 |
| - buffAll.buff2[strlen(buffAll.buff2)]=0; // BAD |
45 |
| - buffAll1->buff1[strlen(buffAll1->buff1)]=0; // BAD |
46 |
| - buffAll1->buff2[strlen(buffAll1->buff2)]=0; // BAD |
47 |
| - globalBuff1.buff1[strlen(globalBuff1.buff1)]=0; // BAD |
48 |
| - globalBuff1.buff2[strlen(globalBuff1.buff2)]=0; // BAD |
49 |
| - globalBuff2->buff1[strlen(globalBuff2->buff1)]=0; // BAD |
50 |
| - globalBuff2->buff2[strlen(globalBuff2->buff2)]=0; // BAD |
| 55 | + buffAll.array[strlen(buffAll.array)]=0; // BAD |
| 56 | + buffAll.pointer[strlen(buffAll.pointer)]=0; // BAD |
| 57 | + buffAll1->array[strlen(buffAll1->array)]=0; // BAD |
| 58 | + buffAll1->pointer[strlen(buffAll1->pointer)]=0; // BAD |
| 59 | + globalBuff1.array[strlen(globalBuff1.array)]=0; // BAD |
| 60 | + globalBuff1.pointer[strlen(globalBuff1.pointer)]=0; // BAD |
| 61 | + globalBuff2->array[strlen(globalBuff2->array)]=0; // BAD |
| 62 | + globalBuff2->pointer[strlen(globalBuff2->pointer)]=0; // BAD |
51 | 63 | }
|
52 |
| -void noBadFunc0(){ |
| 64 | + |
| 65 | +void strlen_test2(){ |
53 | 66 | unsigned char buff1[12],buff1_c[12];
|
54 | 67 | struct buffers buffAll,buffAll_c;
|
55 | 68 | struct buffers * buffAll1,*buffAll1_c;
|
56 | 69 |
|
57 | 70 | buff1[strlen(buff1_c)]=0; // GOOD
|
58 |
| - buffAll.buff1[strlen(buffAll_c.buff1)]=0; // GOOD |
59 |
| - buffAll.buff2[strlen(buffAll.buff1)]=0; // GOOD |
60 |
| - buffAll1->buff1[strlen(buffAll1_c->buff1)]=0; // GOOD |
61 |
| - buffAll1->buff2[strlen(buffAll1->buff1)]=0; // GOOD |
62 |
| - globalBuff1.buff1[strlen(globalBuff1_c.buff1)]=0; // GOOD |
63 |
| - globalBuff1.buff2[strlen(globalBuff1.buff1)]=0; // GOOD |
64 |
| - globalBuff2->buff1[strlen(globalBuff2_c->buff1)]=0; // GOOD |
65 |
| - globalBuff2->buff2[strlen(globalBuff2->buff1)]=0; // GOOD |
| 71 | + buffAll.array[strlen(buffAll_c.array)]=0; // GOOD |
| 72 | + buffAll.pointer[strlen(buffAll.array)]=0; // GOOD |
| 73 | + buffAll1->array[strlen(buffAll1_c->array)]=0; // GOOD |
| 74 | + buffAll1->pointer[strlen(buffAll1->array)]=0; // GOOD |
| 75 | + globalBuff1.array[strlen(globalBuff1_c.array)]=0; // GOOD |
| 76 | + globalBuff1.pointer[strlen(globalBuff1.array)]=0; // GOOD |
| 77 | + globalBuff2->array[strlen(globalBuff2_c->array)]=0; // GOOD |
| 78 | + globalBuff2->pointer[strlen(globalBuff2->array)]=0; // GOOD |
66 | 79 | }
|
67 |
| -void goodFunc0(){ |
| 80 | + |
| 81 | +void strlen_test3(){ |
68 | 82 | unsigned char buffer[12];
|
69 | 83 | int i;
|
70 | 84 | for(i = 0; i < 6; i++)
|
|
0 commit comments