-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issues found using default CodeQL settings. #2923
base: master
Are you sure you want to change the base?
Conversation
When relying projects enable CodeQL, they will by default see ten (10x) issues of the following type: `Comparison of narrow type with wide type in loop condition` The type of `TOTAL_DRIVER_COUNT` is `int` (`uint8_t` + `size_t` --> `int`) The type of the loop variable was `uint8_t`. The loop is gated by a check of `(uint8_t)i < TOTAL_DRIVER_COUNT`. As a result, a high severity alert is generated by CodeQL. This fix does the following: 1. Fix the CodeQL alert by changing the loop variable type to use `int`. 2. Explicitly limiting the loop to MAXIMUM_TOTAL_DRIVER_COUNT. 3. Sets the originally named variable to the loop variable (but c-style cast to type `uint8_t`). In addition, because there was not previously any code to ensure that `TOTAL_DRIVER_COUNT` would be in the expected range of `[0x01..0xFF]`, added a `TU_ASSERT()` at the appropriate location to ensure this condition.
@@ -544,7 +556,8 @@ | |||
} | |||
|
|||
static void configuration_reset(uint8_t rhport) { | |||
for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) { | |||
for (int q = 0; (q < TOTAL_DRIVER_COUNT) && (q <= MAXIMUM_TOTAL_DRIVER_COUNT); q++) { |
Check warning
Code scanning / CodeQL
Comparison result is always the same Warning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Belt and suspenders: Leave in the check for (q <= MAXIMUM_TOTAL_DRIVER_COUNT)
... this is not a bug.
@@ -1193,7 +1207,8 @@ | |||
|
|||
case DCD_EVENT_SOF: | |||
// SOF driver handler in ISR context | |||
for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) { | |||
for (int q = 0; (q < TOTAL_DRIVER_COUNT) && (q <= MAXIMUM_TOTAL_DRIVER_COUNT); q++) { |
Check warning
Code scanning / CodeQL
Comparison result is always the same Warning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Belt and suspenders: Leave in the check for (q <= MAXIMUM_TOTAL_DRIVER_COUNT)
... this is not a bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you for your PR and patient, I was off for TET (Lunar New Year). I think we may just cast TOTAL_DRIVER_COUNT to uint8_t instead. To ensure TOTAL_DRIVER_COUNT is less than 255, we can do an TU_ASSERT() in tud_init() so that we only run the check once.
#define TOTAL_DRIVER_COUNT ((uint8_t) (_app_driver_count + BUILTIN_DRIVER_COUNT))
Welcome back, and I hope your time off was all that you hoped!
Could you help me understand the proposed assertion, which I understand to be: If I understand correctly, if |
I made the change proposal for the idea here #2987 . We check/assert when app driver count is intialized and give up if total driver count overflows 8-bit. The rest should function as it is. Let me know if this makes sense to you |
It's been a couple months since I worked on this. If I understand your PR, you are trying to explicitly keep that macro to type If this reduces the CodeQL warnings, then I'm OK with your alternate method, and you can close this PR. |
thanks, I am always working on something and got pending PR. the |
Describe the PR
This fix does the following:
int
.uint8_t
).In addition, because there was not previously any code to ensure that
TOTAL_DRIVER_COUNT
would be in the expected range of[0x01..0xFF]
, added aTU_ASSERT()
at the appropriate location to ensure this condition, and immediately stop execution if a configuration would ever exceed this.There is no change to the logical flow, except for that additional
TU_ASSERT()
.What is solved
When relying projects enable CodeQL (with default settings), and build tinyUSB,
ten (10x) high-severity alerts of the following type will be generated:
Comparison of narrow type with wide type in loop condition
This PR updates the code to prevent these CodeQL alerts,
which otherwise show up in all projects relying on TinyUSB.
Additional context
The alerts note that the loop variable (e.g.,
i
) is of a smaller size (e.g.,uint8_t
) than what it is being compared against (e.g.,int
). As an example, code which would generate this alert is:The type of
TOTAL_DRIVER_COUNT
isint
(uint8_t
+size_t
--> promoted toint
).The type of the loop variable was
uint8_t
.Therefore, this would loop infinitely if
TOTAL_DRIVER_COUNT
ever exceeds 0xFFu.As a result, a high severity alert is generated by CodeQL.
It is understood that the count of drivers is low (built-in is only ~12 right now), and unlikely to ever exceed 0xFFu.
However, there was no code that ensured that
TOTAL_DRIVER_COUNT
would be in the expected range of[ 0x01u .. 0xFFu ]
.