Skip to content

Fix build-SITL-Mac VLA-folding-constant error in log.c#11679

Open
sensei-hacker wants to merge 2 commits into
iNavFlight:maintenance-10.xfrom
sensei-hacker:fix-sitl-mac-vla-error
Open

Fix build-SITL-Mac VLA-folding-constant error in log.c#11679
sensei-hacker wants to merge 2 commits into
iNavFlight:maintenance-10.xfrom
sensei-hacker:fix-sitl-mac-vla-error

Conversation

@sensei-hacker

Copy link
Copy Markdown
Member

Summary

Fixes the build-SITL-Mac CI failure on maintenance-10.x: AppleClang errors on src/main/common/log.c:210 under -Werror -Wgnu-folding-constant.

Root Cause

_logBufferHex() sizes a stack buffer with:

const size_t charsPerByte = 5;
const size_t maxBytes = 8;
char buf[LOG_PREFIX_FORMATTED_SIZE + charsPerByte * maxBytes + 1];

In C (unlike C++), a const-qualified object is not an integer constant expression. So this array size is technically a variable-length array. Compilers have long tolerated this via a GNU extension that folds it back to a fixed size when the value happens to be known — but a newer AppleClang on the macOS CI runner now warns (and -Werror promotes to error) when code relies on that extension.

Fix

Replace the two const size_t locals with a local enum:

enum {
    charsPerByte = 5,
    maxBytes = 8,
};

Enum constants are genuine integer constant expressions in C (C11 6.6p6), so the buffer size expression is now a real compile-time constant on every compiler — not just one that happens to fold it. This removes the VLA classification at the root rather than suppressing the warning.

No behavior change: buffer size is 13 + 5*8 + 1 = 54 bytes both before and after.

Testing

  • Full clean SITL build (Linux/GCC) succeeds with no warnings or errors in log.c.
  • Verified via minimal reproduction that const size_t locals vs. enum constants differ in constant-expression classification per the C standard (C11 6.6p6 explicitly lists enumeration constants as valid ICE operands, and excludes const-qualified objects).
  • build-SITL-Mac CI leg on this PR will provide the authoritative confirmation against the actual AppleClang toolchain that originally reported the error.
  • No log output formatting change — only the kind of constant used to compute a buffer size changed, not its value.

Code Review

Reviewed with inav-code-review agent — no critical or important issues found. Approved.

Fixes the build-SITL-Mac failure reported against maintenance-10.x (see PR #11678 CI for original occurrence).

_logBufferHex() sized a stack buffer using two `const size_t` locals
(charsPerByte, maxBytes). In C, const-qualified objects are not integer
constant expressions, so the array size expression was technically a
variable-length array; recent AppleClang treats reliance on its
extension that folds such expressions back to a constant as an error
under -Werror -Wgnu-folding-constant, breaking build-SITL-Mac.

Replace the const locals with a local enum, whose members are genuine
integer constant expressions in C. The buffer size and behavior are
unchanged (13 + 5*8 + 1 = 54 bytes either way).
@qodo-code-review

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

Fix AppleClang VLA folding-constant build error in _logBufferHex()

🐞 Bug fix 🕐 10-20 Minutes

Grey Divider

AI Description

• Fix macOS SITL build failure caused by AppleClang treating a non-constant array size as a VLA
• Replace const size_t locals with enum constants so the stack buffer size is a true ICE in C
• Preserve behavior and buffer size while eliminating reliance on GNU constant-folding extensions
Diagram

graph TD
  A["build-SITL-Mac CI"] --> B["AppleClang (-Werror)"] --> C["src/main/common/log.c"] --> D["_logBufferHex()"] --> E["ICE-sized stack buffer"] --> F["Build succeeds"]
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Use preprocessor macros (#define) for constants
  • ➕ Guaranteed compile-time constants in C
  • ➕ Clear intent for fixed sizes
  • ➖ Pollutes macro namespace
  • ➖ Harder to scope to a single function without naming ceremony
2. Use a file-scope enum/const for shared reuse
  • ➕ Avoids repeating constants if used elsewhere
  • ➕ Still provides true compile-time constants (enum)
  • ➖ Unnecessary wider scope for a purely local formatting detail
  • ➖ Slightly increases global surface area
3. Suppress/relax the warning on macOS build
  • ➕ Minimal code churn
  • ➖ Masks a real standards issue (VLA classification)
  • ➖ Build flags become toolchain-specific and harder to maintain

Recommendation: The PR’s local enum approach is the best trade-off: it keeps the constants scoped to the function, produces a true integer constant expression in C on all compilers, and avoids weakening warnings or introducing global macros.

Files changed (1) +6 / -2

Bug fix (1) +6 / -2
log.cMake _logBufferHex() buffer size a true C constant expression +6/-2

Make _logBufferHex() buffer size a true C constant expression

• Replaces two 'const size_t' locals used in an array bound with local 'enum' constants so the bound is a true integer constant expression in C. This avoids AppleClang treating the array as a VLA under '-Wgnu-folding-constant' + '-Werror', while keeping the buffer size and behavior unchanged.

src/main/common/log.c

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Qodo Logo

STATIC_ASSERT expands to a typedef of a char array sized by the
condition, a common compile-time-assertion trick. This call site's
condition compared GPS_DEGREES_DIVIDER (already 10000000L) against the
floating-point literal 1e7. Per C11 6.6p6, integer constant expressions
may not contain floating constants, so despite being trivially
foldable, AppleClang's -Wgnu-folding-constant -Werror rejects it.

Replace 1e7 with 10000000L to match GPS_DEGREES_DIVIDER's own literal
form, making the condition a pure integer constant expression. No
change to the assertion's truth value or any runtime behavior.
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Test firmware build ready — commit b13c96a

Download firmware for PR #11679

238 targets built. Find your board's .hex file by name on that page (e.g. MATEKF405SE.hex). Files are individually downloadable — no GitHub login required.

Development build for testing only. Use Full Chip Erase when flashing.

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.

1 participant