Skip to content

Commit 121e35e

Browse files
committed
Added cppcheck static analysis tool
Fixed potential bugs in 1. STM32L475xx_main.c - scanf overflow - printf format 2. syssbrk.c - pointer conversions when comparing address NOTE: We must add cppcheck cmake variable before the cmake add_executable is defined
1 parent 824ccea commit 121e35e

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

Template/Minimal_Tools/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ else()
6464

6565
message("Configuring ARM Project")
6666

67+
include(cmake/tool/cppcheck.cmake)
68+
6769
# Cross Compiled project
6870
set(USER_PROJECT_TARGET "${PROJECT_NAME}.elf")
6971
add_executable(${USER_PROJECT_TARGET}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
set(CPPCHECK_ENABLE "--enable=all" "--force")
2+
set(CPPCHECK_STD "--std=c11")
3+
4+
# unmatchedSuppression: When an error doesn't occur we get an error called unmatchedSuppression
5+
# unusedFunction: functions that are not used. Throws false positives when interrupt functions are defined
6+
# missingIncludeSystem: We do not require the system includes to be passed into cppcheck
7+
set(CPPCHECK_SUPPRESS
8+
"--suppress=unmatchedSuppression"
9+
"--suppress=unusedFunction"
10+
"--suppress=missingIncludeSystem"
11+
12+
# Third party library suppressions
13+
"--suppress=*:*l0_lowlevel/arm/cmsis/core/*"
14+
"--suppress=*:*l1_third_party/FreeRTOS/*"
15+
)
16+
17+
set(CPPCHECK_ADDITIONAL_OPTIONS
18+
"--error-exitcode=1"
19+
"-q"
20+
)
21+
22+
# Cppcheck directory for intermediate files
23+
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/cppcheck_build")
24+
set(CPPCHECK_BUILDDIR "--cppcheck-build-dir=${CMAKE_CURRENT_BINARY_DIR}/cppcheck_build")
25+
26+
# CMake generates compile_commands.json which can be used by cppcheck
27+
set(CPPCHECK_PROJECT "--project=${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json")
28+
29+
# Final command
30+
set(CMAKE_C_CPPCHECK "cppcheck"
31+
${CPPCHECK_BUILDDIR}
32+
${CPPCHECK_ENABLE}
33+
${CPPCHECK_SUPPRESS}
34+
${CPPCHECK_ADDITIONAL_OPTIONS}
35+
)

Template/Minimal_Tools/l0_lowlevel/newlib/syssbrk.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ caddr_t _sbrk(int increment) {
2828

2929
char *heap_limit = (char *)&__HeapLimit;
3030
char *prev_heap_end = heap_end;
31-
if ((heap_end + increment) > (char *)heap_limit) {
32-
return ((void *)-1); // error - no more memory
31+
32+
// We are comparing addresses here
33+
if ((size_t)(heap_end + increment) > (size_t)heap_limit) {
34+
return NULL; // error - no more memory
3335
}
3436

3537
heap_end += increment;

Template/Minimal_Tools/l5_application/STM32L475xx_main.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void blink_task(void *arg) {
3939
static void uart_write_task(void *arg) {
4040
uint32_t counter = 0;
4141
while (1) {
42-
printf("Hello World: %ld\r\n", counter);
42+
printf("Hello World: %u\r\n", counter);
4343
counter++;
4444
vTaskDelay(1000);
4545
}
@@ -49,8 +49,8 @@ static void uart_read_task(void *arg) {
4949
char buf[20] = {0};
5050
uint32_t counter = 0;
5151
while (1) {
52-
scanf("%s", buf);
53-
printf("Recv %ld: %s\r\n", counter, buf);
52+
scanf("%19s", buf);
53+
printf("Recv %u: %s\r\n", counter, buf);
5454
counter++;
5555
}
5656
}
@@ -66,6 +66,15 @@ static void gpio_input_task(void *arg) {
6666
}
6767
}
6868

69+
// * Uncomment the function below to see how cppcheck works
70+
// int foo(int x) {
71+
// int buf[10] = {0};
72+
// if (x == 1000) {
73+
// buf[x] = x; // <- ERROR
74+
// }
75+
// return buf[x];
76+
// }
77+
6978
int main(void) {
7079
printf("Main\r\n");
7180
// Port specific code

0 commit comments

Comments
 (0)