Skip to content

Commit eb8fc97

Browse files
author
luke
committed
Safer duplicate for STRSXPs without STRING_PTR.
git-svn-id: https://svn.r-project.org/R/trunk@89005 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 4cd643d commit eb8fc97

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/main/duplicate.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,19 @@ static SEXP duplicate1(SEXP s, Rboolean deep)
348348
case CPLXSXP: DUPLICATE_ATOMIC_VECTOR(Rcomplex, COMPLEX, COMPLEX_RO, t, s, deep); break;
349349
case RAWSXP: DUPLICATE_ATOMIC_VECTOR(Rbyte, RAW, RAW_RO, t, s, deep); break;
350350
case STRSXP:
351-
/* direct copying and bypassing the write barrier is OK since
352-
t was just allocated and so it cannot be older than any of
353-
the elements in s. LT */
354-
DUPLICATE_ATOMIC_VECTOR(SEXP, STRING_PTR, STRING_PTR_RO, t, s, deep);
351+
/* Direct copying and bypassing the write barrier would be OK
352+
since t was just allocated and so it cannot be older than
353+
any of the elements in s. But it does not increment the
354+
reference counts, so use a loop with SET_STRING_ELT. LT */
355+
//DUPLICATE_ATOMIC_VECTOR(SEXP, STRING_PTR, STRING_PTR_RO, t, s, deep);
356+
n = XLENGTH(s);
357+
PROTECT(s);
358+
PROTECT(t = allocVector(TYPEOF(s), n));
359+
for(i = 0 ; i < n ; i++)
360+
SET_STRING_ELT(t, i, STRING_ELT(s, i));
361+
DUPLICATE_ATTRIB(t, s, deep);
362+
COPY_TRUELENGTH(t, s);
363+
UNPROTECT(2);
355364
break;
356365
case PROMSXP:
357366
return s;

0 commit comments

Comments
 (0)