From c9a036552ddd809ca0b49f5ef73d469b8f17a621 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 25 Jul 2024 22:27:15 -0700 Subject: [PATCH 1/3] Move to STRING_PTR_RO in all sure-fire 'const' cases --- src/assign.c | 20 ++++++++++---------- src/between.c | 6 +++--- src/bmerge.c | 4 ++-- src/chmatch.c | 2 +- src/cj.c | 2 +- src/fifelse.c | 10 +++++----- src/fmelt.c | 6 +++--- src/forder.c | 14 +++++++------- src/frank.c | 4 ++-- src/fwriteR.c | 2 +- src/gsumm.c | 8 ++++---- src/rbindlist.c | 14 +++++++------- src/uniqlist.c | 4 ++-- src/utils.c | 8 ++++---- 14 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/assign.c b/src/assign.c index d937601eb..b1623875e 100644 --- a/src/assign.c +++ b/src/assign.c @@ -799,7 +799,7 @@ const char *memrecycle(const SEXP target, const SEXP where, const int start, con if (sourceIsFactor) { sourceLevels=PROTECT(getAttrib(source, R_LevelsSymbol)); protecti++; } if (!sourceIsFactor || !R_compute_identical(sourceLevels, targetLevels, 0)) { // !sourceIsFactor for test 2115.6 const int nTargetLevels=length(targetLevels), nSourceLevels=length(sourceLevels); - const SEXP *targetLevelsD=STRING_PTR(targetLevels), *sourceLevelsD=STRING_PTR(sourceLevels); + const SEXP *targetLevelsD=STRING_PTR_RO(targetLevels), *sourceLevelsD=STRING_PTR_RO(sourceLevels); SEXP newSource = PROTECT(allocVector(INTSXP, length(source))); protecti++; savetl_init(); for (int k=0; klmax; ++j) { for (int k=0; knrow; ++k) { SET_STRING_ELT(target, j*data->nrow + k, s[k]); diff --git a/src/forder.c b/src/forder.c index 70744d091..7226f7e45 100644 --- a/src/forder.c +++ b/src/forder.c @@ -277,7 +277,7 @@ static void cradix(SEXP *x, int n) free(cradix_xtmp); cradix_xtmp=NULL; } -static void range_str(SEXP *x, int n, uint64_t *out_min, uint64_t *out_max, int *out_na_count) +static void range_str(const SEXP *x, int n, uint64_t *out_min, uint64_t *out_max, int *out_na_count) // group numbers are left in truelength to be fetched by WRITE_KEY { int na_count=0; @@ -323,7 +323,7 @@ static void range_str(SEXP *x, int n, uint64_t *out_min, uint64_t *out_max, int SEXP *ustr3 = (SEXP *)malloc(ustr_n * sizeof(SEXP)); if (!ustr3) STOP(_("Failed to alloc ustr3 when converting strings to UTF8")); // # nocov - memcpy(ustr3, STRING_PTR(ustr2), ustr_n*sizeof(SEXP)); + memcpy(ustr3, STRING_PTR_RO(ustr2), ustr_n*sizeof(SEXP)); // need to reset ustr_maxlen because we need ustr_maxlen for utf8 strings ustr_maxlen = 0; for (int i=0; i a a regular factor because this case isn't yet implemented. a Date: Thu, 25 Jul 2024 22:27:59 -0700 Subject: [PATCH 2/3] Remaining cases compile as well --- src/chmatch.c | 4 ++-- src/coalesce.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/chmatch.c b/src/chmatch.c index 8f7855810..88333119b 100644 --- a/src/chmatch.c +++ b/src/chmatch.c @@ -10,7 +10,7 @@ static SEXP chmatchMain(SEXP x, SEXP table, int nomatch, bool chin, bool chmatch if (TYPEOF(x) == SYMSXP) { if (xlen!=1) error(_("Internal error: length of SYMSXP is %d not 1"), xlen); // # nocov - sym = PRINTNAME(x); // so we can do &sym to get a length 1 (const SEXP *)STRING_PTR(x) and save an alloc for coerce to STRSXP + sym = PRINTNAME(x); // so we can do &sym to get a length 1 (const SEXP *)STRING_PTR_RO(x) and save an alloc for coerce to STRSXP } else if (!isString(x) && !isSymbol(x) && !isNull(x)) { if (chin && !isVectorAtomic(x)) { return ScalarLogical(FALSE); @@ -40,7 +40,7 @@ static SEXP chmatchMain(SEXP x, SEXP table, int nomatch, bool chin, bool chmatch if (isSymbol(x)) { xd = &sym; } else { - xd = STRING_PTR(PROTECT(coerceUtf8IfNeeded(x))); nprotect++; + xd = STRING_PTR_RO(PROTECT(coerceUtf8IfNeeded(x))); nprotect++; } const SEXP *td = STRING_PTR_RO(PROTECT(coerceUtf8IfNeeded(table))); nprotect++; if (xlen==1) { diff --git a/src/coalesce.c b/src/coalesce.c index b747991c0..0c72f11d5 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -141,7 +141,7 @@ SEXP coalesce(SEXP x, SEXP inplaceArg) { } } break; case STRSXP: { - const SEXP *xP = STRING_PTR(first); + const SEXP *xP = STRING_PTR_RO(first); SEXP finalVal=NA_STRING; int k=0; for (int j=0; j Date: Thu, 25 Jul 2024 22:37:22 -0700 Subject: [PATCH 3/3] Conditionally define STRING_PTR_RO on 3.3.0<=R<3.5.0 --- src/data.table.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/data.table.h b/src/data.table.h index cd9e40efa..812ad5f97 100644 --- a/src/data.table.h +++ b/src/data.table.h @@ -12,6 +12,9 @@ #endif #include #define SEXPPTR_RO(x) ((const SEXP *)DATAPTR_RO(x)) // to avoid overhead of looped STRING_ELT and VECTOR_ELT +#ifndef STRING_PTR_RO +#define STRING_PTR_RO STRING_PTR +#endif #include // for uint64_t rather than unsigned long long #include #include "types.h"