java-topology/defects/r-source/patch/0001-rapply-class-match-early-exit.patch

27 lines
953 B
Diff

# UNDF: UNDF-2026-000000236
--- a/src/main/apply.c
+++ b/src/main/apply.c
@@ -308,10 +308,17 @@ static SEXP do_one(SEXP X, SEXP FUN, SEXP classes, SEXP deflt,
if(strcmp(CHAR(STRING_ELT(classes, 0)), "ANY") == 0) /* ASCII */
matched = true;
else {
+ /* CWE-407 fix: intern class names once; use pointer equality and early
+ * exit to reduce O(k*j) scan to O(k+j) in the typical case. */
PROTECT(klass = R_data_class(X, false));
- for(int i = 0; i < LENGTH(klass); i++)
- for(int j = 0; j < length(classes); j++)
- if(Seql(STRING_ELT(klass, i), STRING_ELT(classes, j)))
- matched = true;
+ int nk = LENGTH(klass);
+ int nc = length(classes);
+ for(int i = 0; i < nk && !matched; i++) {
+ SEXP ki = Rf_installChar(STRING_ELT(klass, i));
+ for(int j = 0; j < nc; j++) {
+ if(ki == Rf_installChar(STRING_ELT(classes, j))) {
+ matched = true;
+ break;
+ }
+ }
+ }
UNPROTECT(1);
}