Skip to content

Commit 883ecca

Browse files
author
Nikita Kraiouchkine
committed
Update RULE-21-17 and RULE-21-18 tests
1 parent 406dab5 commit 883ecca

File tree

4 files changed

+150
-7
lines changed

4 files changed

+150
-7
lines changed
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
No expected results have yet been specified
1+
| test.c:31:5:31:10 | call to strcat | The $@ passed to strcat might not be null-terminated. | test.c:31:12:31:15 | buf1 | argument | test.c:31:12:31:15 | buf1 | |
2+
| test.c:36:5:36:10 | call to strcat | The size of the $@ passed to strcat is 6 bytes, but the size of the $@ is only 5 bytes. | test.c:36:24:36:30 | 12345 | read buffer | test.c:36:12:36:19 | call to get_ca_5 | write buffer |
3+
| test.c:38:5:38:10 | call to strcat | The size of the $@ passed to strcat is 5 bytes, but the size of the $@ is only 4 bytes. | test.c:38:28:38:33 | 1234 | read buffer | test.c:38:12:38:25 | ... + ... | write buffer |
4+
| test.c:43:5:43:10 | call to strchr | The $@ passed to strchr might not be null-terminated. | test.c:43:12:43:18 | ca5_bad | argument | test.c:43:12:43:18 | ca5_bad | |
5+
| test.c:45:5:45:10 | call to strchr | The $@ passed to strchr is 5 bytes, but an offset of 5 bytes is used to access it. | test.c:45:12:45:23 | ... + ... | read buffer | test.c:45:12:45:23 | ... + ... | |
6+
| test.c:47:5:47:11 | call to strrchr | The $@ passed to strrchr might not be null-terminated. | test.c:47:13:47:19 | ca5_bad | argument | test.c:47:13:47:19 | ca5_bad | |
7+
| test.c:49:5:49:11 | call to strrchr | The $@ passed to strrchr is 5 bytes, but an offset of 5 bytes is used to access it. | test.c:49:13:49:24 | ... + ... | read buffer | test.c:49:13:49:24 | ... + ... | |
8+
| test.c:53:5:53:10 | call to strcmp | The $@ passed to strcmp might not be null-terminated. | test.c:53:22:53:28 | ca5_bad | argument | test.c:53:22:53:28 | ca5_bad | |
9+
| test.c:55:5:55:10 | call to strcmp | The $@ passed to strcmp might not be null-terminated. | test.c:55:12:55:18 | ca5_bad | argument | test.c:55:12:55:18 | ca5_bad | |
10+
| test.c:58:5:58:11 | call to strcoll | The $@ passed to strcoll might not be null-terminated. | test.c:58:23:58:29 | ca5_bad | argument | test.c:58:23:58:29 | ca5_bad | |
11+
| test.c:60:5:60:11 | call to strcoll | The $@ passed to strcoll might not be null-terminated. | test.c:60:13:60:19 | ca5_bad | argument | test.c:60:13:60:19 | ca5_bad | |
12+
| test.c:66:5:66:10 | call to strcpy | The size of the $@ passed to strcpy is 6 bytes, but the size of the $@ is only 5 bytes. | test.c:66:22:66:28 | test1 | read buffer | test.c:66:12:66:19 | ca5_good | write buffer |
13+
| test.c:70:5:70:10 | call to strcpy | The $@ passed to strcpy might not be null-terminated. | test.c:70:24:70:30 | ca5_bad | argument | test.c:70:24:70:30 | ca5_bad | |
14+
| test.c:71:5:71:10 | call to strcpy | The size of the $@ passed to strcpy is 6 bytes, but the size of the $@ is only 5 bytes. | test.c:71:24:71:31 | ca6_good | read buffer | test.c:71:12:71:19 | call to get_ca_5 | write buffer |
15+
| test.c:76:5:76:11 | call to strcspn | The $@ passed to strcspn might not be null-terminated. | test.c:76:13:76:19 | ca5_bad | argument | test.c:76:13:76:19 | ca5_bad | |
16+
| test.c:78:5:78:11 | call to strcspn | The $@ passed to strcspn is null. | test.c:78:13:78:16 | 0 | argument | test.c:78:13:78:16 | 0 | |
17+
| test.c:80:5:80:10 | call to strspn | The $@ passed to strspn might not be null-terminated. | test.c:80:12:80:18 | ca5_bad | argument | test.c:80:12:80:18 | ca5_bad | |
18+
| test.c:82:5:82:10 | call to strspn | The $@ passed to strspn is null. | test.c:82:12:82:15 | 0 | argument | test.c:82:12:82:15 | 0 | |
19+
| test.c:86:5:86:10 | call to strlen | The $@ passed to strlen might not be null-terminated. | test.c:86:12:86:18 | ca5_bad | argument | test.c:86:12:86:18 | ca5_bad | |
20+
| test.c:88:5:88:10 | call to strlen | The $@ passed to strlen is 5 bytes, but an offset of 5 bytes is used to access it. | test.c:88:12:88:23 | ... + ... | read buffer | test.c:88:12:88:23 | ... + ... | |
21+
| test.c:93:5:93:11 | call to strpbrk | The $@ passed to strpbrk might not be null-terminated. | test.c:93:13:93:19 | ca5_bad | argument | test.c:93:13:93:19 | ca5_bad | |
22+
| test.c:95:5:95:11 | call to strpbrk | The $@ passed to strpbrk is null. | test.c:95:13:95:16 | 0 | argument | test.c:95:13:95:16 | 0 | |
23+
| test.c:102:5:102:10 | call to strstr | The $@ passed to strstr might not be null-terminated. | test.c:102:12:102:18 | ca5_bad | argument | test.c:102:12:102:18 | ca5_bad | |
24+
| test.c:111:5:111:10 | call to strtok | The $@ passed to strtok is null. | test.c:111:18:111:21 | 0 | argument | test.c:111:18:111:21 | 0 | |
25+
| test.c:113:5:113:10 | call to strtok | The $@ passed to strtok might not be null-terminated. | test.c:113:12:113:18 | ca5_bad | argument | test.c:113:12:113:18 | ca5_bad | |
26+
| test.c:117:5:117:10 | call to strtok | The $@ passed to strtok might not be null-terminated. | test.c:117:22:117:28 | ca6_bad | argument | test.c:117:22:117:28 | ca6_bad | |

