Skip to content

Commit 96f35fc

Browse files
authored
Merge pull request #3334 from hathach/fix-alerts-3
Fix more code alerts
2 parents 91c3a4f + 7f173ab commit 96f35fc

File tree

30 files changed

+640
-609
lines changed

30 files changed

+640
-609
lines changed

.PVS-Studio/.pvsconfig

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
//V_EXCLUDE_PATH */iar/cxarm*
2-
//V_EXCLUDE_PATH */pico-sdk/
3-
//V_EXCLUDE_PATH */esp-idf/
4-
//V_EXCLUDE_PATH */hw/bsp/espressif/components/
5-
//V_EXCLUDE_PATH */hw/mcu/
2+
//V_EXCLUDE_PATH */pico-sdk/*
3+
//V_EXCLUDE_PATH */esp-idf/*
4+
//V_EXCLUDE_PATH */hw/mcu/*
5+
//V_EXCLUDE_PATH */hw/bsp/espressif/components/*
6+
//V_EXCLUDE_PATH */lib/*
67

78
//-V::2506 MISRA. A function should have a single point of exit at the end.
89
//-V::2514 MISRA. Unions should not be used.
10+
//-V::2520 [MISRA-C-16.3] Every switch-clause should be terminated by an unconditional 'break' statement
911
//-V:memcpy:2547 [MISRA-C-17.7] The return value of non-void function 'memcpy' should be used.
10-
//-V:printf:2547 [MISRA-C-17.7] The return value of non-void function 'printf' should be used.
12+
//-V:memmove:2547 [MISRA-C-17.7] The return value of non-void function 'memmove' should be used.
13+
//-V:printf:2547 [MISRA-C-17.7]
1114
//-V::2584::{gintsts} dwc2
1215
//-V::2600 [MISRA-C-21.6] The function with the 'printf' name should not be used.
1316
//+V2614 DISABLE_LENGHT_LIMIT_CHECK:YES
1417
//-V:memcpy:2628 Pointer arguments to the 'memcpy' function should be pointers to qualified or unqualified versions of compatible types.
18+
//-V::2659 [MISRA-C-16.1] Switch statements should be well-formed. Every switch-clause should be terminated by an unconditional 'break' statement

.clang-format

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ AllowAllConstructorInitializersOnNextLine: false
3333
AllowAllParametersOfDeclarationOnNextLine: false
3434
AllowShortBlocksOnASingleLine: Empty
3535
AllowShortCaseExpressionOnASingleLine: true
36-
AllowShortCaseLabelsOnASingleLine: true
36+
AllowShortCaseLabelsOnASingleLine: false
37+
AllowShortEnumsOnASingleLine: false
3738
AllowShortFunctionsOnASingleLine: None
3839
AllowShortIfStatementsOnASingleLine: Never
3940
AlwaysBreakTemplateDeclarations: Yes
@@ -76,6 +77,8 @@ MacroBlockBegin: ''
7677
MacroBlockEnd: ''
7778
MaxEmptyLinesToKeep: 2
7879
NamespaceIndentation: All
80+
QualifierAlignment: Custom
81+
QualifierOrder: ['static', 'const', 'volatile', 'restrict', 'type']
7982
ReflowComments: false
8083
SpaceAfterTemplateKeyword: false
8184
SpaceBeforeRangeBasedForLoopColon: false
@@ -84,5 +87,6 @@ SpacesInAngles: false
8487
SpacesInConditionalStatement: false
8588
SpacesInCStyleCastParentheses: false
8689
SpacesInParentheses: false
90+
SortIncludes: false
8791
TabWidth: 2
8892
...

.github/copilot-instructions.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ python3 tools/build.py -b BOARD_NAME
8888
- Check spelling: `pip install codespell && codespell` (uses `.codespellrc` config)
8989
- Pre-commit hooks validate unit tests and code quality automatically
9090

