cppcheck/addons/misra-test.c

310 lines
4.4 KiB
C
Raw Normal View History

// To test:
// ~/cppcheck/cppcheck --dump misra-test.c && python misra.py -verify misra-test.c.dump
2017-04-08 19:00:50 +02:00
2017-04-14 22:22:22 +02:00
#include "path\file.h" // 20.2
2017-04-14 22:25:35 +02:00
#include /*abc*/ "file.h" // 20.3
2017-04-14 22:58:42 +02:00
#include <setjmp.h> // 21.4
#include <signal.h> // 21.5
2017-04-15 12:40:23 +02:00
#include <tgmath.h> // 21.11
2017-04-14 14:13:37 +02:00
2017-04-16 13:09:37 +02:00
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
2017-04-16 11:41:00 +02:00
typedef unsigned long long u64;
2017-04-13 10:04:50 +02:00
2017-04-11 14:45:38 +02:00
void misra_5_1() {
2017-04-08 19:00:50 +02:00
int a123456789012345678901234567890; // no-warning
2017-04-11 22:21:54 +02:00
int a1234567890123456789012345678901; // 5.1
2017-04-08 19:00:50 +02:00
}
2017-04-17 12:48:28 +02:00
#define m55(x,y) (x+y)
int m55; // 5.5
2017-04-11 14:45:38 +02:00
void misra_7_1() {
2017-04-11 22:21:54 +02:00
int x = 066; // 7.1
2017-04-08 19:00:50 +02:00
}
void misra_7_3() {
2017-04-11 22:21:54 +02:00
int x = 12l; // 7.3
2017-04-12 19:07:10 +02:00
int x = 12lu; // 7.3
2017-04-11 22:21:54 +02:00
}
2017-04-17 07:45:27 +02:00
extern int a811[]; // 8.11
2017-04-16 19:01:33 +02:00
enum e812 {
A=3,
B=3 // 8.12
};
2017-04-16 17:04:40 +02:00
void misra_8_14(char * restrict str) {} // 8.14
2017-04-16 17:19:30 +02:00
void misra_9_5() {
int x[] = {[0]=23}; // 9.5
}
2017-04-16 13:39:49 +02:00
void misra_10_4(u8 x, u16 y) {
z = x + y; // 10.4
}
2017-04-16 13:28:08 +02:00
void misra_10_6(u8 x) {
u16 y = x+x; // 10.6
}
2017-04-16 13:09:37 +02:00
void misra_10_8(u8 x) {
2017-04-17 08:44:30 +02:00
y = (u16)x;
2017-04-16 13:09:37 +02:00
y = (u16)(x+x); // 10.8
}
2017-04-16 12:13:30 +02:00
void misra_11_3(u8* p) {
x = (u64*)p; // 11.3
}
2017-04-16 12:00:03 +02:00
void misra_11_4(u8*p) {
u64 y = (u64)p; // 11.4
}
2017-04-16 11:47:44 +02:00
void misra_11_5(void *p) {
x = (u8 *)p; // 11.5
}
2017-04-16 11:41:00 +02:00
void misra_11_6() {
void *p;
p = (void*)123; // 11.6
x = (u64)p; // 11.6
}
2017-04-15 22:06:18 +02:00
struct Fred {}; struct Wilma {};
void misra_11_7(struct Fred *fred) {
struct Wilma *wilma = (struct Wilma *)fred; // 11.7
}
2017-04-15 21:55:07 +02:00
char * misra_11_8(const char *str) {
return (char *)str; // 11.8
}
2017-04-15 19:00:34 +02:00
#define MISRA_11_9 ((void*)0) // 11.9
2017-04-11 22:21:54 +02:00
void misra_12_1() {
2017-04-12 16:19:13 +02:00
sz = sizeof x + y; // 12.1
2017-04-11 22:21:54 +02:00
a = (b * c) + d;
a = b << c + d; // 12.1
}
2017-04-13 10:04:50 +02:00
void misra_12_2(u8 x) {
a = x << 8; // 12.2
}
2017-04-12 20:18:54 +02:00
void misra_12_3() {
2017-04-12 21:45:39 +02:00
f((1,2),3); // TODO
2017-04-12 20:18:54 +02:00
for (i=0;i<10;i++,j++){} // 12.3
}
2017-04-13 11:05:04 +02:00
void misra_12_4() {
x = 123456u * 123456u; // 12.4
}
2017-04-13 19:11:48 +02:00
void misra_13_1(int *p) {
volatile int v;
int a[3] = {0, (*p)++, 2}; // 13.1
int b[2] = {v,1}; // TODO
}
2017-04-13 19:38:25 +02:00
void misra_13_3() {
x = y++; // 13.3
}
2017-04-13 19:43:06 +02:00
void misra_13_4() {
if (x != (y = z)) {} // 13.4
2017-04-14 08:05:14 +02:00
else {}
2017-04-13 19:43:06 +02:00
}
2017-04-11 14:45:38 +02:00
void misra_13_5() {
2017-04-11 22:21:54 +02:00
if (x && (y++ < 123)){} // 13.5
2017-04-14 08:05:14 +02:00
else {}
2017-04-09 10:11:54 +02:00
}
2017-04-13 21:40:59 +02:00
void misra_13_6() {
return sizeof(x++); // 13.6
}
2017-04-13 22:05:27 +02:00
void misra_14_1() {
for (float f=0.1f; f<1.0f; f += 0.1f){} // 14.1
}
2017-04-13 22:26:12 +02:00
void misra_14_2() {
for (dostuff();a<10;a++) {} // 14.2
for (;i++<10;) {} // 14.2
for (;i<10;dostuff()) {} // TODO
// TODO check more variants
}
2017-04-11 14:45:38 +02:00
void misra_14_4() {
2017-04-11 22:21:54 +02:00
if (x+4){} // 14.4
2017-04-14 08:05:14 +02:00
else {}
2017-04-08 19:00:50 +02:00
}
2017-04-11 14:45:38 +02:00
void misra_15_1() {
2017-04-11 22:21:54 +02:00
goto a1; // 15.1
2017-04-13 22:44:43 +02:00
a1:
2017-04-08 19:00:50 +02:00
}
2017-04-13 22:44:43 +02:00
void misra_15_2() {
label:
goto label; // 15.2 15.1
}
2017-04-13 23:02:06 +02:00
void misra_15_3() {
if (x!=0) {
goto L1; // 15.3 15.1
if (y!=0) {
L1:
2017-04-14 08:05:14 +02:00
} else {}
} else {}
2017-04-13 23:02:06 +02:00
}
2017-04-13 23:07:41 +02:00
int misra_15_5() {
if (x!=0) {
return 1; // 15.5
2017-04-14 08:05:14 +02:00
} else {}
2017-04-13 23:07:41 +02:00
return 2;
}
2017-04-13 23:24:35 +02:00
void misra_15_6() {
if (x!=0); // 15.6
2017-04-14 08:05:14 +02:00
else{}
2017-04-17 10:39:05 +02:00
#if A>1 // no-warning
2017-04-17 09:38:46 +02:00
(void)0;
#endif
2017-04-17 10:39:05 +02:00
do {} while (x<0); // no-warning
2017-04-14 08:05:14 +02:00
}
void misra_15_7() {
if (x!=0){} // 15.7
2017-04-13 23:24:35 +02:00
}
2017-04-14 08:05:14 +02:00
2017-04-14 08:17:32 +02:00
void misra_16_2() {
switch (x) {
2017-04-14 12:57:07 +02:00
default:
break;
2017-04-14 08:17:32 +02:00
case 1:
while (y>4) {
case 2: break; // 16.2
}
break;
}
}
2017-04-14 10:46:35 +02:00
void misra_16_3() {
switch (x) {
case 1:
case 2:
a=1;
case 3: // 16.3
a=2;
// fallthrough
case 5:
break;
default: break;
}
}
2017-04-14 12:57:07 +02:00
void misra_16_4() {
switch (x) { // 16.4
case 1:
break;
case 2:
break;
}
2017-04-14 13:07:48 +02:00
}
void misra_16_5() {
switch (x) {
case 1:
break;
default: // 16.5
break;
case 2:
break;
}
}
2017-04-14 13:18:20 +02:00
void misra_16_6() {
switch (x) { // 16.6
default:
break;
}
2017-04-17 09:07:45 +02:00
switch (x) {
case 1: {break;}
case 2: {break;}
default: {break;}
}
2017-04-14 13:18:20 +02:00
}
2017-04-14 13:23:53 +02:00
void misra_16_7() {
switch (x != 123) { // 16.7
case 1:
break;
default:
break;
}
}
2017-04-14 14:13:37 +02:00
2017-04-15 15:20:28 +02:00
void misra_17_1() {
va_list(); // 17.1
va_arg(); // 17.1
va_start(); // 17.1
va_end(); // 17.1
va_copy(); // 17.1
}
2017-04-14 18:51:12 +02:00
void misra_17_6(int x[static 20]) {} // 17.6
2017-04-14 20:38:35 +02:00
void misra_17_8(int x) {
x = 3; // 17.8
}
2017-04-14 20:51:09 +02:00
void misra_18_5() {
int *** p; // 18.5
}
2017-04-14 21:09:25 +02:00
void misra_18_8(int x) {
int buf1[10];
int buf2[sizeof(int)];
int vla[x]; // 18.8
}
2017-04-14 21:14:26 +02:00
union misra_19_2 { }; // 19.2
2017-04-14 21:49:52 +02:00
#include "notfound.h" // 20.1
2017-04-14 22:37:17 +02:00
#define int short // 20.4
#undef X // 20.5
2017-04-14 22:53:45 +02:00
void misra_21_3() {
p1=malloc(10); // 21.3
p2=calloc(10); // 21.3
realloc(10); // 21.3
free(p1); // 21.3
}
2017-04-15 08:08:17 +02:00
void misra_21_7() {
2017-04-15 08:24:24 +02:00
atof(str); // 21.7
2017-04-15 08:08:17 +02:00
atoi(str); // 21.7
2017-04-15 08:24:24 +02:00
atol(str); // 21.7
atoll(str); // 21.7
}
void misra_21_8() {
abort(); // 21.8
getenv("foo"); // 21.8
system(""); // 21.8
2017-04-15 08:08:17 +02:00
}
2017-04-15 08:30:09 +02:00
void misra_21_9() {
bsearch(key,base,num,size,cmp); // 21.9
qsort(base,num,size,cmp); // 21.9
}