Skip to content

Fix C frontend array type sync in goto program#8799

Open
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:fix-5022-array-length
Open

Fix C frontend array type sync in goto program#8799
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:fix-5022-array-length

Conversation

@tautschnig
Copy link
Copy Markdown
Collaborator

Update goto program expressions to use updated array type from symbol table when arrays declared without length are later defined per C standard 6.9.2

Co-authored-by: Kiro autonomous agent

Fixes: #5022

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

@tautschnig tautschnig self-assigned this Feb 24, 2026
When arrays are declared without a length at file scope (e.g.,
int A[];), C standard 6.9.2 paragraph 5 makes them arrays of size 1 as
tentative definitions. The static_lifetime_init function updates the
symbol table accordingly, but the function body expressions (symbol
values) still reference the old incomplete array type with nil size.
When goto_convert later builds the goto program from these expressions,
the stale types propagate into the goto program.

Add a targeted pass in static_lifetime_init that, after processing all
symbols, walks function bodies in the symbol table and updates
symbol_exprt nodes whose type is an incomplete array (nil size) to use
the completed array type. This ensures goto_convert produces goto
programs with consistent types, eliminating the need for the previous
workaround in field_sensitivity.cpp that looked up the symbol table at
symex time.

Fixes: diffblue#5022

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig tautschnig force-pushed the fix-5022-array-length branch from 673ef5f to 2b52ffe Compare March 17, 2026 19:01
@tautschnig tautschnig marked this pull request as ready for review March 17, 2026 20:00
Copilot AI review requested due to automatic review settings March 17, 2026 20:00
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a type inconsistency between the symbol table and function bodies in the generated goto program when C tentative array definitions (e.g., int A[];) are later completed with a concrete size per C standard 6.9.2.

Changes:

  • Propagates completed array types from the symbol table into function body expressions before goto conversion.
  • Removes a now-redundant field-sensitivity fallback that attempted to recover missing array sizes from the symbol table during symex.
  • Adds a regression test covering tentative array definition completion and use across functions.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/linking/static_lifetime_init.cpp Updates symbol expressions inside function bodies to use completed array types from the symbol table.
src/goto-symex/field_sensitivity.cpp Removes a symbol-table lookup fallback for incomplete array sizes when computing field-sensitivity.
regression/cbmc/Tentative_definition_array1/test.desc Adds expected-output specification for the new regression test.
regression/cbmc/Tentative_definition_array1/main.c New C test reproducing tentative array completion behavior and validating consistent typing in goto program.

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

Comment on lines +168 to +170
for(const std::string &id : symbols)
{
symbolt &symbol = symbol_table.get_writeable_ref(id);
Comment on lines +174 to +191
symbol.value.visit_pre(
[&symbol_table](exprt &expr)
{
if(expr.id() != ID_symbol)
return;
if(expr.type().id() != ID_array)
return;
if(!to_array_type(expr.type()).size().is_nil())
return;
const auto *sym =
symbol_table.lookup(to_symbol_expr(expr).get_identifier());
if(
sym != nullptr && sym->type.id() == ID_array &&
!to_array_type(sym->type).size().is_nil())
{
expr.type() = sym->type;
}
});
@@ -215,15 +215,6 @@ exprt field_sensitivityt::apply(
bool was_l2 = !tmp.get_level_2().empty();
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.41%. Comparing base (85c204f) to head (2b52ffe).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8799      +/-   ##
===========================================
- Coverage    80.41%   80.41%   -0.01%     
===========================================
  Files         1703     1703              
  Lines       188398   188408      +10     
  Branches        73       73              
===========================================
+ Hits        151498   151503       +5     
- Misses       36900    36905       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

C frontend issue with arrays that are declared without a length

2 participants