c/misra/test/rules/RULE-21-17/test.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// test partially copied from CERT-C ARR38-C test
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
char *get_ca_5(void) {
6+
void *ptr = malloc(5 * sizeof(char));
7+
memset(ptr, 0, 5 * sizeof(char));
8+
return (char *)ptr;
9+
}
10+
11+
void test(void) {
12+
char ca5_good[5] = "test"; // ok
13+
char ca5_bad[5] = "test1"; // no null terminator
14+
char ca6_good[6] = "test1"; // ok
15+
char ca6_bad[6] = "test12"; // no null terminator
16+
17+
// strcat
18+
{
19+
char buf0[10]; // memset after first use
20+
char buf1[10]; // no memset
21+
char buf2[10]; // memset before first use
22+
char buf3[10] = {'\0'};
23+
char buf4[10] = "12345";
24+
25+
strcat(buf0, " "); // NON_COMPLIANT[FALSE_NEGATIVE] - not null terminated at
26+
// initialization
27+
28+
memset(buf0, 0, sizeof(buf0)); // COMPLIANT
29+
memset(buf2, 0, sizeof(buf2)); // COMPLIANT
30+
31+
strcat(buf1, " "); // NON_COMPLIANT - not null terminated
32+
strcat(buf2, " "); // COMPLIANT
33+
strcat(buf3, " "); // COMPLIANT
34+
strcat(buf4, "12345"); // NON_COMPLIANT[FALSE_NEGATIVE]
35+
36+
strcat(get_ca_5(), "12345"); // NON_COMPLIANT
37+
strcat(get_ca_5(), "1234"); // COMPLIANT
38+
strcat(get_ca_5() + 1, "1234"); // NON_COMPLIANT
39+
}
40+
// strchr and strrchr
41+
{
42+
strchr(ca5_good, 't'); // COMPLIANT
43+
strchr(ca5_bad, 't'); // NON_COMPLIANT
44+
strchr(ca5_good + 4, 't'); // COMPLIANT
45+
strchr(ca5_good + 5, 't'); // NON_COMPLIANT
46+
strrchr(ca5_good, 1); // COMPLIANT
47+
strrchr(ca5_bad, 1); // NON_COMPLIANT
48+
strrchr(ca5_good + 4, 1); // COMPLIANT
49+
strrchr(ca5_good + 5, 1); // NON_COMPLIANT
50+
}
51+
// strcmp and strcoll
52+
{
53+
strcmp(ca5_good, ca5_bad); // NON_COMPLIANT
54+
strcmp(ca5_good, ca5_good); // COMPLIANT
55+
strcmp(ca5_bad, ca5_good); // NON_COMPLIANT
56+
strcmp(ca5_good, ca6_good); // COMPLIANT
57+
strcmp(ca6_good, ca5_good); // COMPLIANT
58+
strcoll(ca5_good, ca5_bad); // NON_COMPLIANT
59+
strcoll(ca5_good, ca5_good); // COMPLIANT
60+
strcoll(ca5_bad, ca5_good); // NON_COMPLIANT
61+
strcoll(ca5_good, ca6_good); // COMPLIANT
62+
strcoll(ca6_good, ca5_good); // COMPLIANT
63+
}
64+
// strcpy
65+
{
66+
strcpy(ca5_good, "test1"); // NON_COMPLIANT
67+
strcpy(ca5_bad, "test"); // COMPLIANT
68+
// strcpy to char buffer indirect
69+
strcpy(get_ca_5(), ca5_good); // COMPLIANT
70+
strcpy(get_ca_5(), ca5_bad); // NON_COMPLIANT
71+
strcpy(get_ca_5(), ca6_good); // NON_COMPLIANT
72+
}
73+
// strcspn and strspn
74+
{
75+
strcspn(ca5_good, "test"); // COMPLIANT
76+
strcspn(ca5_bad, "test"); // NON_COMPLIANT - not null-terminated
77+
strcspn(ca5_good, "1234567890"); // COMPLIANT
78+
strcspn(NULL, "12345"); // NON_COMPLIANT
79+
strspn(ca5_good, "test"); // COMPLIANT
80+
strspn(ca5_bad, "test"); // NON_COMPLIANT - not null-terminated
81+
strspn(ca5_good, "1234567890"); // COMPLIANT
82+
strspn(NULL, "12345"); // NON_COMPLIANT
83+
}
84+
// strlen
85+
{
86+
strlen(ca5_bad); // NON_COMPLIANT
87+
strlen(ca5_good + 4); // COMPLIANT
88+
strlen(ca5_good + 5); // NON_COMPLIANT
89+
}
90+
// strpbrk
91+
{
92+
strpbrk(ca5_good, "test"); // COMPLIANT
93+
strpbrk(ca5_bad, "test"); // NON_COMPLIANT - not null-terminated
94+
strpbrk(ca5_good, "1234567890"); // COMPLIANT
95+
strpbrk(NULL, "12345"); // NON_COMPLIANT
96+
}
97+
// strstr
98+
{
99+
strstr("12345", "123"); // COMPLIANT
100+
strstr("123", "12345"); // COMPLIANT
101+
strstr(ca5_good, "test"); // COMPLIANT
102+
strstr(ca5_bad, "test"); // NON_COMPLIANT - not null-terminated
103+
strstr(ca5_good, "1234567890"); // COMPLIANT
104+
}
105+
// strtok
106+
{
107+
char ca5_good[5] = "test"; // ok
108+
char ca5_bad[5] = "test1"; // no null terminator
109+
char ca6_good[6] = "test1"; // ok
110+
char ca6_bad[6] = "test12"; // no null terminator
111+
strtok(NULL, NULL); // NON_COMPLIANT - 2nd arg null
112+
strtok(NULL, ""); // COMPLIANT
113+
strtok(ca5_bad, ""); // NON_COMPLIANT - 1st arg not null-terminated
114+
strtok(ca5_good, ""); // COMPLIANT
115+
strtok(ca6_good, ca5_good); // COMPLIANT
116+
strtok(ca6_good + 4, ca6_good); // COMPLIANT
117+
strtok(ca6_good, ca6_bad); // NON_COMPLIANT - 2nd arg not null-terminated
118+
}
119+
}

