From 842ab2f129cd9fa19f74a648e147de3fd09986f5 Mon Sep 17 00:00:00 2001 From: Randy Lai Date: Sat, 22 Apr 2023 19:01:15 -0700 Subject: [PATCH] fix rchk check --- src/deque.c | 10 ++++++---- src/dict.c | 6 ++++-- src/priority_queue.c | 31 ++++++++++++++++++++----------- src/xxh.c | 6 +++++- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/deque.c b/src/deque.c index 171ecbe..394121f 100644 --- a/src/deque.c +++ b/src/deque.c @@ -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); @@ -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); diff --git a/src/dict.c b/src/dict.c index a6e3742..ae3f5cc 100644 --- a/src/dict.c +++ b/src/dict.c @@ -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; } diff --git a/src/priority_queue.c b/src/priority_queue.c index 1976b36..fe844b0 100644 --- a/src/priority_queue.c +++ b/src/priority_queue.c @@ -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; } @@ -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); @@ -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); @@ -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; } diff --git a/src/xxh.c b/src/xxh.c index f67f631..87a7f0e 100644 --- a/src/xxh.c +++ b/src/xxh.c @@ -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;