Skip to content

Fix build-SITL-Mac VLA-folding-constant error in log.c (9.1 backport)#11680

Merged
sensei-hacker merged 2 commits into
iNavFlight:release/9.1from
sensei-hacker:fix-sitl-mac-vla-error-9.1
Jul 2, 2026
Merged

Fix build-SITL-Mac VLA-folding-constant error in log.c (9.1 backport)#11680
sensei-hacker merged 2 commits into
iNavFlight:release/9.1from
sensei-hacker:fix-sitl-mac-vla-error-9.1

Conversation

@sensei-hacker

Copy link
Copy Markdown
Member

Summary

Backport of #11679 to release/9.1. Fixes the same -Wgnu-folding-constant AppleClang build failure in src/main/common/log.c (_logBufferHex()), which exists identically on this branch.

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. AppleClang on the macOS CI runner now warns (and -Werror promotes to error) when relying on the GNU extension that folds it back to a fixed size.

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 this is a real fix rather than a suppression. No behavior change: buffer size is 13 + 5*8 + 1 = 54 bytes both before and after.

Testing

  • Clean cherry-pick of the already-reviewed maintenance-10.x fix (Fix build-SITL-Mac VLA-folding-constant error in log.c #11679) onto release/9.1 — identical diff, no conflicts.
  • Full clean SITL build (Linux/GCC) succeeds with no warnings or errors in log.c.
  • build-SITL-Mac CI leg on this PR will provide the authoritative confirmation against the actual AppleClang toolchain.
  • No log output formatting change.

Code Review

Same fix already reviewed with inav-code-review agent on #11679 — no critical or important issues found. Approved.

Companion PR (maintenance-10.x): #11679

_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 warning in log.c (9.1 backport)

🐞 Bug fix 🕐 Less than 10 minutes

Grey Divider

AI Description

• Fix AppleClang -Wgnu-folding-constant build failure in _logBufferHex().
• Replace const size_t locals with enum constants for true compile-time sizing.
• Keep buffer size and log formatting behavior unchanged.
Diagram

graph TD
  A["build-SITL-Mac (AppleClang)"] --> B["Compile C sources"] --> C["src/main/common/log.c"] --> D["_logBufferHex()"] --> E["buf[] sized via enum"]
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Use preprocessor macros for constants
  • ➕ Guaranteed compile-time constants in C
  • ➕ Can be reused across functions/files if needed
  • ➖ Pollutes global namespace
  • ➖ Less scoped/typed than a local enum
2. Hardcode the final buffer size (e.g., 54)
  • ➕ Simplest possible compile-time array bound
  • ➕ No reliance on constant-expression rules
  • ➖ Less self-documenting; intent (5 chars/byte, 8 bytes) is obscured
  • ➖ More error-prone if formatting rules change
3. Move constants to file-scope enum/static constants
  • ➕ Still compile-time constant if enum at file scope
  • ➕ Avoids repeating constants in multiple functions
  • ➖ Wider scope than necessary for values used only in one function
  • ➖ Static const at file scope still isn’t an ICE in C for array bounds

Recommendation: Keep the PR’s local enum approach: it is the most correct C fix (true integer constant expressions) while staying narrowly scoped and preserving readability and behavior. Macros and hardcoded sizes are viable but either broaden scope or reduce clarity.

Files changed (1) +6 / -2

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

Make _logBufferHex() stack buffer size a true constant expression

• Replaces two 'const size_t' locals used in an array bound with a local 'enum' so AppleClang no longer treats the buffer as a VLA under -Wgnu-folding-constant. Adds a clarifying comment and preserves the exact buffer sizing and output behavior.

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 b757e48

Download firmware for PR #11680

240 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.

@sensei-hacker sensei-hacker merged commit 9426576 into iNavFlight:release/9.1 Jul 2, 2026
23 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.

1 participant