Skip to content

[406] Fix format specifier mismatch in ensureNonNegative - #426

Merged
Alomir merged 2 commits into
PecanProject:masterfrom
Labreo:406-fix-format-string-ensure-non-negative
Oct 28, 2026
Merged

Alomir merged 2 commits into
PecanProject:masterfrom
Labreo:406-fix-format-string-ensure-non-negative

Conversation

@Labreo

@Labreo Labreo commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Summary

  • What: Restore (value %8.5f) to ensureNonNegative() error logging and add printf format attribute validation to logprint(). Fix compiler-flagged format errors in limitations.c and modelParams.c.
  • Motivation: In ensureNonNegative(), the error log format string omitted the stock value while keeping *var in the argument list. This caused argument type mismatch and undefined behavior. Adding compile-time format validation prevents format regressions across the codebase.

How was this change tested?

Ran local unit tests and smoke tests:

  • make clean && make
  • make testbuild && make unit
  • make smoke
  • make test

Updated testOutputHeader.c to test clamping and violation error exits.

Reproduction steps

Set plantWoodInit to -10.0 in tests/smoke/niwot/sipnet.param and run sipnet. Prior to this fix, the error log prints corrupted timestamp values due to argument register offsets.

Related issues

Checklist

  • Related issues are listed above. PRs without an approved, related issue may not get reviewed.
  • PR title has the issue number in it ("[#] ")
  • Tests added/updated for new features (if applicable)
  • Documentation updated (if applicable)
  • docs/CHANGELOG.md updated with noteworthy changes
  • Code formatted with clang-format (run git clang-format if needed)

Restore the stock value conversion specifier in ensureNonNegative error logging. This change prevents argument offset errors and undefined behavior.

Add format checking attributes to logprint in src/common/logging.h. Resolve latent format specifier errors identified across the codebase. Add regression assertions in testOutputHeader to verify clamping and error exits.
Copilot AI balanced review requested due to automatic review settings October 28, 2026 09:47

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Store test climate data in static storage and reset climate pointer to NULL before returning.

@Alomir Alomir left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Very nice, great addition of __attribute__

Comment thread src/common/logging.h
Comment on lines +34 to +39
#if defined(__GNUC__) || defined(__clang__)
#define ATTRIB_PRINTF(fmt_idx, arg_idx) \
__attribute__((format(printf, fmt_idx, arg_idx)))
#else
#define ATTRIB_PRINTF(fmt_idx, arg_idx)
#endif

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice!

@Alomir
Alomir merged commit e8bf882 into PecanProject:master Oct 28, 2026
12 checks passed
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.

[BUG] Format string specifier mismatch in ensureNonNegative() causes undefined behavior

3 participants