Skip to content

Fix: write barrier bypass in setv()/copyv() for STRSXP/VECSXP targets - #877

Merged
SebKrantz merged 1 commit into
masterfrom
claude/issue-876-20260827-2130
Aug 28, 2026
Merged

Fix: write barrier bypass in setv()/copyv() for STRSXP/VECSXP targets#877
SebKrantz merged 1 commit into
masterfrom
claude/issue-876-20260827-2130

Conversation

@SebKrantz

@SebKrantz SebKrantz commented Aug 28, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #876: setv()/copyv() wrote element pointers directly into the backing array of STRSXP/VECSXP targets in setcopyv() (src/programming.c), bypassing R's generational write barrier. On an old-generation target this can leave an unrecorded reference to a younger CHARSXP/list element, which a subsequent young-generation GC may free while still referenced, causing memory corruption, cryptic CHAR()/SET_STRING_ELT() errors, or segfaults.

  • Use SET_STRING_ELT()/SET_VECTOR_ELT() in the STRSXP/VECSXP write loops instead of raw pointer assignment, so the old-to-new reference is properly recorded.
  • Numeric/logical/raw branches are unchanged.
  • Dropped the omp simd pragma on the boxed-type write path, since the barriered setters are function calls and not safely vectorizable.
  • Added a NEWS.md entry.

Test plan

  • Run full testthat suite (devtools::test())
  • R CMD check
  • Spot-check setv()/copyv() on character and list vectors, including the vind1=TRUE index-vector path

Note: this PR was authored in a sandbox without an R toolchain, so the change has not been compiled or test-run here.

Generated with Claude Code

setcopyv() wrote element pointers directly into STRSXP/VECSXP backing
arrays, skipping R's generational write barrier. On an old-generation
target this can leave an unrecorded reference to a younger CHARSXP/list
element, which a subsequent young-generation GC may free while still
referenced, causing memory corruption, cryptic CHAR()/SET_STRING_ELT()
errors, or segfaults.

Use SET_STRING_ELT()/SET_VECTOR_ELT() for the boxed-type write loops so
the reference is properly recorded, while leaving the numeric/raw
branches unchanged. Also drops the omp simd pragma from the boxed
write paths, since the barriered setters are function calls and are not
safely vectorizable.

Co-authored-by: Sebastian Krantz <48053842+SebKrantz@users.noreply.github.com>
@SebKrantz
SebKrantz merged commit 503f11d into master Aug 28, 2026
6 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.

[BUG] Write barrier in setv/copyv

1 participant