Skip to content

protect new cons cell in named StretchyList::push_back() - #1490

Open
kevinushey wants to merge 1 commit into
masterfrom
bugfix/stretchylist-push-back
Open

protect new cons cell in named StretchyList::push_back()#1490
kevinushey wants to merge 1 commit into
masterfrom
bugfix/stretchylist-push-back

Conversation

@kevinushey

@kevinushey kevinushey commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #1489.

The named variant of StretchyList::push_back() allocated the new cons cell before constructing the Symbol for its tag. If the tag name was not yet interned, Rf_install() could allocate and trigger a garbage collection that reclaimed the still-unreachable cell; SET_TAG() then wrote into a reclaimed node, which was subsequently linked into the list. See #1489 for a reproducible example via Rcpp::sourceCpp() and gctorture().

The fix constructs the Symbol before calling Rf_cons(), matching the ordering already used by the named variant of push_front().

Also adds a regression test exercising named push_back() with a freshly generated (never-interned) tag name under gctorture(); the test fails without the header change and passes with it.

Checklist

  • Code compiles correctly
  • R CMD check still passes all tests (checked locally with RunAllRcppTests=yes on R 4.6.1; --no-manual --no-vignettes, so the only flagged items were two vignette-packaging warnings from the local --no-build-vignettes build)
  • Preferably, new tests were added which fail without the change
  • Document the changes by file in ChangeLog

The named variant of push_back() allocated the new cons cell before
constructing the Symbol for its tag. If the tag name was not yet
interned, Rf_install() could allocate and trigger a garbage collection
that reclaimed the still-unreachable cell. Construct the Symbol first,
matching push_front().

Fixes #1489.

@eddelbuettel eddelbuettel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

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.

StretchyList::push_back() can corrupt memory: cons cell unprotected across Rf_install()

2 participants