Skip to content

Commit fd7ad87

Browse files
authored
Lay the ground for Rf_error masking (#1402) (#1407)
* Lay the ground for Rf_error masking (#1402) * Add a message at compilation time * Protect the valid Rf_error calls generated by Rcpp * some rewording to fit the ChangeLog text in two lines * messages are considered statements by gcc and cannot be used safely; switching to a warning * rebase fix * emit a message instead of a warning using an internal wrapper * remove space * Revert "remove space" This reverts commit bb86312. * Revert "emit a message instead of a warning using an internal wrapper" This reverts commit 9d409e8. * comment out warning * add on switch
1 parent 56e74cb commit fd7ad87

File tree

6 files changed

+53
-5
lines changed

6 files changed

+53
-5
lines changed

ChangeLog

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@
104104
or related R headers were installed first
105105
* inst/include/RcppCommon.h: Call new header as first thing
106106

107+
2025-11-12 Iñaki Ucar <[email protected]>
108+
109+
* inst/include/Rcpp/macros/mask.h: Lay the ground for Rf_error masking
110+
with a warning at compilation time unless RCPP_NO_MASK_RF_ERROR is defined
111+
* inst/include/RcppCommon.h: Include the previous file in the last place
112+
* src/attributes.cpp: Use parentheses to protect call to Rf_error
113+
* inst/tinytest/cpp/stack.cpp: Idem
114+
* inst/tinytest/testRcppInterfaceExporter/src/RcppExports.cpp: Idem
115+
107116
2025-11-04 Dirk Eddelbuettel <[email protected]>
108117

109118
* .github/workflows/macos.yaml (jobs): Roll macos-13 to macos-14
@@ -116,7 +125,7 @@
116125

117126
2025-10-21 Iñaki Ucar <[email protected]>
118127

119-
* inst/include/Rcpp/exceptions_impl.h: use __has_include to simplify checks
128+
* inst/include/Rcpp/exceptions_impl.h: Use __has_include to simplify checks
120129
to enable demangling, making them robust for more platforms
121130

122131
2025-10-13 Dirk Eddelbuettel <[email protected]>

inst/include/Rcpp/macros/mask.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// mask.h: Rcpp R/C++ interface class library -- masking macros
2+
//
3+
// Copyright (C) 2025 Iñaki Ucar
4+
//
5+
// This file is part of Rcpp.
6+
//
7+
// Rcpp is free software: you can redistribute it and/or modify it
8+
// under the terms of the GNU General Public License as published by
9+
// the Free Software Foundation, either version 2 of the License, or
10+
// (at your option) any later version.
11+
//
12+
// Rcpp is distributed in the hope that it will be useful, but
13+
// WITHOUT ANY WARRANTY; without even the implied warranty of
14+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
// GNU General Public License for more details.
16+
//
17+
// You should have received a copy of the GNU General Public License
18+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
19+
20+
#ifndef Rcpp_macros_mask_h
21+
#define Rcpp_macros_mask_h
22+
23+
#ifndef RCPP_NO_MASK_RF_ERROR
24+
#ifdef RCPP_MASK_RF_ERROR
25+
#define Rf_error(...) \
26+
_Pragma("GCC warning \"Use of Rf_error() instead of Rcpp::stop(). Calls \
27+
to Rf_error() in C++ contexts are unsafe: consider using Rcpp::stop() instead, \
28+
or define RCPP_NO_MASK_RF_ERROR if this is a false positive. More info:\n\
29+
- https://github.com/RcppCore/Rcpp/issues/1247\n\
30+
- https://github.com/RcppCore/Rcpp/pull/1402\"") \
31+
Rf_error(__VA_ARGS__)
32+
#endif
33+
#endif
34+
35+
#endif

inst/include/RcppCommon.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,6 @@ namespace Rcpp {
190190

191191
#include <Rcpp/internal/wrap.h>
192192

193+
#include <Rcpp/macros/mask.h>
194+
193195
#endif

inst/tinytest/cpp/stack.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//
33
// misc.cpp: Rcpp R/C++ interface class library -- misc unit tests
44
//
5-
// Copyright (C) 2013 - 2022 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2013 - 2024 Dirk Eddelbuettel and Romain Francois
6+
// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
67
//
78
// This file is part of Rcpp.
89
//
@@ -55,7 +56,7 @@ SEXP testSendInterrupt() {
5556
SEXP maybeThrow(void* data) {
5657
bool* fail = (bool*) data;
5758
if (*fail)
58-
Rf_error("throw!");
59+
(Rf_error)("throw!"); // prevent masking
5960
else
6061
return NumericVector::create(42);
6162
}

inst/tinytest/testRcppInterfaceExporter/src/RcppExports.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ RcppExport SEXP _testRcppInterfaceExporter_test_cpp_interface(SEXP xSEXP, SEXP f
4343
if (rcpp_isError_gen) {
4444
SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen);
4545
UNPROTECT(1);
46-
Rf_error("%s", CHAR(rcpp_msgSEXP_gen));
46+
(Rf_error)("%s", CHAR(rcpp_msgSEXP_gen));
4747
}
4848
UNPROTECT(1);
4949
return rcpp_result_gen;

src/attributes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2953,7 +2953,8 @@ namespace attributes {
29532953
<< " if (rcpp_isError_gen) {" << std::endl
29542954
<< " SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen);" << std::endl
29552955
<< " UNPROTECT(1);" << std::endl
2956-
<< " Rf_error(\"%s\", CHAR(rcpp_msgSEXP_gen));" << std::endl
2956+
// Parentheses to prevent masking
2957+
<< " (Rf_error)(\"%s\", CHAR(rcpp_msgSEXP_gen));" << std::endl
29572958
<< " }" << std::endl
29582959
<< " UNPROTECT(1);" << std::endl
29592960
<< " return rcpp_result_gen;" << std::endl

0 commit comments

Comments
 (0)