Skip to content

Fix code alerts#3326

Merged
hathach merged 7 commits intomasterfrom
fix-code-alerts
Nov 5, 2025
Merged

Fix code alerts#3326
hathach merged 7 commits intomasterfrom
fix-code-alerts

Conversation

@hathach
Copy link
Owner

@hathach hathach commented Nov 3, 2025

fixed alerts found by pvs-studio

Copilot AI review requested due to automatic review settings November 3, 2025 04:44
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR performs code cleanup and standardization across the TinyUSB codebase. The changes remove deprecated macros, improve code consistency, and fix minor issues related to header includes and code style.

Key changes:

  • Removes deprecated TU_BIN8() macro usage and replaces with hex literals
  • Renames internal macros to be public (_TU_CHECK_MCUTU_MCU_IS_EQUAL, _OSAL_Q_NAMEOSAL_Q_NAME, _TU_ARGS_APPLY_*TU_ARGS_APPLY_*)
  • Reorganizes <stdio.h> includes to centralize them in appropriate header files
  • Standardizes conditional checks and type casting for consistency

Reviewed Changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/tusb_option.h Renames internal macro _TU_CHECK_MCU to public TU_MCU_IS_EQUAL, improves clarity
src/portable/ohci/ohci.c Replaces TU_BIN8(111) with hex literal 0x7
src/portable/ehci/ehci.c Replaces TU_BIN8() macros with hex literals for interrupt masks
src/osal/osal_freertos.h Renames _OSAL_Q_NAME to OSAL_Q_NAME, changes !TUP_MCU_MULTIPLE_CORE to explicit == 0 comparison, adds explicit casts
src/common/tusb_verify.h Replaces tu_printf() with TU_LOG1() for assert messages, removes direct stdio.h include
src/common/tusb_types.h Removes unnecessary 'u' suffix from shift expressions, adds space after cast operator
src/common/tusb_debug.h Moves stdio.h include to the default case (not custom printf)
src/common/tusb_compiler.h Renames internal _TU_ARGS_APPLY_* macros to public TU_ARGS_APPLY_*, fixes comment typo, improves comment clarity
src/common/tusb_common.h Removes stdio.h include, casts memset/memcpy results to void, makes tu_log2 condition explicit, removes TU_BIN8 macro definitions, consolidates function braces
src/class/net/ncm_device.c Removes redundant standard header includes
src/class/audio/audio.h Removes unnecessary cast and 'u' suffix from shift expressions in enum
hw/bsp/board_api.h Adds explicit cast for nibble extraction
examples/host/msc_file_explorer/src/msc_app.h Adds stdio.h include
examples/host/msc_file_explorer/src/main.c Removes stdio.h include (moved to header)
examples/host/hid_controller/src/app.h Adds stdio.h include
examples/host/cdc_msc_hid_freertos/src/msc_app.c Adds stdio.h include
examples/host/cdc_msc_hid_freertos/src/app.h Adds stdio.h include
examples/host/cdc_msc_hid/src/msc_app.c Adds stdio.h include
examples/host/cdc_msc_hid/src/hid_app.c Removes blank line
examples/host/cdc_msc_hid/src/app.h Adds stdio.h include
examples/device/cdc_msc/src/main.c Changes !btn_prev to explicit (btn_prev == 0) comparison
examples/device/audio_4_channel_mic/src/usb_descriptors.c Adds static qualifier, improves brace placement, adds explicit cast
.github/workflows/build_util.yml Extracts inline expression to TOOLCHAIN environment variable for better readability

p_qhd->int_smask = (interval == 1) ? TU_BIN8(11111111) :
(interval == 2) ? TU_BIN8(10101010): TU_BIN8(01000100);
p_qhd->int_smask = (interval == 1) ? 0xff : // 0b11111111
(interval == 2) ? 0xaa /* 0b10101010 */ : 0x44 /* 01000100 */;
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Binary representation comment is missing '0b' prefix for the last value. Should be '0b01000100' for consistency with the other binary comments on this line and line 922.

Suggested change
(interval == 2) ? 0xaa /* 0b10101010 */ : 0x44 /* 01000100 */;
(interval == 2) ? 0xaa /* 0b10101010 */ : 0x44 /* 0b01000100 */;

Copilot uses AI. Check for mistakes.
#define _TU_ARGS_APPLY_EXPAND_6(_X, _a1, _a2, _a3, _a4, _a5, _a6) _X(_a1), _TU_ARGS_APPLY_EXPAND_5(_X, _a2, _a3, _a4, _a5, _a6)
#define _TU_ARGS_APPLY_EXPAND_7(_X, _a1, _a2, _a3, _a4, _a5, _a6, _a7) _X(_a1), _TU_ARGS_APPLY_EXPAND_6(_X, _a2, _a3, _a4, _a5, _a6, _a7)
#define _TU_ARGS_APPLY_EXPAND_8(_X, _a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8) _X(_a1), _TU_ARGS_APPLY_EXPAND_7(_X, _a2, _a3, _a4, _a5, _a6, _a7, _a8)
// Apply a macro X to each of the arguments with a selected separation/delimiter
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammatical error: 'with a selected' should be 'with a separator' to match the actual functionality. The macro uses a separator/delimiter parameter, not a 'selected' one.

Suggested change
// Apply a macro X to each of the arguments with a selected separation/delimiter
// Apply a macro X to each of the arguments with a separator/delimiter

Copilot uses AI. Check for mistakes.
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>

Check warning

Code scanning / C-STAT

