Skip to content

Commit 9334372

Browse files
committed
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 #3182)
1 parent 18085fa commit 9334372

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/ex_cmds.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ EX(CMD_for, "for", ex_while,
584584
EXTRA|NOTRLCOM|SBOXOK|CMDWIN,
585585
ADDR_LINES),
586586
EX(CMD_function, "function", ex_function,
587-
EXTRA|BANG|CMDWIN,
587+
EXTRA|BANG|SBOXOK|CMDWIN,
588588
ADDR_LINES),
589589
EX(CMD_global, "global", ex_global,
590590
RANGE|WHOLEFOLD|BANG|EXTRA|DFLALL|SBOXOK|CMDWIN,

src/userfunc.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
#include "vim.h"
1515

1616
#if defined(FEAT_EVAL) || defined(PROTO)
17-
/* function flags */
18-
#define FC_ABORT 0x01 /* abort function on error */
19-
#define FC_RANGE 0x02 /* function accepts range */
20-
#define FC_DICT 0x04 /* Dict function, uses "self" */
21-
#define FC_CLOSURE 0x08 /* closure, uses outer scope variables */
22-
#define FC_DELETED 0x10 /* :delfunction used while uf_refcount > 0 */
23-
#define FC_REMOVED 0x20 /* function redefined while uf_refcount > 0 */
17+
// flags used in uf_flags
18+
#define FC_ABORT 0x01 // abort function on error
19+
#define FC_RANGE 0x02 // function accepts range
20+
#define FC_DICT 0x04 // Dict function, uses "self"
21+
#define FC_CLOSURE 0x08 // closure, uses outer scope variables
22+
#define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0
23+
#define FC_REMOVED 0x20 // function redefined while uf_refcount > 0
24+
#define FC_SANDBOX 0x40 // function defined in the sandbox
2425

2526
/* From user function to hashitem and back. */
2627
#define UF2HIKEY(fp) ((fp)->uf_name)
@@ -296,6 +297,8 @@ get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
296297
if (prof_def_func())
297298
func_do_profile(fp);
298299
#endif
300+
if (sandbox)
301+
flags |= FC_SANDBOX;
299302
fp->uf_varargs = TRUE;
300303
fp->uf_flags = flags;
301304
fp->uf_calls = 0;
@@ -688,6 +691,7 @@ call_user_func(
688691
char_u *save_sourcing_name;
689692
linenr_T save_sourcing_lnum;
690693
scid_T save_current_SID;
694+
int using_sandbox = FALSE;
691695
funccall_T *fc;
692696
int save_did_emsg;
693697
static int depth = 0;
@@ -854,6 +858,13 @@ call_user_func(
854858
save_sourcing_name = sourcing_name;
855859
save_sourcing_lnum = sourcing_lnum;
856860
sourcing_lnum = 1;
861+
862+
if (fp->uf_flags & FC_SANDBOX)
863+
{
864+
using_sandbox = TRUE;
865+
++sandbox;
866+
}
867+
857868
/* need space for function name + ("function " + 3) or "[number]" */
858869
len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
859870
+ STRLEN(fp->uf_name) + 20;
@@ -1020,6 +1031,8 @@ call_user_func(
10201031
if (do_profiling == PROF_YES)
10211032
script_prof_restore(&wait_start);
10221033
#endif
1034+
if (using_sandbox)
1035+
--sandbox;
10231036

10241037
if (p_verbose >= 12 && sourcing_name != NULL)
10251038
{
@@ -2429,6 +2442,8 @@ ex_function(exarg_T *eap)
24292442
func_do_profile(fp);
24302443
#endif
24312444
fp->uf_varargs = varargs;
2445+
if (sandbox)
2446+
flags |= FC_SANDBOX;
24322447
fp->uf_flags = flags;
24332448
fp->uf_calls = 0;
24342449
fp->uf_script_ID = current_SID;

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,8 @@ static char *(features[]) =
789789

790790
static int included_patches[] =
791791
{ /* Add new patch number below this line */
792+
/**/
793+
177,
792794
/**/
793795
176,
794796
/**/

0 commit comments

Comments
 (0)