From 27a7a4cf07ca3c630f8c0c932ad970538ea57dc9 Mon Sep 17 00:00:00 2001 From: Felix Held Date: Tue, 10 Dec 2024 14:44:27 +0100 Subject: [PATCH] attempt to fix clang-UBSAN error --- DESCRIPTION | 2 +- cran-comments.md | 3 ++- src/optim.cpp | 14 ++++++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3508364..5b3f0d3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: scregclust Title: Reconstructing the Regulatory Programs of Target Genes in scRNA-Seq Data -Version: 0.2.0 +Version: 0.2.1 Authors@R: c( person("Felix", "Held", ,"felix.held@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-7679-7752")), diff --git a/cran-comments.md b/cran-comments.md index dd43320..6ff7f7b 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1 +1,2 @@ -* New functionality was added to the package +* Despite repeated efforts we did not manage to replicate the clang-UBSAN errors detected on CRAN. +* We added additional checks in the compiled code which should ensure that the pointed out undefined behaviour error is avoided, but due to not being able to replicate the error, we could not test if this fixes the error. diff --git a/src/optim.cpp b/src/optim.cpp index d2aef85..fbcc341 100644 --- a/src/optim.cpp +++ b/src/optim.cpp @@ -18,8 +18,10 @@ static Matd compute_xtx(const Matd& x) { const auto p = x.cols(); Matd xtx = Eigen::MatrixXd::Zero(p, p); - xtx.selfadjointView().rankUpdate(x.transpose()); - xtx.triangularView() = xtx.transpose(); + if (p > 0) { + xtx.selfadjointView().rankUpdate(x.transpose()); + xtx.triangularView() = xtx.transpose(); + } return xtx; } @@ -71,6 +73,10 @@ Rcpp::List coop_lasso( const auto m = y.cols(); const auto p = x.cols(); + if (n <= 0 || m <= 0 || p <= 0 || x.rows() <= 0) { + Rcpp::stop("Matrix dimensions of y and x need to be positive."); + } + if (x.rows() != n) { Rcpp::stop("y and x need to have the same number of rows."); } @@ -399,6 +405,10 @@ Rcpp::List coef_nnls(Eigen::Map x, Eigen::Map const auto n = x.cols(); auto m = y.cols(); // Will be reduced whenever right-hand sides reach convergence + if (n <= 0 || m <= 0 || x.rows() <= 0 || y.rows() <= 0) { + Rcpp::stop("Matrix dimensions of y and x need to be positive."); + } + // Pre-compute some quantities to speed up computation // Precompute X^T X const Matd xtx = compute_xtx(x);