vim-patch:8.1.0177: defining function in sandbox is inconsistent

Problem:    Defining function in sandbox is inconsistent, cannot use :function
            but can define a lambda.
Solution:   Allow defining a function in the sandbox, but also use the sandbox
            when executing it. (closes vim/vim#3182)
93343725b5
This commit is contained in:
Jan Edmund Lazo 2019-03-23 00:58:00 -04:00
parent 3e78319ac6
commit b5582d1b32
2 changed files with 19 additions and 2 deletions

View File

@ -241,13 +241,14 @@ typedef enum {
///< the value (prevents error message). ///< the value (prevents error message).
} GetLvalFlags; } GetLvalFlags;
// function flags // flags used in uf_flags
#define FC_ABORT 0x01 // abort function on error #define FC_ABORT 0x01 // abort function on error
#define FC_RANGE 0x02 // function accepts range #define FC_RANGE 0x02 // function accepts range
#define FC_DICT 0x04 // Dict function, uses "self" #define FC_DICT 0x04 // Dict function, uses "self"
#define FC_CLOSURE 0x08 // closure, uses outer scope variables #define FC_CLOSURE 0x08 // closure, uses outer scope variables
#define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0 #define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0
#define FC_REMOVED 0x20 // function redefined while uf_refcount > 0 #define FC_REMOVED 0x20 // function redefined while uf_refcount > 0
#define FC_SANDBOX 0x40 // function defined in the sandbox
// The names of packages that once were loaded are remembered. // The names of packages that once were loaded are remembered.
static garray_T ga_loaded = { 0, 0, sizeof(char_u *), 4, NULL }; static garray_T ga_loaded = { 0, 0, sizeof(char_u *), 4, NULL };
@ -5853,6 +5854,9 @@ static int get_lambda_tv(char_u **arg, typval_T *rettv, bool evaluate)
if (prof_def_func()) { if (prof_def_func()) {
func_do_profile(fp); func_do_profile(fp);
} }
if (sandbox) {
flags |= FC_SANDBOX;
}
fp->uf_varargs = true; fp->uf_varargs = true;
fp->uf_flags = flags; fp->uf_flags = flags;
fp->uf_calls = 0; fp->uf_calls = 0;
@ -20352,6 +20356,9 @@ void ex_function(exarg_T *eap)
if (prof_def_func()) if (prof_def_func())
func_do_profile(fp); func_do_profile(fp);
fp->uf_varargs = varargs; fp->uf_varargs = varargs;
if (sandbox) {
flags |= FC_SANDBOX;
}
fp->uf_flags = flags; fp->uf_flags = flags;
fp->uf_calls = 0; fp->uf_calls = 0;
fp->uf_script_ID = current_SID; fp->uf_script_ID = current_SID;
@ -21342,6 +21349,7 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars,
char_u *save_sourcing_name; char_u *save_sourcing_name;
linenr_T save_sourcing_lnum; linenr_T save_sourcing_lnum;
scid_T save_current_SID; scid_T save_current_SID;
bool using_sandbox = false;
funccall_T *fc; funccall_T *fc;
int save_did_emsg; int save_did_emsg;
static int depth = 0; static int depth = 0;
@ -21499,6 +21507,12 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars,
save_sourcing_name = sourcing_name; save_sourcing_name = sourcing_name;
save_sourcing_lnum = sourcing_lnum; save_sourcing_lnum = sourcing_lnum;
sourcing_lnum = 1; sourcing_lnum = 1;
if (fp->uf_flags & FC_SANDBOX) {
using_sandbox = true;
sandbox++;
}
// need space for new sourcing_name: // need space for new sourcing_name:
// * save_sourcing_name // * save_sourcing_name
// * "["number"].." or "function " // * "["number"].." or "function "
@ -21659,6 +21673,9 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars,
if (do_profiling_yes) { if (do_profiling_yes) {
script_prof_restore(&wait_start); script_prof_restore(&wait_start);
} }
if (using_sandbox) {
sandbox--;
}
if (p_verbose >= 12 && sourcing_name != NULL) { if (p_verbose >= 12 && sourcing_name != NULL) {
++no_wait_return; ++no_wait_return;

View File

@ -1004,7 +1004,7 @@ return {
}, },
{ {
command='function', command='function',
flags=bit.bor(EXTRA, BANG, CMDWIN), flags=bit.bor(EXTRA, BANG, SBOXOK, CMDWIN),
addr_type=ADDR_LINES, addr_type=ADDR_LINES,
func='ex_function', func='ex_function',
}, },