Skip to content

Commit a791bbe

Browse files
committed
check module and internal function arity before invoking
1 parent a6f354e commit a791bbe

8 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎ChangeLog‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
2026-10-29 Kevin Ushey <kevinushey@gmail.com>
2+
3+
* src/module.cpp (InternalFunction_invoke): Raise an error when fewer
4+
arguments are supplied than the arity stored in the function pointer's
5+
tag, instead of reading the missing ones from uninitialized memory
6+
(#1521)
7+
* inst/include/Rcpp/module/Module.h (Module::get_function): Store the
8+
arity of module functions in the pointer tag
9+
* inst/include/Rcpp/InternalFunction.h: Idem for InternalFunction
10+
* inst/tinytest/cpp/Module.cpp: Add module functions with formals
11+
* inst/tinytest/test_module.R: Test calls with too few arguments
12+
* inst/tinytest/test_internal_function.R: Idem
13+
* inst/tinytest/test_internal_function_cpp11.R: Idem
14+
115
2026-10-23 Dirk Eddelbuettel <edd@debian.org>
216

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

‎inst/include/Rcpp/InternalFunction.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,24 @@ namespace Rcpp{
3535

3636
RCPP_GENERATE_CTOR_ASSIGN(InternalFunction_Impl)
3737

38+
// the arity goes in the pointer tag, where InternalFunction_invoke
39+
// checks it against the number of supplied arguments
40+
3841
template <typename RESULT_TYPE, typename... Args>
3942
InternalFunction_Impl(const std::function<RESULT_TYPE(Args...)> &fun) {
43+
Shield<SEXP> arity(Rf_ScalarInteger(static_cast<int>(sizeof...(Args))));
4044
set(
4145
XPtr<Rcpp::InternalFunctionWithStdFunction::CppFunctionBaseFromStdFunction<RESULT_TYPE, Args...> >(
4246
new Rcpp::InternalFunctionWithStdFunction::CppFunctionBaseFromStdFunction<RESULT_TYPE, Args...>(fun),
43-
false
47+
false,
48+
arity
4449
)
4550
);
4651
}
4752
template <typename RESULT_TYPE, typename... T>
4853
InternalFunction_Impl(RESULT_TYPE (*fun)(T...)) {
49-
set(XPtr<CppFunctionN<RESULT_TYPE, T...> >(new CppFunctionN<RESULT_TYPE, T...>(fun), true));
54+
Shield<SEXP> arity(Rf_ScalarInteger(static_cast<int>(sizeof...(T))));
55+
set(XPtr<CppFunctionN<RESULT_TYPE, T...> >(new CppFunctionN<RESULT_TYPE, T...>(fun), true, arity));
5056
}
5157

5258
void update(SEXP){}

‎inst/include/Rcpp/module/Module.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ namespace Rcpp {
164164
}
165165
std::string sign ;
166166
fun->signature( sign, name_.data() ) ;
167+
168+
// checked against the number of supplied arguments by InternalFunction_invoke
169+
Shield<SEXP> arity( Rf_ScalarInteger( fun->nargs() ) ) ;
170+
167171
return List::create(
168-
XPtr<CppFunction>( fun, false ),
172+
XPtr<CppFunction>( fun, false, arity ),
169173
fun->is_void(),
170174
fun->docstring,
171175
sign,

‎inst/tinytest/cpp/Module.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ RCPP_MODULE(demoModule) {
166166
function("bla1" , &bla1 );
167167
function("bla2" , &bla2 );
168168

169+
// formals with defaults, and formals shorter than the parameter list
170+
function("foo_formals", &foo, List::create(_["x"] = 2, _["y"] = 10.0));
171+
function("foo_short_formals", &foo, List::create(_["x"] = 2));
172+
169173
function("test_reference", test_reference);
170174
function("test_const_reference", test_const_reference);
171175
function("test_const", test_const);

‎inst/tinytest/test_internal_function.R‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Rcpp::sourceCpp("cpp/InternalFunction.cpp")
2424
fun <- getAdd()
2525
expect_equal( fun(10,32), 42 )
2626

27+
## too few arguments raise an error rather than reading the missing ones
28+
## from uninitialized memory (#1521)
29+
expect_error( fun(10), "expecting 2, got 1" )
30+
2731
# test.internal_function_concatenate <- function(){
2832
fun <- getConcatenate()
2933
expect_equal( fun("Hello"," World"), "Hello World" )

‎inst/tinytest/test_internal_function_cpp11.R‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ expect_equal( fun(38), 42 )
2929
# test.internal_function_concatenate <- function(){
3030
fun <- getConcatenate()
3131
expect_equal( fun("Hello"," World"), "Hello World" )
32+
33+
## too few arguments raise an error rather than reading the missing ones
34+
## from uninitialized memory (#1521)
35+
expect_error( fun("Hello"), "expecting 2, got 1" )

‎inst/tinytest/test_module.R‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ expect_true(is_void(bla@.Data))
4141
expect_true(is_void(bla1@.Data))
4242
expect_true(is_void(bla2@.Data))
4343

44+
## module functions must be called with at least as many arguments as
45+
## the C++ function takes, rather than reading the missing ones from
46+
## uninitialized memory (#1521)
47+
expect_error( foo( 2L ), "expecting 2, got 1" )
48+
expect_error( bar(), "expecting 1, got 0" )
49+
expect_equal( foo_formals(), 20.0 )
50+
expect_equal( foo_formals( y = 3.0 ), 6.0 )
51+
expect_error( foo_short_formals(), "expecting 2, got 1" )
52+
4453
w <- new( ModuleWorld )
4554
expect_equal( w$greet(), "hello" )
4655
w$set( "hello world" )

‎src/module.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ BEGIN_RCPP
119119
SEXP p = CDR(args);
120120
XP_Function fun(CAR(p)); p = CDR(p);
121121
UNPACK_EXTERNAL_ARGS(cargs,p)
122+
123+
// Module::get_function() and InternalFunction store the arity in the
124+
// tag; without this check, parameters beyond those supplied would be
125+
// read from uninitialized slots of cargs
126+
SEXP arity = R_ExternalPtrTag(fun);
127+
if (TYPEOF(arity) == INTSXP && Rf_xlength(arity) == 1 && nargs < INTEGER(arity)[0])
128+
Rcpp::stop(
129+
"incorrect number of arguments: expecting %d, got %d",
130+
INTEGER(arity)[0],
131+
nargs
132+
);
133+
122134
return fun->operator()(cargs);
123135
END_RCPP
124136
}

0 commit comments

Comments
 (0)