Skip to content

Commit f1d5963

Browse files
committed
1
Signed-off-by: sakumisu <1203593632@qq.com>
1 parent 8f04252 commit f1d5963

14 files changed

Lines changed: 195 additions & 164 deletions

.clang-tidy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
Checks: >
3+
-*,
4+
clang-analyzer-core.*,
5+
clang-analyzer-deadcode.*,
6+
clang-analyzer-nullability.*,
7+
clang-analyzer-security.*,
8+
-clang-analyzer-security.insecureAPI.*,
9+
clang-analyzer-unix.*,
10+
bugprone-*,
11+
-bugprone-easily-swappable-parameters,
12+
-bugprone-macro-parentheses,
13+
-bugprone-reserved-identifier
14+
15+
WarningsAsErrors: ''
16+
HeaderFilterRegex: '.*(core|class|common|osal|port|platform)/.*'
17+
FormatStyle: none
18+
19+
CheckOptions:
20+
bugprone-argument-comment.StrictMode: false
21+
bugprone-sizeof-expression.WarnOnSizeOfConstant: true
22+
bugprone-sizeof-expression.WarnOnSizeOfIntegerExpression: false
23+
bugprone-suspicious-memset-usage.WarnOnImplicitCast: true
24+
...

.github/workflows/clang-tidy.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: Clang-Tidy action
2+
3+
on:
4+
push:
5+
pull_request:
6+
workflow_dispatch:
7+
inputs:
8+
scan_regex:
9+
description: "Regex used to select files from compile_commands.json"
10+
required: false
11+
default: "/(core|class|common|osal|port|platform)/.*[.]c$"
12+
13+
jobs:
14+
clang-tidy:
15+
name: clang-tidy
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
21+
- name: Install dependencies
22+
run: |
23+
sudo apt-get update
24+
sudo apt-get install -y clang-tidy cmake ninja-build jq ripgrep wget
25+
26+
- name: Download hpm_sdk
27+
run: |
28+
cd ~
29+
git clone --depth 1 https://github.com/hpmicro/hpm_sdk.git
30+
31+
- name: Download RISC-V toolchain
32+
run: |
33+
cd ~
34+
wget -q https://github.com/hpmicro/riscv-gnu-toolchain/releases/download/2023.10.18/rv32imac_zicsr_zifencei_multilib_b_ext-linux.tar.gz
35+
tar -xzf rv32imac_zicsr_zifencei_multilib_b_ext-linux.tar.gz
36+
37+
- name: Generate HPM compile database
38+
run: |
39+
cd tests/hpmicro
40+
export HPM_SDK_BASE=~/hpm_sdk
41+
export GNURISCV_TOOLCHAIN_PATH=~/rv32imac_zicsr_zifencei_multilib_b_ext-linux
42+
export HPM_SDK_TOOLCHAIN_VARIANT=
43+
cmake -S . -B build -GNinja \
44+
-DBOARD=hpm6800evk \
45+
-DHPM_BUILD_TYPE=flash_sdram_xip \
46+
-DCMAKE_BUILD_TYPE=debug \
47+
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
48+
49+
- name: Run clang-tidy
50+
shell: bash
51+
env:
52+
SCAN_REGEX: ${{ github.event.inputs.scan_regex || '/(core|class|common|osal|port|platform)/.*[.]c$' }}
53+
run: |
54+
TOOLCHAIN="$HOME/rv32imac_zicsr_zifencei_multilib_b_ext-linux"
55+
GCC="$TOOLCHAIN/bin/riscv32-unknown-elf-gcc"
56+
GCC_INCLUDE="$("$GCC" -print-file-name=include)"
57+
GCC_INCLUDE_FIXED="$("$GCC" -print-file-name=include-fixed)"
58+
59+
set +e
60+
jq -r '.[].file' tests/hpmicro/build/compile_commands.json \
61+
| rg "^${GITHUB_WORKSPACE}${SCAN_REGEX}" \
62+
| xargs -r -n1 clang-tidy \
63+
-p tests/hpmicro/build \
64+
--quiet \
65+
--extra-arg-before=--target=riscv32-unknown-elf \
66+
--extra-arg=-isystem"$GCC_INCLUDE" \
67+
--extra-arg=-isystem"$GCC_INCLUDE_FIXED" \
68+
> clang-tidy.log 2>&1
69+
tidy_status=$?
70+
set -e
71+
72+
grep -E "warning:|error:" clang-tidy.log | tee clang-tidy-summary.log || true
73+
74+
while IFS= read -r line; do
75+
if [[ "$line" =~ ^([^:]+):([0-9]+):([0-9]+):[[:space:]]warning:[[:space:]](.*)$ ]]; then
76+
echo "::warning file=${BASH_REMATCH[1]},line=${BASH_REMATCH[2]},col=${BASH_REMATCH[3]}::${BASH_REMATCH[4]}"
77+
elif [[ "$line" =~ ^([^:]+):([0-9]+):([0-9]+):[[:space:]]error:[[:space:]](.*)$ ]]; then
78+
echo "::error file=${BASH_REMATCH[1]},line=${BASH_REMATCH[2]},col=${BASH_REMATCH[3]}::${BASH_REMATCH[4]}"
79+
fi
80+
done < clang-tidy-summary.log
81+
82+
if [ "$tidy_status" -ne 0 ]; then
83+
exit "$tidy_status"
84+
fi
85+
86+
- name: Upload clang-tidy logs
87+
if: always()
88+
uses: actions/upload-artifact@v4
89+
with:
90+
name: clang-tidy-logs
91+
path: |
92+
clang-tidy.log
93+
clang-tidy-summary.log

