Skip to content

Commit 18ef60e

Browse files
Another PROTECT(getAttrib(.)), UNPROTECT() early returns
1 parent 0358f99 commit 18ef60e

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/assign.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ static int _selfrefok(SEXP x, Rboolean checkNames, Rboolean verbose) {
121121
p = R_ExternalPtrAddr(v);
122122
if (p==NULL) {
123123
if (verbose) Rprintf(_(".internal.selfref ptr is NULL. This is expected and normal for a data.table loaded from disk. Please remember to always setDT() immediately after loading to prevent unexpected behavior. If this table was not loaded from disk or you've already run setDT(), please report to data.table issue tracker.\n"));
124+
UNPROTECT(nprotect);
124125
return -1;
125126
}
126127
if (!isNull(p)) internal_error(__func__, ".internal.selfref ptr is neither NULL nor R_NilValue"); // # nocov
@@ -132,8 +133,10 @@ static int _selfrefok(SEXP x, Rboolean checkNames, Rboolean verbose) {
132133
// R copied this vector not data.table; it's not actually over-allocated. It looks over-allocated
133134
// because R copies the original vector's tl over despite allocating length.
134135
prot = R_ExternalPtrProtected(v);
135-
if (TYPEOF(prot) != EXTPTRSXP) // Very rare. Was error(_(".internal.selfref prot is not itself an extptr")).
136+
if (TYPEOF(prot) != EXTPTRSXP) { // Very rare. Was error(_(".internal.selfref prot is not itself an extptr")).
137+
UNPROTECT(nprotect);
136138
return 0; // # nocov ; see http://stackoverflow.com/questions/15342227/getting-a-random-internal-selfref-error-in-data-table-for-r
139+
}
137140
if (x!=R_ExternalPtrAddr(prot) && !ALTREP(x))
138141
SET_TRUELENGTH(x, LENGTH(x)); // R copied this vector not data.table, it's not actually over-allocated
139142
int ok = checkNames ? names==tag : x==R_ExternalPtrAddr(prot);

src/subset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ SEXP subsetDT(SEXP x, SEXP rows, SEXP cols) { // API change needs update NEWS.md
345345
// clear any index that was copied over by copyMostAttrib() above, e.g. #1760 and #1734 (test 1678)
346346
setAttrib(ans, sym_index, R_NilValue);
347347
// but maintain key if ordered subset
348-
SEXP key = getAttrib(x, sym_sorted);
348+
SEXP key = PROTECT(getAttrib(x, sym_sorted)); nprotect++;
349349
if (length(key)) {
350350
SEXP ans_names = PROTECT(getAttrib(ans, R_NamesSymbol)); nprotect++;
351351
SEXP in = PROTECT(chin(key, ans_names)); nprotect++;

0 commit comments

Comments
 (0)