Skip to content

Commit 73506c0

Browse files
committed
r/src/ Fix unprotected variable found by rchk.
1 parent 667276a commit 73506c0

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

r/src/Rcommon.h

+29-27
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,41 @@
2626
/* The C API of R is awfully ugly and unpractical (and poorly
2727
documented). These wrappers make it a little more bearable. */
2828

29+
#define PROTECT_PLUS(WHAT) PROTECT(WHAT); nprotected++
30+
2931
#define Rexp(VAR) Rexp_##VAR
3032

31-
#define new_real_matrix(VAR, DIM1, DIM2) \
32-
SEXP Rexp_##VAR; double *VAR; \
33-
PROTECT(Rexp_##VAR = Rf_allocMatrix(REALSXP, (DIM1), (DIM2))); \
34-
nprotected++; VAR = REAL(Rexp_##VAR)
33+
#define new_real_matrix(VAR, DIM1, DIM2) \
34+
SEXP Rexp_##VAR; \
35+
PROTECT_PLUS(Rexp_##VAR = Rf_allocMatrix(REALSXP, (DIM1), (DIM2))); \
36+
double *VAR = REAL(Rexp_##VAR)
3537

36-
#define new_real_vector(VAR, DIM) \
37-
SEXP Rexp_##VAR; double *VAR; \
38-
PROTECT(Rexp_##VAR = Rf_allocVector(REALSXP, (DIM))); \
39-
nprotected++; VAR = REAL(Rexp_##VAR)
38+
#define new_real_vector(VAR, DIM) \
39+
SEXP Rexp_##VAR; \
40+
PROTECT_PLUS(Rexp_##VAR = Rf_allocVector(REALSXP, (DIM))); \
41+
double *VAR = REAL(Rexp_##VAR)
4042

41-
#define new_int_vector(VAR, DIM) \
42-
SEXP Rexp_##VAR; int *VAR; \
43-
PROTECT(Rexp_##VAR = Rf_allocVector(INTSXP, (DIM))); \
44-
nprotected++; VAR = INTEGER(Rexp_##VAR)
43+
#define new_int_vector(VAR, DIM) \
44+
SEXP Rexp_##VAR; \
45+
PROTECT_PLUS(Rexp_##VAR = Rf_allocVector(INTSXP, (DIM))); \
46+
int *VAR = INTEGER(Rexp_##VAR)
4547

4648
#define new_string_vector(VAR, DIM) \
4749
SEXP Rexp_##VAR; int Rexp_##VAR##_len = 0; \
48-
PROTECT(Rexp_##VAR = Rf_allocVector(STRSXP, (DIM))); \
49-
nprotected++
50+
PROTECT_PLUS(Rexp_##VAR = Rf_allocVector(STRSXP, (DIM)))
5051

5152
#define string_vector_push_back(VAR, ELEMENT) \
5253
SET_STRING_ELT(Rexp_##VAR, Rexp_##VAR##_len, Rf_mkChar(ELEMENT)); \
5354
Rexp_##VAR##_len++
5455

5556
#define new_list(LISTVAR, LENGTH) \
5657
SEXP Rexp_##LISTVAR; int Rexp_##LISTVAR##_len = 0; \
57-
PROTECT(Rexp_##LISTVAR = Rf_allocVector(VECSXP, (LENGTH))); \
58-
++nprotected
58+
PROTECT_PLUS(Rexp_##LISTVAR = Rf_allocVector(VECSXP, (LENGTH)))
5959

6060
#define new_logical_vector(VAR, DIM) \
61-
SEXP Rexp_##VAR; int *VAR; \
62-
PROTECT(Rexp_##VAR = Rf_allocVector(LGLSXP, (DIM))); \
63-
nprotected++; VAR = LOGICAL(Rexp_##VAR)
61+
SEXP Rexp_##VAR; \
62+
PROTECT_PLUS(Rexp_##VAR = Rf_allocVector(LGLSXP, (DIM))); \
63+
int *VAR = LOGICAL(Rexp_##VAR)
6464

6565
#define list_len(VAR) Rexp_##VAR##_len
6666

@@ -74,6 +74,7 @@
7474
#define set_attribute(VAR, ATTRIBUTE, VALUE) \
7575
Rf_setAttrib(Rexp_##VAR, Rf_install(ATTRIBUTE), Rexp_##VALUE)
7676

77+
7778
/*
7879
* Unpack an integer vector stored in SEXP S.
7980
*/
@@ -139,17 +140,16 @@ bool_2_logical_vector(int *dst, const bool *src, size_t n)
139140
static inline SEXP
140141
set_colnames(SEXP matrix, const char *const * names, size_t names_len)
141142
{
142-
int nprotected=0;
143-
SEXP dimnames = Rf_getAttrib(matrix, R_DimNamesSymbol);
144-
if (dimnames == R_NilValue) {
145-
PROTECT(dimnames = Rf_allocVector(VECSXP, 2));
146-
nprotected++;
147-
}
148-
143+
int nprotected = 0;
149144
new_string_vector (colnames, names_len);
150145
for (size_t k = 0; k < names_len; k++) {
151146
string_vector_push_back (colnames, names[k]);
152147
}
148+
149+
SEXP dimnames = PROTECT_PLUS(Rf_getAttrib(matrix, R_DimNamesSymbol));
150+
if (dimnames == R_NilValue) {
151+
PROTECT_PLUS(dimnames = Rf_allocVector(VECSXP, 2));
152+
}
153153
SET_VECTOR_ELT(dimnames, 1, Rexp(colnames));
154154
Rf_setAttrib(matrix, R_DimNamesSymbol, dimnames);
155155

@@ -161,6 +161,7 @@ set_colnames(SEXP matrix, const char *const * names, size_t names_len)
161161
static inline void
162162
matrix_copy_dimnames(SEXP dest, const SEXP src)
163163
{
164+
int nprotected = 0;
164165
// Ensure both source and target are matrices
165166
if (!Rf_isMatrix(src))
166167
Rf_error("src must be a matrix.");
@@ -169,8 +170,9 @@ matrix_copy_dimnames(SEXP dest, const SEXP src)
169170
Rf_error("dest must be a matrix.");
170171

171172
// Get the dimnames from the source matrix
172-
SEXP dimnames = Rf_getAttrib(src, R_DimNamesSymbol);
173+
SEXP dimnames = PROTECT_PLUS(Rf_getAttrib(src, R_DimNamesSymbol));
173174

174175
// Set the dimnames to the target matrix
175176
Rf_setAttrib(dest, R_DimNamesSymbol, dimnames);
177+
UNPROTECT(nprotected);
176178
}

r/src/Rmoocore.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ compute_eafdiff_C(SEXP DATA, SEXP CUMSIZES, SEXP INTERVALS)
9696
}
9797
}
9898
eaf_free(eaf, nruns);
99-
UNPROTECT (1);
99+
UNPROTECT(1);
100100
return mat;
101101
}
102102

0 commit comments

Comments
 (0)