c/misra/test/rules/RULE-21-18/StringLibrarySizeArgumentOutOfBounds.expected

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
| test.c:77:5:77:11 | call to strncat | The $@ passed to strncat might not be null-terminated. | test.c:77:13:77:16 | buf1 | argument | test.c:77:13:77:16 | buf1 | |
2929
| test.c:81:5:81:11 | call to strncat | The size of the $@ passed to strncat is 6 bytes, but the size of the $@ is only 5 bytes. | test.c:81:25:81:31 | 12345 | read buffer | test.c:81:13:81:20 | call to get_ca_5 | write buffer |
3030
| test.c:83:5:83:11 | call to strncat | The size of the $@ passed to strncat is 5 bytes, but the size of the $@ is only 4 bytes. | test.c:83:29:83:34 | 1234 | read buffer | test.c:83:13:83:26 | ... + ... | write buffer |
31-
| test.c:94:5:94:11 | call to strncmp | The size of the $@ passed to strncmp is 5 bytes, but the $@ is 6 bytes. | test.c:94:23:94:30 | ca5_good | read buffer | test.c:94:33:94:33 | 6 | size argument |
32-
| test.c:95:5:95:11 | call to strncmp | The size of the $@ passed to strncmp is 5 bytes, but the $@ is 6 bytes. | test.c:95:13:95:20 | ca5_good | write buffer | test.c:95:32:95:32 | 6 | size argument |
33-
| test.c:95:5:95:11 | call to strncmp | The size of the $@ passed to strncmp is 5 bytes, but the $@ is 6 bytes. | test.c:95:23:95:29 | ca5_bad | read buffer | test.c:95:32:95:32 | 6 | size argument |
34-
| test.c:102:5:102:11 | call to strxfrm | The size of the $@ passed to strxfrm is 64 bytes, but the $@ is 65 bytes. | test.c:102:13:102:15 | buf | write buffer | test.c:102:25:102:39 | ... + ... | size argument |
35-
| test.c:104:5:104:11 | call to strxfrm | The $@ passed to strxfrm might not be null-terminated. | test.c:104:22:104:25 | buf2 | argument | test.c:104:22:104:25 | buf2 | |
31+
| test.c:93:5:93:11 | call to strncmp | The size of the $@ passed to strncmp is 5 bytes, but the $@ is 6 bytes. | test.c:93:23:93:30 | ca5_good | read buffer | test.c:93:33:93:33 | 6 | size argument |
32+
| test.c:94:5:94:11 | call to strncmp | The size of the $@ passed to strncmp is 5 bytes, but the $@ is 6 bytes. | test.c:94:13:94:20 | ca5_good | write buffer | test.c:94:32:94:32 | 6 | size argument |
33+
| test.c:94:5:94:11 | call to strncmp | The size of the $@ passed to strncmp is 5 bytes, but the $@ is 6 bytes. | test.c:94:23:94:29 | ca5_bad | read buffer | test.c:94:32:94:32 | 6 | size argument |
34+
| test.c:101:5:101:11 | call to strxfrm | The size of the $@ passed to strxfrm is 64 bytes, but the $@ is 65 bytes. | test.c:101:13:101:15 | buf | write buffer | test.c:101:25:101:39 | ... + ... | size argument |
35+
| test.c:103:5:103:11 | call to strxfrm | The $@ passed to strxfrm might not be null-terminated. | test.c:103:22:103:25 | buf2 | argument | test.c:103:22:103:25 | buf2 | |

c/misra/test/rules/RULE-21-18/test.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ void test(void) {
8787
char ca5_good[5] = "test"; // ok
8888
char ca5_bad[5] = "test1"; // no null terminator
8989
char ca6_good[6] = "test1"; // ok
90-
char ca6_bad[6] = "test12"; // no null terminator
9190
strncmp(ca5_good, ca5_bad, 4); // COMPLIANT
9291
strncmp(ca5_good, ca5_bad, 5); // COMPLIANT
9392
strncmp(ca6_good, ca5_bad, 5); // COMPLIANT

0 commit comments

Comments
 (0)