Use of `stdio.h' is not compliant

Use of `stdio.h' is not compliant
@chatgpt-codex-connector
Copy link

💡 Codex Review

// Standard Headers
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <stddef.h>
#include <string.h>

P1 Badge Keep stdio available for modules using printf

Dropping #include <stdio.h> from the common header means all translation units that relied on tusb_common.h for the printf prototype now compile without it. Many BSP sources still call printf directly (for example hw/bsp/at32f425/family.c around the USART setup) but do not include <stdio.h> themselves. With C99 builds that treat implicit function declarations as errors, those boards will now fail to compile. Either retain the <stdio.h> include here or add explicit includes in each affected file.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

codescene-delta-analysis[bot]

This comment was marked as outdated.

for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
uint_fast16_t ofs_ep = stm->desc.ep[i];
if (!ofs_ep) continue;
if (0 == ofs_ep) {

Check warning

Code scanning / PVS-Studio

Expression '0 == ofs_ep' is always false. Warning

Expression '0 == ofs_ep' is always false.
TU_ATTR_ALWAYS_INLINE static inline
void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t* s, uint32_t xferred_bytes) {
if (tu_fifo_depth(&s->ff)) {
if (0 != tu_fifo_depth(&s->ff)) {

Check warning

Code scanning / C-STAT

The operands `0' and `tu_fifo_depth(&s->ff)' have essential type categories signed 8-bit int and unsigned 16-bit int, which do not match

The operands `0' and `tu_fifo_depth(&s->ff)' have essential type categories signed 8-bit int and unsigned 16-bit int, which do not match
TU_ATTR_ALWAYS_INLINE static inline
void tu_edpt_stream_read_xfer_complete_with_buf(tu_edpt_stream_t* s, const void * buf, uint32_t xferred_bytes) {
if (tu_fifo_depth(&s->ff)) {
if (0 != tu_fifo_depth(&s->ff)) {

Check warning

Code scanning / C-STAT

The operands `0' and `tu_fifo_depth(&s->ff)' have essential type categories signed 8-bit int and unsigned 16-bit int, which do not match

The operands `0' and `tu_fifo_depth(&s->ff)' have essential type categories signed 8-bit int and unsigned 16-bit int, which do not match
codescene-delta-analysis[bot]

This comment was marked as outdated.


if (dwc2->gsnpsid >= DWC2_CORE_REV_3_00a) {
if(dwc2->epout[0].doepctl & DOEPCTL_EPENA) {
if(0 != (dwc2->epout[0].doepctl & DOEPCTL_EPENA)) {
Copy link
Collaborator

@HiFiPhile HiFiPhile Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hathach I don't like adding a redundant 0 != into the condition, can we change the static analyzer behavior ?

I think it's better to change uint32_t board_button_read(void); to bool.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, It is rather annoying, maybe we can suppress that rule. let me check

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some make sense to change to != 0 though like checking on number. For flag with and/or it is indeed unneccessary.

codescene-delta-analysis[bot]

This comment was marked as outdated.

codescene-delta-analysis[bot]

This comment was marked as outdated.

codescene-delta-analysis[bot]

This comment was marked as outdated.

Copy link
Contributor

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C-STAT found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

codescene-delta-analysis[bot]

This comment was marked as outdated.

codescene-delta-analysis[bot]

This comment was marked as outdated.

codescene-delta-analysis[bot]

This comment was marked as outdated.

(void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);

return -1;
return -1; // stall/failed command request;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

Copilot Autofix

AI 3 months ago

To fix the problem, remove the ambiguous comment on line 246, // stall/failed command request;, in examples/device/cdc_msc/src/msc_disk.c. This will help keep the code clean and free of confusing, commented-out code. No further action is required—simply delete the line containing the comment. No imports, variables, or other code changes are needed; just remove the comment.


Suggested changeset 1
examples/device/cdc_msc/src/msc_disk.c

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/examples/device/cdc_msc/src/msc_disk.c b/examples/device/cdc_msc/src/msc_disk.c
--- a/examples/device/cdc_msc/src/msc_disk.c
+++ b/examples/device/cdc_msc/src/msc_disk.c
@@ -243,7 +243,6 @@
   // Set Sense = Invalid Command Operation
   (void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
 
-  return -1; // stall/failed command request;
-}
+  return -1; }
 
 #endif
EOF
@@ -243,7 +243,6 @@
// Set Sense = Invalid Command Operation
(void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);

return -1; // stall/failed command request;
}
return -1; }

#endif
Copilot is powered by AI and may make mistakes. Always verify output.
fix more alerts
disable IAR CStat since pvs-studio check is better integrated with clion
Copy link

@codescene-delta-analysis codescene-delta-analysis bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Health Improved (2 files improve in Code Health)

Gates Failed
Enforce advisory code health rules (1 file with Complex Method)

Gates Passed
3 Quality Gates Passed

See analysis details in CodeScene

Reason for failure
Enforce advisory code health rules Violations Code Health Impact
video_device.c 1 advisory rule 4.06 → 4.05 Suppress
View Improvements
File Code Health Impact Categories Improved
usbd.c 5.14 → 5.15 Complex Method
dcd_dwc2.c 6.65 → 6.70 Complex Method, Overall Code Complexity

Quality Gate Profile: Clean Code Collective
Want more control? Customize Code Health rules or catch issues early with our IDE extension and CLI tool.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 5, 2025

@hathach hathach merged commit 58b4104 into master Nov 5, 2025
192 of 194 checks passed
@hathach hathach deleted the fix-code-alerts branch November 5, 2025 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants