From 0e39ceaa4d9b3e21a93a525f5f0e85467a8d17ef Mon Sep 17 00:00:00 2001 From: djvanderlaan Date: Wed, 16 May 2018 16:29:51 +0200 Subject: [PATCH] Fixed undefined behaviour issues with negative sizes --- DESCRIPTION | 2 +- src/assign_range.cpp | 2 ++ src/construct.cpp | 1 + src/indexing_range.cpp | 2 ++ 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4ea8510..7baa4a4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: lvec Type: Package Title: Out of Memory Vectors -Version: 0.2.0 +Version: 0.2.1 Date: 2018-05-15 Author: Jan van der Laan Maintainer: Jan van der Laan diff --git a/src/assign_range.cpp b/src/assign_range.cpp index 05005b3..4bbe5ef 100644 --- a/src/assign_range.cpp +++ b/src/assign_range.cpp @@ -64,6 +64,8 @@ RcppExport SEXP assign_range(SEXP rv, SEXP rindex, SEXP rvalues) { throw Rcpp::exception("Missing value for lower bound of range."); if (index.is_na(index[1])) throw Rcpp::exception("Missing value for upper bound of range."); + if (index[0] < 1 || index[1] < 1) + throw Rcpp::exception("Index out of range."); // create and call visitor assign_range_visitor visitor{static_cast(index[0]-1), static_cast(index[1]-1), *values}; diff --git a/src/construct.cpp b/src/construct.cpp index 02cecb1..dbba49d 100644 --- a/src/construct.cpp +++ b/src/construct.cpp @@ -5,6 +5,7 @@ RcppExport SEXP new_lvec(SEXP rsize, SEXP rtype, SEXP rstrlen) { BEGIN_RCPP double size = Rcpp::as(rsize); if (Rcpp::NumericVector::is_na(size)) throw Rcpp::exception("Size is not a number."); + if (size < 0) throw Rcpp::exception("Size is smaller than 0"); if (size > ldat::max_index) throw Rcpp::exception("Size is too large."); std::string type = Rcpp::as(rtype); diff --git a/src/indexing_range.cpp b/src/indexing_range.cpp index c603b3d..4c9f5c7 100644 --- a/src/indexing_range.cpp +++ b/src/indexing_range.cpp @@ -57,6 +57,8 @@ RcppExport SEXP get_range(SEXP rv, SEXP rindex) { throw Rcpp::exception("Missing value for lower bound of range."); if (ldat::is_na(index[1])) throw Rcpp::exception("Missing value for upper bound of range."); + if (index[0] < 1 || index[1] < 1) + throw Rcpp::exception("Index out of range."); // index range_indexing_visitor visitor{static_cast(index[0]-1), static_cast(index[1]-1)};