Skip to content

fix: widen avail-objects count to avoid truncation#444

Draft
MarkAtwood wants to merge 1 commit into
wolfSSL:mainfrom
MarkAtwood:fix/nvm-avail-count-truncation
Draft

fix: widen avail-objects count to avoid truncation#444
MarkAtwood wants to merge 1 commit into
wolfSSL:mainfrom
MarkAtwood:fix/nvm-avail-count-truncation

Conversation

@MarkAtwood

@MarkAtwood MarkAtwood commented Jul 9, 2026

Copy link
Copy Markdown

Bug: In wh_NvmFlashLog_GetAvailable (src/wh_nvm_flash_log.c), the local count was declared uint8_t but nfl_ObjectCount() returns int. The assignment count = nfl_ObjectCount(ctx, NULL); narrows int -> uint8_t. whNvmId is uint16_t and WOLFHSM_CFG_NVM_OBJECT_COUNT is user-overridable (default 32), so a build configured with >255 objects truncates the count. Example: 256 objects -> count wraps to 0 -> *out_avail_objects = WOLFHSM_CFG_NVM_OBJECT_COUNT - count reports 256 available when 0 remain. The sibling call in the same file already uses a wider type for the same function, confirming this narrowing is unintentional.

Fix: declare count as int to match nfl_ObjectCount()'s return type. One-line change.

Build-verified: compiled the translation unit with -Wconversion (with WOLFHSM_CFG_SERVER_NVM_FLASH_LOG defined, against a wolfSSL install). Before the fix, gcc warned conversion from 'int' to 'uint8_t' ... may change value at the count = assignment. After the fix: gcc exit 0, that warning is gone, object file and wh_NvmFlashLog_GetAvailable symbol produced.

Reported by static analysis (Fenrir finding 388).

[fenrir-sweep:held]

Copilot AI review requested due to automatic review settings July 9, 2026 23:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot couldn't run its full agentic review because no GitHub Actions runner was available. Make sure your repository has a runner available to run Copilot's review, or add a copilot-setup-steps.yml file specifying one with the runs-on attribute. See the docs for more details.

Fixes a narrowing conversion bug in wh_NvmFlashLog_GetAvailable where the object count could truncate/wrap when WOLFHSM_CFG_NVM_OBJECT_COUNT exceeds 255.

Changes:

  • Widened the local count variable from uint8_t to int to match nfl_ObjectCount()’s return type.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@MarkAtwood MarkAtwood assigned MarkAtwood and unassigned wolfSSL-Bot Jul 10, 2026
@MarkAtwood MarkAtwood marked this pull request as draft July 10, 2026 16:48
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.

3 participants