91+
### Static Analysis with PVS-Studio
92+
- **Analyze whole project**:
93+
```bash
94+
pvs-studio-analyzer analyze -f examples/cmake-build-raspberry_pi_pico/compile_commands.json -R .PVS-Studio/.pvsconfig -o pvs-report.log -j12 --dump-files --misra-cpp-version 2008 --misra-c-version 2023 --use-old-parser
95+
```
96+
- **Analyze specific source files**:
97+
```bash
98+
pvs-studio-analyzer analyze -f examples/cmake-build-raspberry_pi_pico/compile_commands.json -R .PVS-Studio/.pvsconfig -S path/to/file.c -o pvs-report.log -j12 --dump-files --misra-cpp-version 2008 --misra-c-version 2023 --use-old-parser
99+
```
100+
- **Multiple specific files**:
101+
```bash
102+
pvs-studio-analyzer analyze -f examples/cmake-build-raspberry_pi_pico/compile_commands.json -R .PVS-Studio/.pvsconfig -S src/file1.c -S src/file2.c -o pvs-report.log -j12 --dump-files --misra-cpp-version 2008 --misra-c-version 2023 --use-old-parser
103+
```
104+
- Requires `compile_commands.json` in the build directory (generated by CMake with `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`)
105+
- Use `-f` option to specify path to `compile_commands.json`
106+
- Use `-R .PVS-Studio/.pvsconfig` to specify rule configuration file
107+
- Use `-j12` for parallel analysis with 12 threads
108+
- `--dump-files` saves preprocessed files for debugging
109+
- `--misra-c-version 2023` enables MISRA C:2023 checks
110+
- `--misra-cpp-version 2008` enables MISRA C++:2008 checks
111+
- `--use-old-parser` uses legacy parser for compatibility
112+
- Analysis takes ~10-30 seconds depending on project size. Set timeout to 5+ minutes.
113+
- View results: `plog-converter -a GA:1,2 -t errorfile pvs-report.log` or open in PVS-Studio GUI
114+
91115
## Validation
92116

93117
### ALWAYS Run These After Making Changes

.github/workflows/static_analysis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,14 @@ jobs:
118118
sudo apt update
119119
sudo apt install pvs-studio
120120
pvs-studio-analyzer credentials ${{ secrets.PVS_STUDIO_CREDENTIALS }}
121+
pvs-studio-analyzer --version
121122
122123
- name: Analyze
123124
run: |
124125
mkdir -p build
125126
cmake examples -B build -G Ninja -DBOARD=${{ matrix.board }} -DCMAKE_BUILD_TYPE=MinSizeRel
126127
cmake --build build
127-
pvs-studio-analyzer analyze -R .PVS-Studio/.pvsconfig -f build/compile_commands.json --exclude-path hw/mcu/ --exclude-path lib/ -j
128+
pvs-studio-analyzer analyze -f build/compile_commands.json -R .PVS-Studio/.pvsconfig -j4 --security-related-issues --misra-cpp-version 2008 --misra-c-version 2023 --use-old-parser -e lib/ -e hw/mcu/ -e */iar/cxarm/ -e pico-sdk/
128129
plog-converter -t sarif -o pvs-studio-${{ matrix.board }}.sarif PVS-Studio.log
129130
130131
- name: Upload SARIF

examples/device/cdc_msc/src/main.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
*/
3838
enum {
3939
BLINK_NOT_MOUNTED = 250,
40-
BLINK_MOUNTED = 1000,
41-
BLINK_SUSPENDED = 2500,
40+
BLINK_MOUNTED = 1000,
41+
BLINK_SUSPENDED = 2500,
4242
};
4343

4444
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
45-
static bool blink_enable = true;
45+
static bool blink_enable = true;
4646

