From 51468b764689e7d724a87e6c2b8cdb4e599a3604 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Sun, 8 May 2016 15:57:56 -0700 Subject: add `function --shadow-builtin` flag It's currently too easy for someone to bork their shell by doing something like `function test; return 0; end`. That's obviously a silly, contrived, example but the point is that novice users who learn about functions are prone to do something like that without realizing it will bork the shell. Even expert users who know about the `test` builtin might forget that, say, `pwd` is a builtin. This change adds a `--shadow-builtin` flag that must be specified to indicate you know what you're doing. Fixes #3000 --- src/function.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/function.cpp') diff --git a/src/function.cpp b/src/function.cpp index f717a16f..54559bee 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -141,7 +141,8 @@ function_info_t::function_info_t(const function_data_t &data, const wchar_t *fil named_arguments(data.named_arguments), inherit_vars(snapshot_vars(data.inherit_vars)), is_autoload(autoload), - shadows(data.shadows) {} + shadow_builtin(data.shadow_builtin), + shadow_scope(data.shadow_scope) {} function_info_t::function_info_t(const function_info_t &data, const wchar_t *filename, int def_offset, bool autoload) @@ -152,7 +153,8 @@ function_info_t::function_info_t(const function_info_t &data, const wchar_t *fil named_arguments(data.named_arguments), inherit_vars(data.inherit_vars), is_autoload(autoload), - shadows(data.shadows) {} + shadow_builtin(data.shadow_builtin), + shadow_scope(data.shadow_scope) {} void function_add(const function_data_t &data, const parser_t &parser, int definition_line_offset) { ASSERT_IS_MAIN_THREAD(); @@ -255,10 +257,16 @@ std::map function_get_inherit_vars(const wcstring &name) { return func ? func->inherit_vars : std::map(); } -int function_get_shadows(const wcstring &name) { +int function_get_shadow_builtin(const wcstring &name) { scoped_lock lock(functions_lock); const function_info_t *func = function_get(name); - return func ? func->shadows : false; + return func ? func->shadow_builtin : false; +} + +int function_get_shadow_scope(const wcstring &name) { + scoped_lock lock(functions_lock); + const function_info_t *func = function_get(name); + return func ? func->shadow_scope : false; } bool function_get_desc(const wcstring &name, wcstring *out_desc) { -- cgit v1.2.3