Skip to content

Commit

Permalink
Add missing protects
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Oct 17, 2017
1 parent da39c27 commit 8e6098a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
13 changes: 8 additions & 5 deletions src/flatten.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SEXP flatten_impl(SEXP x) {
// Determine output size and check type
int n = 0;
int has_names = 0;
SEXP x_names = Rf_getAttrib(x, R_NamesSymbol);
SEXP x_names = PROTECT(Rf_getAttrib(x, R_NamesSymbol));

for (int j = 0; j < m; ++j) {
SEXP x_j = VECTOR_ELT(x, j);
Expand All @@ -40,14 +40,13 @@ SEXP flatten_impl(SEXP x) {
SEXP names = PROTECT(Rf_allocVector(STRSXP, n));
if (has_names)
Rf_setAttrib(out, R_NamesSymbol, names);
UNPROTECT(1);

int i = 0;
for (int j = 0; j < m; ++j) {
SEXP x_j = VECTOR_ELT(x, j);
int n_j = Rf_length(x_j);

SEXP names_j = Rf_getAttrib(x_j, R_NamesSymbol);
SEXP names_j = PROTECT(Rf_getAttrib(x_j, R_NamesSymbol));
int has_names_j = !Rf_isNull(names_j);

for (int k = 0; k < n_j; ++k, ++i) {
Expand All @@ -69,12 +68,14 @@ SEXP flatten_impl(SEXP x) {
}
if (i % 1000 == 0)
R_CheckUserInterrupt();

}
UNPROTECT(1);
}



UNPROTECT(1);
UNPROTECT(3);
return out;
}

Expand Down Expand Up @@ -108,7 +109,7 @@ SEXP vflatten_impl(SEXP x, SEXP type_) {
SEXP x_j = VECTOR_ELT(x, j);
int n_j = Rf_length(x_j);

SEXP names_j = Rf_getAttrib(x_j, R_NamesSymbol);
SEXP names_j = PROTECT(Rf_getAttrib(x_j, R_NamesSymbol));
int has_names_j = !Rf_isNull(names_j);

for (int k = 0; k < n_j; ++k, ++i) {
Expand All @@ -119,6 +120,8 @@ SEXP vflatten_impl(SEXP x, SEXP type_) {
if (i % 1000 == 0)
R_CheckUserInterrupt();
}

UNPROTECT(1);
}

UNPROTECT(1);
Expand Down
22 changes: 12 additions & 10 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ SEXP call_loop(SEXP env, SEXP call, int n, SEXPTYPE type, int force_args) {
SEXP i_val = PROTECT(Rf_ScalarInteger(1));
SEXP i = Rf_install("i");
Rf_defineVar(i, i_val, env);
UNPROTECT(1);

SEXP out = PROTECT(Rf_allocVector(type, n));
for (int i = 0; i < n; ++i) {
Expand All @@ -31,17 +30,18 @@ SEXP call_loop(SEXP env, SEXP call, int n, SEXPTYPE type, int force_args) {
INTEGER(i_val)[0] = i + 1;

#if defined(R_VERSION) && R_VERSION >= R_Version(3, 2, 3)
SEXP res = R_forceAndCall(call, force_args, env);
SEXP res = PROTECT(R_forceAndCall(call, force_args, env));
#else
SEXP res = Rf_eval(call, env);
SEXP res = PROTECT(Rf_eval(call, env));
#endif
if (type != VECSXP && Rf_length(res) != 1)
Rf_errorcall(R_NilValue, "Result %i is not a length 1 atomic vector", i + 1);

set_vector_value(out, i, res, 0);
UNPROTECT(1);
}

UNPROTECT(1);
UNPROTECT(2);
return out;
}

Expand Down Expand Up @@ -88,8 +88,8 @@ SEXP map2_impl(SEXP env, SEXP x_name_, SEXP y_name_, SEXP f_name_, SEXP type_) {
SEXP i = Rf_install("i");
SEXPTYPE type = Rf_str2type(CHAR(Rf_asChar(type_)));

SEXP x_val = Rf_eval(x, env);
SEXP y_val = Rf_eval(y, env);
SEXP x_val = PROTECT(Rf_eval(x, env));
SEXP y_val = PROTECT(Rf_eval(y, env));

if (!Rf_isVector(x_val) && !Rf_isNull(x_val))
Rf_errorcall(R_NilValue, "`.x` is not a vector (%s)", Rf_type2char(TYPEOF(x_val)));
Expand All @@ -98,6 +98,7 @@ SEXP map2_impl(SEXP env, SEXP x_name_, SEXP y_name_, SEXP f_name_, SEXP type_) {

int nx = Rf_length(x_val), ny = Rf_length(y_val);
if (nx == 0 || ny == 0) {
UNPROTECT(2);
return Rf_allocVector(type, 0);
}
if (nx != ny && !(nx == 1 || ny == 1)) {
Expand All @@ -114,14 +115,14 @@ SEXP map2_impl(SEXP env, SEXP x_name_, SEXP y_name_, SEXP f_name_, SEXP type_) {
SEXP out = PROTECT(call_loop(env, f_call, n, type, 2));
copy_names(x_val, out);

UNPROTECT(5);
UNPROTECT(7);
return out;
}

SEXP pmap_impl(SEXP env, SEXP l_name_, SEXP f_name_, SEXP type_) {
const char* l_name = CHAR(Rf_asChar(l_name_));
SEXP l = Rf_install(l_name);
SEXP l_val = Rf_eval(l, env);
SEXP l_val = PROTECT(Rf_eval(l, env));
SEXPTYPE type = Rf_str2type(CHAR(Rf_asChar(type_)));

if (!Rf_isVectorList(l_val))
Expand All @@ -140,6 +141,7 @@ SEXP pmap_impl(SEXP env, SEXP l_name_, SEXP f_name_, SEXP type_) {
int nj = Rf_length(j_val);

if (nj == 0) {
UNPROTECT(1);
return Rf_allocVector(type, 0);
} else if (nj > n) {
n = nj;
Expand All @@ -156,7 +158,7 @@ SEXP pmap_impl(SEXP env, SEXP l_name_, SEXP f_name_, SEXP type_) {
Rf_errorcall(R_NilValue, "Element %i has length %i, not 1 or %i.", j + 1, nj, n);
}

SEXP l_names = Rf_getAttrib(l_val, R_NamesSymbol);
SEXP l_names = PROTECT(Rf_getAttrib(l_val, R_NamesSymbol));
int has_names = !Rf_isNull(l_names);

const char* f_name = CHAR(Rf_asChar(f_name_));
Expand Down Expand Up @@ -193,6 +195,6 @@ SEXP pmap_impl(SEXP env, SEXP l_name_, SEXP f_name_, SEXP type_) {
SEXP out = PROTECT(call_loop(env, f_call, n, type, m));
copy_names(VECTOR_ELT(l_val, 0), out);

UNPROTECT(3);
UNPROTECT(5);
return out;
}
4 changes: 2 additions & 2 deletions src/transpose.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ SEXP transpose_impl(SEXP x, SEXP names_template) {

// Create space for output
SEXP out = PROTECT(Rf_allocVector(VECSXP, m));
SEXP names1 = Rf_getAttrib(x, R_NamesSymbol);
SEXP names1 = PROTECT(Rf_getAttrib(x, R_NamesSymbol));

for (int j = 0; j < m; ++j) {
SEXP xj = PROTECT(Rf_allocVector(VECSXP, n));
Expand Down Expand Up @@ -95,6 +95,6 @@ SEXP transpose_impl(SEXP x, SEXP names_template) {
UNPROTECT(1);
}

UNPROTECT(1);
UNPROTECT(2);
return out;
}

0 comments on commit 8e6098a

Please sign in to comment.