Skip to content

Commit 7ec0389

Browse files
authored
Merge branch 'master' into safe_math
2 parents 87dc09c + 5fd3b68 commit 7ec0389

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2026-02-27 Iñaki Ucar <iucar@fedoraproject.org>
2+
3+
* inst/include/Rcpp/sugar/functions/max.h: Fix UB for empty integer
4+
* inst/include/Rcpp/sugar/functions/min.h: Idem
5+
* inst/tinytest/test_sugar.R: Adapt tests
6+
17
2026-02-26 Iñaki Ucar <iucar@fedoraproject.org>
28

39
* inst/include/Rcpp/sugar/tools/safe_math.h: New header implementing safe

inst/include/Rcpp/sugar/functions/max.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
// max.h: Rcpp R/C++ interface class library -- max
33
//
4-
// Copyright (C) 2012 - 2018 Dirk Eddelbuettel and Romain Francois
4+
// Copyright (C) 2012 - 2025 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2026 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
56
//
67
// This file is part of Rcpp.
78
//
@@ -33,7 +34,11 @@ namespace sugar{
3334

3435
operator STORAGE() const {
3536
R_xlen_t n = obj.size();
36-
if (n == 0) return(static_cast<STORAGE>(R_NegInf));
37+
if (n == 0) {
38+
if (RTYPE != REALSXP)
39+
Rcpp::stop("missing argument to max");
40+
return(static_cast<STORAGE>(R_NegInf));
41+
}
3742

3843
STORAGE max, current ;
3944
max = obj[0] ;
@@ -60,7 +65,11 @@ namespace sugar{
6065

6166
operator STORAGE() const {
6267
R_xlen_t n = obj.size();
63-
if (n == 0) return(static_cast<STORAGE>(R_NegInf));
68+
if (n == 0) {
69+
if (RTYPE != REALSXP)
70+
Rcpp::stop("missing argument to max");
71+
return(static_cast<STORAGE>(R_NegInf));
72+
}
6473

6574
STORAGE max, current ;
6675
max = obj[0] ;

inst/include/Rcpp/sugar/functions/min.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
// Min.h: Rcpp R/C++ interface class library -- min
33
//
4-
// Copyright (C) 2012 - 2018 Dirk Eddelbuettel and Romain Francois
4+
// Copyright (C) 2012 - 2025 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2026 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
56
//
67
// This file is part of Rcpp.
78
//
@@ -33,7 +34,11 @@ namespace sugar{
3334

3435
operator STORAGE() const {
3536
R_xlen_t n = obj.size();
36-
if (n == 0) return(static_cast<STORAGE>(R_PosInf));
37+
if (n == 0) {
38+
if (RTYPE != REALSXP)
39+
Rcpp::stop("missing argument to min");
40+
return(static_cast<STORAGE>(R_PosInf));
41+
}
3742

3843
STORAGE min, current ;
3944
min = obj[0] ;
@@ -60,7 +65,11 @@ namespace sugar{
6065

6166
operator STORAGE() const {
6267
R_xlen_t n = obj.size();
63-
if (n == 0) return(static_cast<STORAGE>(R_PosInf));
68+
if (n == 0) {
69+
if (RTYPE != REALSXP)
70+
Rcpp::stop("missing argument to min");
71+
return(static_cast<STORAGE>(R_PosInf));
72+
}
6473

6574
STORAGE min, current ;
6675
min = obj[0] ;

inst/tinytest/test_sugar.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,12 +1685,10 @@ expect_error(strimws(x[1], "invalid"), info = "strimws -- bad `which` argument")
16851685
## 21 July 2018
16861686
## min/max
16871687
# test.sugar.min.max <- function() {
1688-
## min(empty) gives NA for integer, Inf for numeric (#844)
1689-
if (!isArm) expect_true(is.na(intmin(integer(0))), "min(integer(0))")
1688+
expect_error(intmin(integer(0)), "missing argument")
16901689
if (!isArm) expect_equal(doublemin(numeric(0)), Inf, info = "min(numeric(0))")
16911690

1692-
## max(empty_ gives NA for integer, Inf for numeric (#844)
1693-
expect_true(is.na(intmax(integer(0))), "max(integer(0))")
1691+
expect_error(intmax(integer(0)), "missing argument")
16941692
expect_equal(doublemax(numeric(0)), -Inf, info = "max(numeric(0))")
16951693

16961694
## 'normal' values

0 commit comments

Comments
 (0)