Skip to content

Commit

Permalink
fix rchk check
Browse files Browse the repository at this point in the history
  • Loading branch information
randy3k committed Apr 23, 2023
1 parent e78d307 commit 842ab2f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
10 changes: 6 additions & 4 deletions src/deque.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,13 @@ SEXP deque_remove(SEXP self, SEXP value) {
SEXP last_ptr = PROTECT(get_sexp_value(self, "last"));
// make sure the pointers are resolved after serialization/unserialization
get_last_cons(q, last_ptr);
SEXP v, nextq, prev_ptr;
SEXP v, v1, nextq, prev_ptr;
while (q != R_NilValue) {
v = CAR(q);
v1 = PROTECT(VECTOR_ELT(v, 1));
nextq = CDR(q);
if (R_compute_identical(VECTOR_ELT(v, 1), value, 16)) {
prev_ptr = VECTOR_ELT(v, 0);
if (R_compute_identical(v1, value, 16)) {
prev_ptr = PROTECT(VECTOR_ELT(v, 0));
if (nextq == R_NilValue && prev_ptr == R_NilValue) {
set_sexp_value(self, "q", R_NilValue);
R_SetExternalPtrAddr(last_ptr, NULL);
Expand All @@ -158,9 +159,10 @@ SEXP deque_remove(SEXP self, SEXP value) {
SETCDR(prev, nextq);
SET_VECTOR_ELT(CAR(nextq), 0, prev_ptr);
}
UNPROTECT(2);
UNPROTECT(4);
return R_NilValue;
}
UNPROTECT(1);
q = nextq;
}
UNPROTECT(2);
Expand Down
6 changes: 4 additions & 2 deletions src/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ SEXP dict_get(SEXP self, SEXP _key) {
}
UNPROTECT(1);
}
SEXP vs = get_sexp_value(self, "vs");
return VECTOR_ELT(vs, index - 1);
SEXP vs = PROTECT(get_sexp_value(self, "vs"));
SEXP res = VECTOR_ELT(vs, index - 1);
UNPROTECT(1);
return res;
}


Expand Down
31 changes: 20 additions & 11 deletions src/priority_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@


static void swap(SEXP h, int a, int b) {
SEXP temp = PROTECT(VECTOR_ELT(h, a));
SET_VECTOR_ELT(h, a, VECTOR_ELT(h, b));
SET_VECTOR_ELT(h, b, temp);
UNPROTECT(1);
SEXP ha = PROTECT(VECTOR_ELT(h, a));
SEXP hb = PROTECT(VECTOR_ELT(h, b));
SET_VECTOR_ELT(h, a, hb);
SET_VECTOR_ELT(h, b, ha);
UNPROTECT(2);
}

static int cmp(SEXP h, int a, int b) {
SEXP _x = VECTOR_ELT(h, a);
SEXP _y = VECTOR_ELT(h, b);
PROTECT(h);
SEXP _x = PROTECT(VECTOR_ELT(h, a));
SEXP _y = PROTECT(VECTOR_ELT(h, b));
double x = Rf_asReal(VECTOR_ELT(_x, 0));
double y = Rf_asReal(VECTOR_ELT(_y, 0));
UNPROTECT(3);
return x < y;
}

Expand All @@ -33,7 +36,9 @@ static void grow(SEXP self, int n) {
} else if (m < n + 1) {
h2 = PROTECT(Rf_allocVector(VECSXP, (int) ceil(GROW_FACTOR * m)));
for (i = 0; i < n; i++) {
SET_VECTOR_ELT(h2, i, VECTOR_ELT(h, i));
SEXP h2i = PROTECT(VECTOR_ELT(h, i));
SET_VECTOR_ELT(h2, i, h2i);
UNPROTECT(1);
}
set_sexp_value(self, "h", h2);
UNPROTECT(1);
Expand All @@ -50,7 +55,9 @@ static void shrink(SEXP self, int n) {
if (n < m1 && m1 > INITIAL_SIZE) {
h2 = PROTECT(Rf_allocVector(VECSXP, m1));
for (i = 0; i < n; i++) {
SET_VECTOR_ELT(h2, i, VECTOR_ELT(h, i));
SEXP h2i = PROTECT(VECTOR_ELT(h, i));
SET_VECTOR_ELT(h2, i, h2i);
UNPROTECT(1);
}
set_sexp_value(self, "h", h2);
UNPROTECT(1);
Expand Down Expand Up @@ -120,11 +127,13 @@ SEXP heap_pop(SEXP self) {
int n = Rf_asInteger(_n);
if (n == 0) Rf_error("queue is empty");
SEXP x = PROTECT(VECTOR_ELT(h, 0));
SET_VECTOR_ELT(h, 0, VECTOR_ELT(h, n - 1));
SEXP h0 = PROTECT(VECTOR_ELT(h, n - 1));
SET_VECTOR_ELT(h, 0, h0);
sift_down(h, 0, n - 2);
_n = PROTECT(Rf_ScalarInteger(n - 1));
set_sexp_value(self, "n", _n);
shrink(self, n);
UNPROTECT(4);
return VECTOR_ELT(x, 1);
SEXP res = PROTECT(VECTOR_ELT(x, 1));
UNPROTECT(6);
return res;
}
6 changes: 5 additions & 1 deletion src/xxh.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ int is_hashable(SEXP key) {
} else if (TYPEOF(key) == VECSXP) {
R_xlen_t i;
R_xlen_t n = Rf_length(key);
SEXP keyi;
for (i = 0; i < n; i++) {
if (!is_hashable(VECTOR_ELT(key, i))) {
keyi = PROTECT(VECTOR_ELT(key, i));
if (!is_hashable(keyi)) {
UNPROTECT(1);
return 0;
}
UNPROTECT(1);
}
if (!is_hashable(ATTRIB(key))) {
return 0;
Expand Down

0 comments on commit 842ab2f

Please sign in to comment.