4747
void led_blinking_task(void);
4848
void cdc_task(void);
@@ -52,10 +52,7 @@ int main(void) {
5252
board_init();
5353

5454
// init device stack on configured roothub port
55-
tusb_rhport_init_t dev_init = {
56-
.role = TUSB_ROLE_DEVICE,
57-
.speed = TUSB_SPEED_AUTO
58-
};
55+
tusb_rhport_init_t dev_init = {.role = TUSB_ROLE_DEVICE, .speed = TUSB_SPEED_AUTO};
5956
tusb_init(BOARD_TUD_RHPORT, &dev_init);
6057

6158
board_init_after_tusb();
@@ -86,7 +83,7 @@ void tud_umount_cb(void) {
8683
// remote_wakeup_en : if host allow us to perform remote wakeup
8784
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
8885
void tud_suspend_cb(bool remote_wakeup_en) {
89-
(void) remote_wakeup_en;
86+
(void)remote_wakeup_en;
9087
blink_interval_ms = BLINK_SUSPENDED;
9188
}
9289

@@ -107,9 +104,9 @@ void cdc_task(void) {
107104
// connected and there are data available
108105
if (tud_cdc_available()) {
109106
// read data
110-
char buf[64];
107+
char buf[64];
111108
uint32_t count = tud_cdc_read(buf, sizeof(buf));
112-
(void) count;
109+
(void)count;
113110

114111
// Echo back
115112
// Note: Skip echo by commenting out write() and write_flush()
@@ -120,10 +117,12 @@ void cdc_task(void) {
120117
}
121118

122119
// Press on-board button to send Uart status notification
120+
static cdc_notify_uart_state_t uart_state = {.value = 0};
121+
123122
static uint32_t btn_prev = 0;
124-
static cdc_notify_uart_state_t uart_state = { .value = 0 };
125-
const uint32_t btn = board_button_read();
126-
if ((btn_prev == 0u) && btn) {
123+
const uint32_t btn = board_button_read();
124+
125+
if ((btn_prev == 0u) && (btn != 0u)) {
127126
uart_state.dsr ^= 1;
128127
tud_cdc_notify_uart_state(&uart_state);
129128
}
@@ -133,8 +132,8 @@ void cdc_task(void) {
133132

134133
// Invoked when cdc when line state changed e.g connected/disconnected
135134
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
136-
(void) itf;
137-
(void) rts;
135+
(void)itf;
136+
(void)rts;
138137

139138
if (dtr) {
140139
// Terminal connected
@@ -148,15 +147,15 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
148147

149148
// Invoked when CDC interface received data from host
150149
void tud_cdc_rx_cb(uint8_t itf) {
151-
(void) itf;
150+
(void)itf;
152151
}
153152

154153
//--------------------------------------------------------------------+
155154
// BLINKING TASK
156155
//--------------------------------------------------------------------+
157156
void led_blinking_task(void) {
158-
static uint32_t start_ms = 0;
159-
static bool led_state = false;
157+
static uint32_t start_ms = 0;
158+
static bool led_state = false;
160159

161160
if (blink_enable) {
162161
// Blink every interval ms

examples/device/cdc_uac2/src/cdc_app.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void tud_cdc_rx_cb(uint8_t itf) {
4848
// connected() check for DTR bit
4949
// Most but not all terminal client set this when making connection
5050
if (tud_cdc_connected()) {
51-
if (tud_cdc_available()) {
51+
if (tud_cdc_available() > 0) {
5252
count = tud_cdc_n_read(itf, buf, sizeof(buf));
5353
(void) count;
5454

examples/device/mtp/src/mtp_fs_example.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ static int32_t fs_get_device_properties(tud_mtp_cb_data_t* cb_data) {
414414
// get describing dataset
415415
mtp_device_prop_desc_header_t device_prop_header;
416416
device_prop_header.device_property_code = dev_prop_code;
417-
switch (dev_prop_code) { //-V2520 //-V2659
417+
switch (dev_prop_code) {
418418
case MTP_DEV_PROP_DEVICE_FRIENDLY_NAME:
419419
device_prop_header.datatype = MTP_DATA_TYPE_STR;
420420
device_prop_header.get_set = MTP_MODE_GET;
@@ -430,7 +430,7 @@ static int32_t fs_get_device_properties(tud_mtp_cb_data_t* cb_data) {
430430
}
431431
} else {
432432
// get value
433-
switch (dev_prop_code) { //-V2520 //-V2659
433+
switch (dev_prop_code) {
434434
case MTP_DEV_PROP_DEVICE_FRIENDLY_NAME:
435435
(void) mtp_container_add_cstring(io_container, DEV_PROP_FRIENDLY_NAME);
436436
tud_mtp_data_send(io_container);

examples/device/net_lwip_webserver/src/arch/cc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
* Author: Adam Dunkels <[email protected]>
3030
*
3131
*/
32-
#ifndef __CC_H__
33-
#define __CC_H__
32+
#ifndef CC_H__
33+
#define CC_H__
3434

3535
//#include "cpu.h"
3636

@@ -72,4 +72,4 @@ typedef int sys_prot_t;
7272

7373
#define LWIP_PLATFORM_ASSERT(x) do { if(!(x)) while(1); } while(0)
7474

75-
#endif /* __CC_H__ */
75+
#endif /* CC_H__ */

0 commit comments

Comments
 (0)