Repository navigation
Conversation
The C backend incorrectly emitted top-level const array declarations
as pointers (const T* name) instead of arrays (const T name[N]),
causing segmentation faults when indexing.
This was because flowc_cgen_emit_type returns T* for array<T, N> types
(as documented in line 2717-2718 comment), and struct fields handle
the [N] suffix separately. Added similar logic to flowc_cgen_emit_const
to emit the element type and append [N] after the identifier.
Fixes indexing of const arrays like:
const NOTES: [i32; 4] = [36, 38, 42, 46]
Before: static const int32_t* NOTES = { 36, 38, 42, 46 };
After: static const int32_t NOTES[4] = { 36, 38, 42, 46 };
Tested with i32, f32, f64, u64, struct elements and module imports.
Co-authored-by: Abhishek Shivakumar <abhishek.shivakumar@gmail.com>
Co-authored-by: Abhishek Shivakumar <abhishek.shivakumar@gmail.com>
Tier 1 · macos-15 was cancelled without acquiring a hosted runner (no steps ran). Empty commit to restart the workflow. Co-authored-by: Abhishek Shivakumar <abhishek.shivakumar@gmail.com>
Co-authored-by: Abhishek Shivakumar <abhishek.shivakumar@gmail.com>
This branch has not been deployed
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.
Summary
Fixed a bug in flowc's C backend where top-level const array declarations were incorrectly emitted as pointers instead of arrays, causing segmentation faults when indexing.
Problem
Given a Flow program:
The C backend emitted:
This initializes a pointer to address
0x24(the integer 36), so indexingNOTES[2]causes a segfault.Solution
Modified
flowc_cgen_emit_constincompiler/src/cgen.flowto:[N]suffix after the identifierGenerated C: Before and After
Before:
After:
Testing
Added comprehensive regression tests:
test_const_array_i32.flow- i32 array indexingtest_const_array_f32.flow- f32 element typetest_const_array_f64.flow- f64 element typetest_const_array_u64.flow- u64 element typetest_const_array_struct.flow- struct element typetest_const_array_import.flow- exported const arrays through module importsAll tests compile the generated C and verify correct indexing behavior.
Verification
./flow test-lang)./compiler/scripts/bootstrap_from_c.sh --verify)./flow tool compiler/scripts/self_host_full.flow)./flow tool compiler/scripts/roundtrip.flow)Related
Found in flooooooooooow/flow-audio#61 where 808 and piano asset patches crashed with valgrind showing invalid read at address 0x24.