Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Administrator
Public Cpp Test
Commits
2a291a73
Commit
2a291a73
authored
Oct 23, 2023
by
shengnan hu
Browse files
main
parents
Changes
638
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
459 additions
and
0 deletions
+459
-0
BM-MISRA-C 2012/R.21.15/R.21.15.c
BM-MISRA-C 2012/R.21.15/R.21.15.c
+11
-0
BM-MISRA-C 2012/R.21.16/R.21.16.c
BM-MISRA-C 2012/R.21.16/R.21.16.c
+32
-0
BM-MISRA-C 2012/R.21.17/R.21.17.c
BM-MISRA-C 2012/R.21.17/R.21.17.c
+24
-0
BM-MISRA-C 2012/R.21.18/R.21.18.c
BM-MISRA-C 2012/R.21.18/R.21.18.c
+16
-0
BM-MISRA-C 2012/R.21.19/R.21.19.c
BM-MISRA-C 2012/R.21.19/R.21.19.c
+32
-0
BM-MISRA-C 2012/R.21.2/R.21.2.c
BM-MISRA-C 2012/R.21.2/R.21.2.c
+23
-0
BM-MISRA-C 2012/R.21.20/R.21.20.c
BM-MISRA-C 2012/R.21.20/R.21.20.c
+24
-0
BM-MISRA-C 2012/R.22.1/R.22.1-file1.c
BM-MISRA-C 2012/R.22.1/R.22.1-file1.c
+12
-0
BM-MISRA-C 2012/R.22.1/R.22.1-file2.c
BM-MISRA-C 2012/R.22.1/R.22.1-file2.c
+12
-0
BM-MISRA-C 2012/R.22.1/R.22.1-file3.c
BM-MISRA-C 2012/R.22.1/R.22.1-file3.c
+22
-0
BM-MISRA-C 2012/R.22.10/R.22.10.c
BM-MISRA-C 2012/R.22.10/R.22.10.c
+29
-0
BM-MISRA-C 2012/R.22.2/R.22.2.c
BM-MISRA-C 2012/R.22.2/R.22.2.c
+25
-0
BM-MISRA-C 2012/R.22.3/R.22.3.c
BM-MISRA-C 2012/R.22.3/R.22.3.c
+11
-0
BM-MISRA-C 2012/R.22.4/R.22.4.c
BM-MISRA-C 2012/R.22.4/R.22.4.c
+14
-0
BM-MISRA-C 2012/R.22.5/R.22.5.c
BM-MISRA-C 2012/R.22.5/R.22.5.c
+21
-0
BM-MISRA-C 2012/R.22.6/R.22.6.c
BM-MISRA-C 2012/R.22.6/R.22.6.c
+23
-0
BM-MISRA-C 2012/R.22.7/R.22.7.c
BM-MISRA-C 2012/R.22.7/R.22.7.c
+42
-0
BM-MISRA-C 2012/R.22.8/R.22.8.c
BM-MISRA-C 2012/R.22.8/R.22.8.c
+32
-0
BM-MISRA-C 2012/R.22.9/R.22.9.c
BM-MISRA-C 2012/R.22.9/R.22.9.c
+45
-0
BM-MISRA-C 2012/R.3.1/BM-MISRA-C2012-R.3.1.c
BM-MISRA-C 2012/R.3.1/BM-MISRA-C2012-R.3.1.c
+9
-0
No files found.
BM-MISRA-C 2012/R.21.15/R.21.15.c
0 → 100644
View file @
2a291a73
#include <stdint.h>
#include <string.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void
f
(
uint8_t
s1
[
8
],
uint16_t
s2
[
8
]
)
{
(
void
)
memcpy
(
s1
,
s2
,
8
);
/* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.21.16/R.21.16.c
0 → 100644
View file @
2a291a73
#include <stdint.h>
typedef
_Bool
bool_t
;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
struct
S
;
bool_t
f1
(
struct
S
*
s1
,
struct
S
*
s2
)
{
return
(
memcmp
(
s1
,
s2
,
sizeof
(
struct
S
)
)
!=
0
);
/* Non-compliant */
}
union
U
{
uint32_t
range
;
uint32_t
height
;
};
bool_t
f2
(
union
U
*
u1
,
union
U
*
u2
)
{
return
(
memcmp
(
u1
,
u2
,
sizeof
(
union
U
)
)
!=
0
);
/* Non-compliant */
}
const
char
a
[
6
]
=
"task"
;
bool_t
f3
(
const
char
b
[
6
]
)
{
return
(
memcmp
(
a
,
b
,
6
)
!=
0
);
/* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.21.17/R.21.17.c
0 → 100644
View file @
2a291a73
#include <string.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
char
string
[
]
=
"Short"
;
void
f1
(
const
char
*
str
)
{
(
void
)
strcpy
(
string
,
"Too long to fit"
);
/* Non-compliant */
if
(
strlen
(
str
)
<
(
sizeof
(
string
)
-
1u
)
)
/* Compliant */
{
(
void
)
strcpy
(
string
,
str
);
}
}
size_t
f2
(
void
)
{
char
text
[
5
]
=
"Token"
;
return
strlen
(
text
);
/* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.21.18/R.21.18.c
0 → 100644
View file @
2a291a73
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
char
buf1
[
5
]
=
"12345"
;
char
buf2
[
10
]
=
"1234567890"
;
void
f
(
void
)
{
if
(
memcmp
(
buf1
,
buf2
,
5
)
==
0
)
/* Compliant */
{
}
if
(
memcmp
(
buf1
,
buf2
,
6
)
==
0
)
/* Non-compliant */
{
}
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.21.19/R.21.19.c
0 → 100644
View file @
2a291a73
#include <locale.h>
#include <string.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void
f1
(
void
)
{
char
*
s1
=
setlocale
(
LC_ALL
,
0
);
/* Non-compliant */
struct
lconv
*
conv
=
localeconv
(
);
/* Non-compliant */
s1
[
1
]
=
'A'
;
/* Undefined behaviour */
conv
->
decimal_point
=
"^"
;
/* Undefined behaviour */
}
void
f2
(
void
)
{
char
str
[
128
];
(
void
)
strcpy
(
str
,
setlocale
(
LC_ALL
,
0
)
);
/* Compliant */
const
struct
lconv
*
conv
=
localeconv
(
);
/* Compliant */
conv
->
decimal_point
=
"^"
;
/* Constraint violation */
}
void
f3
(
void
)
{
const
struct
lconv
*
conv
=
localeconv
(
);
/* Compliant */
conv
->
grouping
[
2
]
=
'x'
;
/* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.21.2/R.21.2.c
0 → 100644
View file @
2a291a73
typedef
double
float64_t
;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stddef.h>
/* Non-compliant */
extern
void
*
memcpy
(
void
*
restrict
s1
,
const
void
*
restrict
s2
,
size_t
n
);
/* Non-compliant */
#define _BUILTIN_sqrt( x ) ( x )
#include <math.h>
/* ------------------ Example code from MISRA C:2012 end ------------------ */
void
callSqrt
(
void
)
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
/* sqrt may not behave as defined in the Standard */
float64_t
x
=
sqrt
(
(
float64_t
)
2
.
0
);
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
BM-MISRA-C 2012/R.21.20/R.21.20.c
0 → 100644
View file @
2a291a73
#include <locale.h>
#include <stdio.h>
#include <string.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void
f1
(
void
)
{
const
char
*
res1
;
const
char
*
res2
;
char
copy
[
128
];
res1
=
setlocale
(
LC_ALL
,
0
);
(
void
)
strcpy
(
copy
,
res1
);
res2
=
setlocale
(
LC_MONETARY
,
"French"
);
printf
(
"%s
\n
"
,
res1
);
/* Non-compliant */
printf
(
"%s
\n
"
,
copy
);
/* Compliant */
printf
(
"%s
\n
"
,
res2
);
/* Compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.22.1/R.22.1-file1.c
0 → 100644
View file @
2a291a73
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdlib.h>
int
main
(
void
)
{
void
*
b
=
malloc
(
40
);
/* Non-compliant */
return
1
;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.22.1/R.22.1-file2.c
0 → 100644
View file @
2a291a73
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
int
main
(
void
)
{
FILE
*
fp
=
fopen
(
"tmp"
,
"r"
);
/* Non-compliant */
return
1
;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.22.1/R.22.1-file3.c
0 → 100644
View file @
2a291a73
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
int
main
(
void
)
{
FILE
*
fp
;
fp
=
fopen
(
"tmp-1"
,
"w"
);
/* Non-compliant */
fprintf
(
fp
,
"*"
);
fp
=
fopen
(
"tmp-2"
,
"w"
);
fprintf
(
fp
,
"!"
);
fclose
(
fp
);
return
(
0
);
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.22.10/R.22.10.c
0 → 100644
View file @
2a291a73
#include <errno.h>
#include <stddef.h>
typedef
double
float64_t
;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void
f
(
void
)
{
float64_t
f64
;
errno
=
0
;
f64
=
atof
(
"A.12"
);
if
(
0
==
errno
)
/* Non-compliant */
{
}
errno
=
0
;
f64
=
strtod
(
"A.12"
,
NULL
);
if
(
0
==
errno
)
/* Compliant */
{
}
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.22.2/R.22.2.c
0 → 100644
View file @
2a291a73
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdlib.h>
void
fn
(
void
)
{
int32_t
a
;
free
(
&
a
);
/* Non-compliant */
}
void
g
(
void
)
{
char
*
p
=
(
char
*
)
malloc
(
512
);
char
*
q
=
p
;
free
(
p
);
free
(
q
);
/* Non-compliant */
p
=
(
char
*
)
realloc
(
p
,
1024
);
/* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.22.3/R.22.3.c
0 → 100644
View file @
2a291a73
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
void
fn
(
void
)
{
FILE
*
fw
=
fopen
(
"tmp"
,
"r+"
);
FILE
*
fr
=
fopen
(
"tmp"
,
"r"
);
/* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.22.4/R.22.4.c
0 → 100644
View file @
2a291a73
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
void
fn
(
void
)
{
FILE
*
fp
=
fopen
(
"tmp"
,
"r"
);
(
void
)
fprintf
(
fp
,
"What happens now?"
);
/* Non-compliant */
(
void
)
fclose
(
fp
);
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.22.5/R.22.5.c
0 → 100644
View file @
2a291a73
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
/* ------------------ Example code from MISRA C:2012 end ------------------ */
void
example
(
void
)
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
FILE
*
pf1
;
FILE
*
pf2
;
FILE
f3
;
pf2
=
pf1
;
/* Compliant */
f3
=
*
pf2
;
/* Non-compliant */
pf1
->
pos
=
0
;
/* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
BM-MISRA-C 2012/R.22.6/R.22.6.c
0 → 100644
View file @
2a291a73
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
void
fn
(
void
)
{
FILE
*
fp
;
void
*
p
;
fp
=
fopen
(
"tmp"
,
"w"
);
if
(
fp
==
NULL
)
{
error_action
(
);
}
fclose
(
fp
);
fprintf
(
fp
,
"?"
);
/* Non-compliant */
p
=
fp
;
/* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.22.7/R.22.7.c
0 → 100644
View file @
2a291a73
#include <stdint.h>
#include <stdio.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void
f1
(
void
)
{
char
ch
;
ch
=
(
char
)
getchar
(
);
if
(
EOF
!=
(
int32_t
)
ch
)
/* Non-compliant */
{
}
}
void
f2
(
void
)
{
char
ch
;
ch
=
(
char
)
getchar
(
);
if
(
!
feof
(
stdin
)
)
/* Compliant */
{
}
}
void
f3
(
void
)
{
int32_t
i_ch
;
i_ch
=
getchar
(
);
if
(
EOF
!=
i_ch
)
/* Compliant */
{
char
ch
;
ch
=
(
char
)
i_ch
;
}
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
BM-MISRA-C 2012/R.22.8/R.22.8.c
0 → 100644
View file @
2a291a73
#include <errno.h>
#include <stdio.h>
void
f
(
void
)
{
FILE
*
fp
;
fpos_t
*
pos
;
fp
=
fopen
(
"file.txt"
,
"w+"
);
fgetpos
(
fp
,
pos
);
/* Non-compliant */
if
(
0
==
errno
)
{
fsetpos
(
fp
,
pos
);
/* Compliant by exception */
if
(
0
==
errno
)
{
}
}
else
{
errno
=
0
;
ftell
(
fp
);
/* Compliant */
if
(
0
==
errno
)
{
}
}
}
BM-MISRA-C 2012/R.22.9/R.22.9.c
0 → 100644
View file @
2a291a73
#include <errno.h>
#include <stdio.h>
void
f1
(
void
)
{
FILE
*
fp
;
fpos_t
*
pos
;
fp
=
fopen
(
"file.txt"
,
"w+"
);
errno
=
0
;
fgetpos
(
fp
,
pos
);
someFunction
(
);
/* Non-compliant - function call */
if
(
0
!=
errno
)
{
}
errno
=
0
;
fsetpos
(
fp
,
pos
);
if
(
0
!=
errno
)
/* Compliant */
{
}
}
void
f2
(
FILE
*
f
,
fpos_t
*
pos
)
{
errno
=
0
;
if
(
fsetpos
(
f
,
pos
)
==
0
)
{
/* Compliant by exception - no need to test errno as no out-of-band error
reported. */
}
else
{
/* Something went wrong - errno holds an implementation-defined positive value.
*/
handleError
(
errno
);
}
}
BM-MISRA-C 2012/R.3.1/BM-MISRA-C2012-R.3.1.c
0 → 100644
View file @
2a291a73
// Example code from MISRA C:2012 begin
/* some comment, end comment marker accidentally omitted
<<New Page>>
Perform_Critical_Safety_Function ( X );
/* this comment is non-compliant */
// Example code from MISRA C:2012 end
Prev
1
…
26
27
28
29
30
31
32
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment