Skip to content

Commit

Permalink
patch 8.1.0177: defining function in sandbox is inconsistent
Browse files Browse the repository at this point in the history
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 #3182)
  • Loading branch information
brammool committed Jul 10, 2018
1 parent 18085fa commit 9334372
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ex_cmds.h
Expand Up @@ -584,7 +584,7 @@ EX(CMD_for, "for", ex_while,
EXTRA|NOTRLCOM|SBOXOK|CMDWIN, EXTRA|NOTRLCOM|SBOXOK|CMDWIN,
ADDR_LINES), ADDR_LINES),
EX(CMD_function, "function", ex_function, EX(CMD_function, "function", ex_function,
EXTRA|BANG|CMDWIN, EXTRA|BANG|SBOXOK|CMDWIN,
ADDR_LINES), ADDR_LINES),
EX(CMD_global, "global", ex_global, EX(CMD_global, "global", ex_global,
RANGE|WHOLEFOLD|BANG|EXTRA|DFLALL|SBOXOK|CMDWIN, RANGE|WHOLEFOLD|BANG|EXTRA|DFLALL|SBOXOK|CMDWIN,
Expand Down
29 changes: 22 additions & 7 deletions src/userfunc.c
Expand Up @@ -14,13 +14,14 @@
#include "vim.h" #include "vim.h"


#if defined(FEAT_EVAL) || defined(PROTO) #if defined(FEAT_EVAL) || defined(PROTO)
/* 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


/* From user function to hashitem and back. */ /* From user function to hashitem and back. */
#define UF2HIKEY(fp) ((fp)->uf_name) #define UF2HIKEY(fp) ((fp)->uf_name)
Expand Down Expand Up @@ -296,6 +297,8 @@ get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
if (prof_def_func()) if (prof_def_func())
func_do_profile(fp); func_do_profile(fp);
#endif #endif
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;
Expand Down Expand Up @@ -688,6 +691,7 @@ call_user_func(
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;
int 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;
Expand Down Expand Up @@ -854,6 +858,13 @@ call_user_func(
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 function name + ("function " + 3) or "[number]" */ /* need space for function name + ("function " + 3) or "[number]" */
len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name)) len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
+ STRLEN(fp->uf_name) + 20; + STRLEN(fp->uf_name) + 20;
Expand Down Expand Up @@ -1020,6 +1031,8 @@ call_user_func(
if (do_profiling == PROF_YES) if (do_profiling == PROF_YES)
script_prof_restore(&wait_start); script_prof_restore(&wait_start);
#endif #endif
if (using_sandbox)
--sandbox;


if (p_verbose >= 12 && sourcing_name != NULL) if (p_verbose >= 12 && sourcing_name != NULL)
{ {
Expand Down Expand Up @@ -2429,6 +2442,8 @@ ex_function(exarg_T *eap)
func_do_profile(fp); func_do_profile(fp);
#endif #endif
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;
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -789,6 +789,8 @@ static char *(features[]) =


static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
177,
/**/ /**/
176, 176,
/**/ /**/
Expand Down

0 comments on commit 9334372

Please sign in to comment.