Skip to content

Commit 5fd3b68

Browse files
authored
Fail instead of trying to cast Inf to int (#1458)
* fail instead of trying to cast Inf to int * update ChangeLog
1 parent 7ff6ba2 commit 5fd3b68

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-17 Dirk Eddelbuettel <edd@debian.org>
28

39
* DESCRIPTION (Version, Date): Roll micro version and date

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
@@ -1623,12 +1623,10 @@ expect_error(strimws(x[1], "invalid"), info = "strimws -- bad `which` argument")
16231623
## 21 July 2018
16241624
## min/max
16251625
# test.sugar.min.max <- function() {
1626-
## min(empty) gives NA for integer, Inf for numeric (#844)
1627-
if (!isArm) expect_true(is.na(intmin(integer(0))), "min(integer(0))")
1626+
expect_error(intmin(integer(0)), "missing argument")
16281627
if (!isArm) expect_equal(doublemin(numeric(0)), Inf, info = "min(numeric(0))")
16291628

1630-
## max(empty_ gives NA for integer, Inf for numeric (#844)
1631-
expect_true(is.na(intmax(integer(0))), "max(integer(0))")
1629+
expect_error(intmax(integer(0)), "missing argument")
16321630
expect_equal(doublemax(numeric(0)), -Inf, info = "max(numeric(0))")
16331631

16341632
## 'normal' values

0 commit comments

Comments
 (0)