.github/workflows/cppcheck.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,12 @@ jobs:
1313
- name: cppcheck
1414
shell: bash
1515
run: |
16+
set -o pipefail
1617
sudo apt install cppcheck
17-
cppcheck --enable=warning,portability,performance --language=c --platform=unix32 --std=c99 --force . -i third_party/ -i class/template -i port/template/
18+
cppcheck --enable=warning,portability,performance --language=c --platform=unix32 --std=c99 --quiet --force . -i third_party/ -i class/template -i port/template/ -i tests/ --include=tests/hpmicro/inc/usb_config.h 2>&1 | tee cppcheck.log
19+
- name: Upload cppcheck log
20+
if: always()
21+
uses: actions/upload-artifact@v4
22+
with:
23+
name: cppcheck-log
24+
path: cppcheck.log

class/msc/usbh_msc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,12 @@ int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, con
403403
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
404404
cbw->dSignature = MSC_CBW_Signature;
405405

406-
cbw->dDataLength = (msc_class->blocksize * nsectors);
406+
cbw->dDataLength = (msc_class->blocksize * (nsectors & 0xffff));
407407
cbw->bCBLength = SCSICMD_WRITE10_SIZEOF;
408408
cbw->CB[0] = SCSI_CMD_WRITE10;
409409

410410
SET_BE32(&cbw->CB[2], start_sector);
411-
SET_BE16(&cbw->CB[7], nsectors);
411+
SET_BE16(&cbw->CB[7], nsectors & 0xffff);
412412

413413
return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_cbw_csw[msc_class->sdchar - 'a'], (uint8_t *)buffer, CONFIG_USBHOST_MSC_TIMEOUT);
414414
}
@@ -422,13 +422,13 @@ int usbh_msc_scsi_read10(struct usbh_msc *msc_class, uint32_t start_sector, cons
422422
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
423423
cbw->dSignature = MSC_CBW_Signature;
424424

425-
cbw->dDataLength = (msc_class->blocksize * nsectors);
425+
cbw->dDataLength = (msc_class->blocksize * (nsectors & 0xffff));
426426
cbw->bmFlags = 0x80;
427427
cbw->bCBLength = SCSICMD_READ10_SIZEOF;
428428
cbw->CB[0] = SCSI_CMD_READ10;
429429

430430
SET_BE32(&cbw->CB[2], start_sector);
431-
SET_BE16(&cbw->CB[7], nsectors);
431+
SET_BE16(&cbw->CB[7], nsectors & 0xffff);
432432

433433
return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_cbw_csw[msc_class->sdchar - 'a'], (uint8_t *)buffer, CONFIG_USBHOST_MSC_TIMEOUT);
434434
}

class/serial/usbh_serial.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ struct usbh_serial *usbh_serial_open(const char *devname, uint32_t open_flags)
228228
return NULL;
229229
}
230230

