Fix obsolete cnt assignment in _tu_fifo_peek() overflow check#3230
Merged
Conversation
Contributor
Author
|
@hathach 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs. I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review. |
Co-authored-by: hathach <249515+hathach@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Possible logic error with overflow check in _tu_fifo_peek()
Fix obsolete cnt assignment in _tu_fifo_peek() overflow check
Sep 3, 2025
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a logic error in the _tu_fifo_peek() function where a variable assignment in the overflow check was obsolete and never used, as identified by static analysis.
- Removes the unused
cnt = f->depth;assignment from the overflow correction block - Preserves the necessary read index correction functionality
- Aligns the function behavior with its single-item peek purpose
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
hathach
approved these changes
Sep 4, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a logic error identified by static analysis in the
_tu_fifo_peek()function where thecntvariable was being assigned but never used.Issue Analysis:
In the overflow check at lines 427-432 of
src/common/tusb_fifo.c, the code was settingcnt = f->depthbut this value was never used afterward:Comparison with similar functions:
_tu_fifo_peek_n()uses the correctedcntvalue to limit the number of items read:if ( cnt < n ) n = cnt;tu_fifo_get_read_info()uses the correctedcntvalue to set buffer info:info->len_lin = cnt;_tu_fifo_peek()only peeks at a single item and doesn't usecntafter the overflow checkSolution:
Removed the obsolete
cnt = f->depth;assignment while preserving the necessary read index correction (rd_idx = _ff_correct_read_index(f, wr_idx);).Testing:
All existing FIFO tests continue to pass (14/14) and the full unit test suite passes (21/21), confirming the change doesn't affect functionality.
Fixes #3218.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.