GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — check module and internal function arity before invoking (#1522) · RcppCore/Rcpp@3f687ae

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • ChangeLog
  • R
    • 00_classes.R
  • inst
    • include/Rcpp
      • InternalFunction.h
      • module
        • Module.h
    • tinytest
      • cpp
        • Module.cpp
      • test_internal_function.R
      • test_internal_function_cpp11.R
      • test_module.R
  • src
    • module.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎ChangeLog‎

Copy file name to clipboardExpand all lines: ChangeLog
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
2026-10-29 Kevin Ushey <[email protected]>
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+
15+
* R/00_classes.R (.cppfunction_formals_gets): Find the .External() call
16+
in the body of the function instead of assuming it is the second
17+
element, which it is not once coverage tools have instrumented the body
18+
* R/00_classes.R (.cppfunction_set_external_args): New helper
19+
* inst/tinytest/test_module.R: Test it with an instrumented body
20+
121
2026-10-23 Dirk Eddelbuettel <[email protected]>
222

323
* DESCRIPTION (Version, Date): Roll micro version and date
Collapse file

‎R/00_classes.R‎

Copy file name to clipboardExpand all lines: R/00_classes.R
+16-6Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,23 @@ setClass( "C++Function",
117117
contains = "function"
118118
)
119119

120-
.cppfunction_formals_gets <- function (fun, envir = environment(fun), value) {
121-
bd <- body(fun)
122-
b2 <- bd[[2L]]
123-
for( i in seq_along(value) ){
124-
b2[[3L+i]] <- as.name( names(value)[i] )
120+
## passes the arguments named in 'names' on from the .External(entry,
121+
## pointer, ...) call in expr; the call is searched for rather than assumed
122+
## to be a top-level statement, as coverage tools wrap each statement
123+
.cppfunction_set_external_args <- function( expr, names ){
124+
if( !is.call( expr ) )
125+
return( expr )
126+
if( identical( expr[[1L]], as.name( ".External" ) ) )
127+
return( as.call( c( as.list( expr )[1:3], lapply( names, as.name ) ) ) )
128+
for( i in seq_along( expr )[-1L] ){
129+
if( is.call( expr[[i]] ) )
130+
expr[[i]] <- .cppfunction_set_external_args( expr[[i]], names )
125131
}
126-
bd[[2]] <- b2
132+
expr
133+
}
134+
135+
.cppfunction_formals_gets <- function (fun, envir = environment(fun), value) {
136+
bd <- .cppfunction_set_external_args( body(fun), names(value) )
127137
f <- fun@.Data
128138
formals(f) <- value
129139
body(f) <- bd
Collapse file

‎inst/include/Rcpp/InternalFunction.h‎

Copy file name to clipboardExpand all lines: inst/include/Rcpp/InternalFunction.h
+8-2Lines 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){}
Collapse file

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

Copy file name to clipboardExpand all lines: inst/include/Rcpp/module/Module.h
+5-1Lines 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,
Collapse file

‎inst/tinytest/cpp/Module.cpp‎

Copy file name to clipboardExpand all lines: inst/tinytest/cpp/Module.cpp
+4Lines 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);
Collapse file

‎inst/tinytest/test_internal_function.R‎

Copy file name to clipboardExpand all lines: inst/tinytest/test_internal_function.R
+4Lines 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" )
Collapse file

‎inst/tinytest/test_internal_function_cpp11.R‎

Copy file name to clipboardExpand all lines: inst/tinytest/test_internal_function_cpp11.R
+4Lines 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" )
Collapse file

‎inst/tinytest/test_module.R‎

Copy file name to clipboardExpand all lines: inst/tinytest/test_module.R
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ expect_true(is_void([email protected]))
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+
53+
## formals<- finds the .External() call wherever it is in the body, e.g.
54+
## inside the block that coverage tools (covr) wrap each statement in
55+
wrapped <- quote({ if (TRUE) { covr:::count("key"); .External(entry, pointer, ...) } })
56+
expect_equal( deparse( Rcpp:::.cppfunction_set_external_args( wrapped, c("x", "y") ) ),
57+
deparse( quote({ if (TRUE) { covr:::count("key"); .External(entry, pointer, x, y) } }) ) )
58+
4459
w <- new( ModuleWorld )
4560
expect_equal( w$greet(), "hello" )
4661
w$set( "hello world" )
Collapse file

‎src/module.cpp‎

Copy file name to clipboardExpand all lines: src/module.cpp
+12Lines 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 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.