Skip to content

cgen: guard coverage counter file creation#27075

Open
guweigang wants to merge 1 commit into
vlang:masterfrom
guweigang:codex/coverage-fopen-guard
Open

cgen: guard coverage counter file creation#27075
guweigang wants to merge 1 commit into
vlang:masterfrom
guweigang:codex/coverage-fopen-guard

Conversation

@guweigang
Copy link
Copy Markdown
Contributor

Summary

Guard coverage counter file creation before writing coverage rows.

Root cause

The generated vprint_coverage_stats() opened the counter CSV with fopen(...) and immediately passed the result to fprintf(...). If the coverage directory was not writable or was otherwise invalid, fopen could return NULL, causing a segfault during _vcleanup().

This also fixes a typo in the generated non-Windows timestamp assignment, where ts.tv_nsec was assigned to secs instead of nsecs.

Changes

  • Return early from generated vprint_coverage_stats() when the counter file cannot be opened.
  • Generate nsecs = ts.tv_nsec; for the monotonic timestamp nanoseconds field.
  • Add a C output regression test for the generated guard and timestamp assignment.

Validation

  • ./v -o ./v4 ./cmd/v
  • ./v4 -cov /private/tmp/vcov_as_file vlib/v/tests/fns/closure_struct_init_cov_regression_test.v
  • ./v4 -cov /private/tmp/vcov_ok vlib/v/tests/fns/closure_struct_init_cov_regression_test.v
  • ./v4 test vlib/v/gen/c/coutput_test.v
  • git diff --check

@guweigang guweigang marked this pull request as ready for review May 3, 2026 11:26
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