231-
if (serial && serial->driver && serial->driver->open) {
231+
if (serial->driver && serial->driver->open) {
232232
ret = serial->driver->open(serial);
233233
if (ret < 0) {
234234
return NULL;
@@ -335,6 +335,8 @@ int usbh_serial_control(struct usbh_serial *serial, int cmd, void *arg)
335335
struct usbh_serial_termios *termios = (struct usbh_serial_termios *)arg;
336336
struct cdc_line_coding line_coding;
337337

338+
USB_ASSERT(termios != NULL);
339+
338340
line_coding.dwDTERate = termios->baudrate;
339341
line_coding.bCharFormat = termios->stopbits;
340342
line_coding.bParityType = termios->parity;
@@ -347,7 +349,7 @@ int usbh_serial_control(struct usbh_serial *serial, int cmd, void *arg)
347349
usbh_kill_urb(&serial->bulkout_urb);
348350
}
349351

350-
if (serial && serial->driver && serial->driver->set_line_coding) {
352+
if (serial->driver && serial->driver->set_line_coding) {
351353
ret = serial->driver->set_line_coding(serial, &line_coding);
352354
if (ret < 0) {
353355
return ret;
@@ -358,7 +360,7 @@ int usbh_serial_control(struct usbh_serial *serial, int cmd, void *arg)
358360

359361
memcpy(&serial->line_coding, &line_coding, sizeof(struct cdc_line_coding));
360362

361-
if (serial && serial->driver && serial->driver->set_flow_control) {
363+
if (serial->driver && serial->driver->set_flow_control) {
362364
ret = serial->driver->set_flow_control(serial, termios->rtscts);
363365
}
364366

@@ -383,8 +385,10 @@ int usbh_serial_control(struct usbh_serial *serial, int cmd, void *arg)
383385
struct usbh_serial_termios *termios = (struct usbh_serial_termios *)arg;
384386
struct cdc_line_coding line_coding;
385387

386-
if (serial && serial->driver && serial->driver->get_line_coding) {
387-
return serial->driver->get_line_coding(serial, &line_coding);
388+
USB_ASSERT(termios != NULL);
389+
390+
if (serial->driver && serial->driver->get_line_coding) {
391+
serial->driver->get_line_coding(serial, &line_coding);
388392
} else {
389393
memcpy(&line_coding, &serial->line_coding, sizeof(struct cdc_line_coding));
390394
}
@@ -424,7 +428,7 @@ int usbh_serial_control(struct usbh_serial *serial, int cmd, void *arg)
424428
uint32_t *flags = (uint32_t *)arg;
425429
int status;
426430

427-
if (serial && serial->driver && serial->driver->get_modem_status) {
431+
if (serial->driver && serial->driver->get_modem_status) {
428432
status = serial->driver->get_modem_status(serial);
429433
if (status < 0) {
430434
return status;

class/vendor/display/usbd_display.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ void usbd_display_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
127127
usbd_ep_start_read(busid, g_usbd_display.out_ep.ep_addr, usb_dispay_dummy, usbd_get_ep_mps(0, g_usbd_display.out_ep.ep_addr));
128128
return;
129129
get_frame:
130+
USB_ASSERT((usb_display_buf_offset + 16384) <= g_usbd_display.current_frame->frame_bufsize);
130131
usbd_ep_start_read(busid, g_usbd_display.out_ep.ep_addr, &g_usbd_display.current_frame->frame_buf[usb_display_buf_offset], 16384);
131132
return;
132133
}
@@ -154,7 +155,7 @@ struct usbd_interface *usbd_display_init_intf(struct usbd_interface *intf,
154155
usbd_add_endpoint(0, &g_usbd_display.in_ep);
155156

156157
for (uint32_t i = 0; i < count; i++) {
157-
USB_ASSERT_MSG(frame[i].frame_bufsize % 16384, "frame_bufsize must be the multiple of 16384");
158+
USB_ASSERT_MSG((frame[i].frame_bufsize % 16384) == 0, "frame_bufsize must be the multiple of 16384");
158159
}
159160

160161
usbd_display_frame_create(frame, count);

class/vendor/net/usbh_asix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
static struct usbh_asix g_asix_class;
1717

18-
static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_rx_buffer[USB_ALIGN_UP(CONFIG_USBHOST_ASIX_ETH_MAX_TX_SIZE, CONFIG_USB_ALIGN_SIZE)];
19-
static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_tx_buffer[USB_ALIGN_UP(CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE, CONFIG_USB_ALIGN_SIZE)];
18+
static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_rx_buffer[USB_ALIGN_UP(CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE, CONFIG_USB_ALIGN_SIZE)];
19+
static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_tx_buffer[USB_ALIGN_UP(CONFIG_USBHOST_ASIX_ETH_MAX_TX_SIZE, CONFIG_USB_ALIGN_SIZE)];
2020
static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_inttx_buffer[USB_ALIGN_UP(16, CONFIG_USB_ALIGN_SIZE)];
2121

2222
static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_buf[USB_ALIGN_UP(32, CONFIG_USB_ALIGN_SIZE)];

core/usbh_core.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static int parse_config_descriptor(struct usbh_hubport *hport, struct usb_config
225225

226226
memset(hport->config.intf, 0, sizeof(struct usbh_interface) * CONFIG_USBHOST_MAX_INTERFACES);
227227

228-
while (p[DESC_bLength] && (desc_len <= length)) {
228+
while (p[DESC_bLength]) {
229229
switch (p[DESC_bDescriptorType]) {
230230
case USB_DESCRIPTOR_TYPE_INTERFACE:
231231
intf_desc = (struct usb_interface_descriptor *)p;
@@ -276,6 +276,10 @@ static int parse_config_descriptor(struct usbh_hubport *hport, struct usb_config
276276
/* skip to next descriptor */
277277
desc_len += p[DESC_bLength];
278278
p += p[DESC_bLength];
279+
280+
if(desc_len > length) {
281+
return -USB_ERR_NOMEM;
282+
}
279283
}
280284
}
281285
return 0;
@@ -799,6 +803,10 @@ static struct usbh_hubport *usbh_list_all_hubport(struct usbh_hub *hub, uint8_t
799803
struct usbh_hubport *hport;
800804
struct usbh_hub *hub_next;
801805

806+
if ((hub_index > hub->index) || (hub_port > hub->nports)) {
807+
return NULL;
808+
}
809+
802810
if (hub->index == hub_index) {
803811
hport = &hub->child[hub_port - 1];
804812
if (hport->connected) {
@@ -816,9 +824,9 @@ static struct usbh_hubport *usbh_list_all_hubport(struct usbh_hub *hub, uint8_t
816824
hub_next = hport->config.intf[itf].priv;
817825

818826
if (hub_next && hub_next->connected) {
819-
hport = usbh_list_all_hubport(hub_next, hub_index, hub_port);
820-
if (hport) {
821-
return hport;
827+
struct usbh_hubport *hport_next = usbh_list_all_hubport(hub_next, hub_index, hub_port);
828+
if (hport_next) {
829+
return hport_next;
822830
}
823831
}
824832
}

osal/usb_osal_freertos.c

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size,
1818
stack_size /= sizeof(StackType_t);
1919
xTaskCreate(entry, name, stack_size, args, configMAX_PRIORITIES - 1 - prio, &htask);
2020
if (htask == NULL) {
21-
USB_LOG_ERR("Create thread %s failed\r\n", name);
22-
while (1) {
23-
}
21+
return NULL;
2422
}
2523
return (usb_osal_thread_t)htask;
2624
}
@@ -44,24 +42,12 @@ void usb_osal_thread_schedule_other(void)
4442

4543
usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count)
4644
{
47-
usb_osal_sem_t sem = (usb_osal_sem_t)xSemaphoreCreateCounting(1, initial_count);
48-
if (sem == NULL) {
49-
USB_LOG_ERR("Create semaphore failed\r\n");
50-
while (1) {
51-
}
52-
}
53-
return sem;
45+
return (usb_osal_sem_t)xSemaphoreCreateCounting(1, initial_count);
5446
}
5547

5648
usb_osal_sem_t usb_osal_sem_create_counting(uint32_t max_count)
5749
{
58-
usb_osal_sem_t sem = (usb_osal_sem_t)xSemaphoreCreateCounting(max_count, 0);
59-
if (sem == NULL) {
60-
USB_LOG_ERR("Create semaphore failed\r\n");
61-
while (1) {
62-
}
63-
}
64-
return sem;
50+
return (usb_osal_sem_t)xSemaphoreCreateCounting(max_count, 0);
6551
}
6652

6753
void usb_osal_sem_delete(usb_osal_sem_t sem)
@@ -102,13 +88,7 @@ void usb_osal_sem_reset(usb_osal_sem_t sem)
10288

10389
usb_osal_mutex_t usb_osal_mutex_create(void)
10490
{
105-
usb_osal_mutex_t mutex = (usb_osal_mutex_t)xSemaphoreCreateMutex();
106-
if (mutex == NULL) {
107-
USB_LOG_ERR("Create mutex failed\r\n");
108-
while (1) {
109-
}
110-
}
111-
return mutex;
91+
return (usb_osal_mutex_t)xSemaphoreCreateMutex();
11292
}
11393

11494
void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
@@ -186,11 +166,8 @@ struct usb_osal_timer *usb_osal_timer_create(const char *name, uint32_t timeout_
186166
(void)name;
187167

188168
timer = pvPortMalloc(sizeof(struct usb_osal_timer));
189-
190169
if (timer == NULL) {
191-
USB_LOG_ERR("Create usb_osal_timer failed\r\n");
192-
while (1) {
193-
}
170+
return NULL;
194171
}
195172
memset(timer, 0, sizeof(struct usb_osal_timer));
196173

@@ -199,9 +176,8 @@ struct usb_osal_timer *usb_osal_timer_create(const char *name, uint32_t timeout_
199176

200177
timer->timer = (void *)xTimerCreate("usb_tim", pdMS_TO_TICKS(timeout_ms), is_period, timer, (TimerCallbackFunction_t)__usb_timeout);
201178
if (timer->timer == NULL) {
202-
USB_LOG_ERR("Create timer failed\r\n");
203-
while (1) {
204-
}
179+
vPortFree(timer);
180+
return NULL;
205181
}
206182
return timer;
207183
}

0 commit comments

Comments
 (0)