Skip to content
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

CodeQL -- Actual fixes #2720

Merged
merged 12 commits into from
Jan 15, 2025
Merged

Conversation

henrygab
Copy link
Contributor

@henrygab henrygab commented Jan 15, 2025

Many of the CodeQL bugs of type "Comparison between A of TypeA and B of wider type TypeB" were caused by having arithmetic within a for() loop's comparison clause. Not only were the operations redundant, but they could cause unintended consequences (e.g., integer promotion rules). Relatively simple to fix (use larger of two sizes for loop variable, compare unsigned vs. unsigned, etc.).

The other reason for many of these was that spiffs, in the definition of struct spiffs_t, defined field block_count of type uint32_t, but defined field free_cursor_block_ix to be of type spiffs_block_ix. In PM3, spiffs_block_ix is defined as uint16_t. Thus, a variable of type spiffs_block_ix cannot directly loop against block_count (potential infinite loops).

Inline "conversation"s added below, both to explain some changes, and to mark code that should have a second set of eyes review.

Code was previously performing arithmetic in
various loop check conditions.  Integer promotion rules could cause unintended comparisons.

`spiffs` defined `fs->block_count` as `uint32_t`, but defined `spiffs_page_ix` as `uint16_t`.  Various overflow checks detected by CodeQL and fixed by checking for those conditions before looping.
The compiler warning is incorrect.
Since `calloc()` zero's memory, can
remove redundant line setting value
to zero, giving quieter builds.
@@ -535,26 +535,61 @@ static s32_t spiffs_page_consistency_check_i(spiffs *fs) {

s32_t res = SPIFFS_OK;
spiffs_page_ix pix_offset = 0;
// Avoid arithmetic in loop conditions (integer promotion rules can cause unintended consequences)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please have second set of eyes review these checks.

armsrc/thinfilm.c Show resolved Hide resolved
client/src/cmdhf14a.c Show resolved Hide resolved
client/src/cmdhfmf.c Show resolved Hide resolved
client/src/cmdlfem410x.c Show resolved Hide resolved
This is supported for GCC >= version 13

See GCC bug 85487:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85487
armsrc/spiffs_check.c Outdated Show resolved Hide resolved
armsrc/thinfilm.c Outdated Show resolved Hide resolved
client/src/cmdhfmf.c Outdated Show resolved Hide resolved
armsrc/spiffs_check.c Show resolved Hide resolved
@iceman1001
Copy link
Collaborator

So we stuck at that stupid spiffs_page_ix again. Can we use that instead of u16?

@iceman1001 iceman1001 merged commit 7082302 into RfidResearchGroup:master Jan 15, 2025
10 of 11 checks passed
@henrygab henrygab deleted the codeQL_fixes2 branch January 15, 2025 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants