diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2019-03-01 02:30:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-01 02:30:05 +0100 |
commit | e7bd49d835e26c96b2ee152e5b697e20ab749650 (patch) | |
tree | b02e91d42c8b8a8ef578076ae8bf8deef26b94b5 /src | |
parent | 533d4a36ec03626e9d796ef7e2a9aa3c0e1ce7bf (diff) | |
parent | 403922b1b47c1f03272e2bdd600dcc02db481389 (diff) | |
download | rneovim-e7bd49d835e26c96b2ee152e5b697e20ab749650.tar.gz rneovim-e7bd49d835e26c96b2ee152e5b697e20ab749650.tar.bz2 rneovim-e7bd49d835e26c96b2ee152e5b697e20ab749650.zip |
Merge #7920 'env: use libuv v1.12 getenv/setenv API'
Diffstat (limited to 'src')
-rwxr-xr-x | src/clint.py | 38 | ||||
-rw-r--r-- | src/nvim/hashtab.h | 2 | ||||
-rw-r--r-- | src/nvim/lib/khash.h | 30 | ||||
-rw-r--r-- | src/nvim/main.c | 7 | ||||
-rw-r--r-- | src/nvim/map.c | 32 | ||||
-rw-r--r-- | src/nvim/map.h | 9 | ||||
-rw-r--r-- | src/nvim/mbyte.c | 3 | ||||
-rw-r--r-- | src/nvim/memory.c | 2 | ||||
-rw-r--r-- | src/nvim/os/env.c | 137 | ||||
-rw-r--r-- | src/nvim/os/fs_defs.h | 4 | ||||
-rw-r--r-- | src/nvim/os/pty_process_unix.c | 12 | ||||
-rw-r--r-- | src/nvim/path.c | 2 |
12 files changed, 203 insertions, 75 deletions
diff --git a/src/clint.py b/src/clint.py index 34af5d15fd..1ef31820ee 100755 --- a/src/clint.py +++ b/src/clint.py @@ -29,10 +29,10 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -"""Does neovim-lint on c files. +"""Lints C files in the Neovim source tree. The goal of this script is to identify places in the code that *may* -be in non-compliance with neovim style. It does not attempt to fix +be in non-compliance with Neovim style. It does not attempt to fix up these problems -- the point is to educate. It does also not attempt to find all problems, or to ensure that everything it does find is legitimately a problem. @@ -88,7 +88,7 @@ Syntax: clint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...] * [whitespace/braces] { should almost always be at the end of the previous line * [build/include] Include the directory when naming .h files - * [runtime/int] Use int16/int64/etc, rather than the C type. + * [runtime/int] Use int16_t/int64_t/etc, rather than the C type. Every problem is given a confidence score from 1-5, with 5 meaning we are certain of the problem, and 1 meaning it could be a legitimate construct. @@ -1487,6 +1487,37 @@ def CheckMemoryFunctions(filename, clean_lines, linenum, error): '...) instead of ' + function + '...).') +os_functions = ( + ('setenv(', 'os_setenv('), + ('getenv(', 'os_getenv('), + ('_wputenv(', 'os_setenv('), + ('_putenv_s(', 'os_setenv('), + ('putenv(', 'os_setenv('), + ('unsetenv(', 'os_unsetenv('), +) + + +def CheckOSFunctions(filename, clean_lines, linenum, error): + """Checks for calls to invalid functions. + + Args: + filename: The name of the current file. + clean_lines: A CleansedLines instance containing the file. + linenum: The number of the line to check. + error: The function to call with any errors found. + """ + line = clean_lines.elided[linenum] + for function, suggested_function in os_functions: + ix = line.find(function) + # Comparisons made explicit for clarity -- pylint: + # disable=g-explicit-bool-comparison + if ix >= 0 and (ix == 0 or (not line[ix - 1].isalnum() and + line[ix - 1] not in ('_', '.', '>'))): + error(filename, linenum, 'runtime/os_fn', 2, + 'Use ' + suggested_function + + '...) instead of ' + function + '...).') + + # Matches invalid increment: *count++, which moves pointer instead of # incrementing a value. _RE_PATTERN_INVALID_INCREMENT = re.compile( @@ -3370,6 +3401,7 @@ def ProcessLine(filename, file_extension, clean_lines, line, nesting_state, error) CheckPosixThreading(filename, clean_lines, line, error) CheckMemoryFunctions(filename, clean_lines, line, error) + CheckOSFunctions(filename, clean_lines, line, error) for check_fn in extra_check_functions: check_fn(filename, clean_lines, line, error) diff --git a/src/nvim/hashtab.h b/src/nvim/hashtab.h index 973b97d476..a70a8bea63 100644 --- a/src/nvim/hashtab.h +++ b/src/nvim/hashtab.h @@ -19,7 +19,7 @@ typedef size_t hash_T; #define HASHITEM_EMPTY(hi) ((hi)->hi_key == NULL \ || (hi)->hi_key == (char_u *)&hash_removed) -/// A hastable item. +/// Hashtable item. /// /// Each item has a NUL terminated string key. /// A key can appear only once in the table. diff --git a/src/nvim/lib/khash.h b/src/nvim/lib/khash.h index 8287cb14da..b2994a3159 100644 --- a/src/nvim/lib/khash.h +++ b/src/nvim/lib/khash.h @@ -24,24 +24,24 @@ */ /* - An example: + Example: #include "nvim/khash.h" KHASH_MAP_INIT_INT(32, char) int main() { - int ret, is_missing; - khiter_t k; - khash_t(32) *h = kh_init(32); - k = kh_put(32, h, 5, &ret); - kh_value(h, k) = 10; - k = kh_get(32, h, 10); - is_missing = (k == kh_end(h)); - k = kh_get(32, h, 5); - kh_del(32, h, k); - for (k = kh_begin(h); k != kh_end(h); ++k) - if (kh_exist(h, k)) kh_value(h, k) = 1; - kh_destroy(32, h); - return 0; + int ret, is_missing; + khiter_t k; + khash_t(32) *h = kh_init(32); + k = kh_put(32, h, 5, &ret); + kh_value(h, k) = 10; + k = kh_get(32, h, 10); + is_missing = (k == kh_end(h)); + k = kh_get(32, h, 5); + kh_del(32, h, k); + for (k = kh_begin(h); k != kh_end(h); ++k) + if (kh_exist(h, k)) kh_value(h, k) = 1; + kh_destroy(32, h); + return 0; } */ @@ -539,7 +539,7 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key) @param r Extra return code: -1 if the operation failed; 0 if the key is present in the hash table; 1 if the bucket is empty (never used); 2 if the element in - the bucket has been deleted [int*] + the bucket has been deleted [int*] @return Iterator to the inserted element [khint_t] */ #define kh_put(name, h, k, r) kh_put_##name(h, k, r) diff --git a/src/nvim/main.c b/src/nvim/main.c index 251a54ad5b..79513b1c21 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -184,6 +184,7 @@ bool event_teardown(void) void early_init(void) { log_init(); + env_init(); fs_init(); handle_init(); eval_init(); // init global variables @@ -1769,7 +1770,7 @@ static bool do_user_initialization(void) FUNC_ATTR_WARN_UNUSED_RESULT { bool do_exrc = p_exrc; - if (process_env("VIMINIT") == OK) { + if (execute_env("VIMINIT") == OK) { do_exrc = p_exrc; return do_exrc; } @@ -1814,7 +1815,7 @@ static bool do_user_initialization(void) } while (iter != NULL); xfree(config_dirs); } - if (process_env("EXINIT") == OK) { + if (execute_env("EXINIT") == OK) { do_exrc = p_exrc; return do_exrc; } @@ -1878,7 +1879,7 @@ static void source_startup_scripts(const mparm_T *const parmp) /// /// @return FAIL if the environment variable was not executed, /// OK otherwise. -static int process_env(char *env) +static int execute_env(char *env) FUNC_ATTR_NONNULL_ALL { const char *initstr = os_getenv(env); diff --git a/src/nvim/map.c b/src/nvim/map.c index 53ab734802..9b6f57a56f 100644 --- a/src/nvim/map.c +++ b/src/nvim/map.c @@ -1,6 +1,13 @@ // This is an open source non-commercial project. Dear PVS-Studio, please check // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com +/// +/// map.c: khash.h wrapper +/// +/// NOTE: Callers must manage memory (allocate) for keys and values. +/// khash.h does not make its own copy of the key or value. +/// + #include <stdlib.h> #include <stdbool.h> #include <string.h> @@ -72,6 +79,16 @@ return kh_get(T##_##U##_map, map->table, key) != kh_end(map->table); \ } \ \ + T map_##T##_##U##_key(Map(T, U) *map, T key) \ + { \ + khiter_t k; \ + \ + if ((k = kh_get(T##_##U##_map, map->table, key)) == kh_end(map->table)) { \ + abort(); /* Caller must check map_has(). */ \ + } \ + \ + return kh_key(map->table, k); \ + } \ U map_##T##_##U##_put(Map(T, U) *map, T key, U value) \ { \ int ret; \ @@ -167,3 +184,18 @@ MAP_IMPL(String, MsgpackRpcRequestHandler, MSGPACK_HANDLER_INITIALIZER) #define KVEC_INITIALIZER { .size = 0, .capacity = 0, .items = NULL } MAP_IMPL(HlEntry, int, DEFAULT_INITIALIZER) MAP_IMPL(String, handle_T, 0) + + +/// Deletes a key:value pair from a string:pointer map, and frees the +/// storage of both key and value. +/// +void pmap_del2(PMap(cstr_t) *map, const char *key) +{ + if (pmap_has(cstr_t)(map, key)) { + void *k = (void *)pmap_key(cstr_t)(map, key); + void *v = pmap_get(cstr_t)(map, key); + pmap_del(cstr_t)(map, key); + xfree(k); + xfree(v); + } +} diff --git a/src/nvim/map.h b/src/nvim/map.h index 0e4308b953..75ab64cca4 100644 --- a/src/nvim/map.h +++ b/src/nvim/map.h @@ -25,11 +25,15 @@ void map_##T##_##U##_free(Map(T, U) *map); \ U map_##T##_##U##_get(Map(T, U) *map, T key); \ bool map_##T##_##U##_has(Map(T, U) *map, T key); \ + T map_##T##_##U##_key(Map(T, U) *map, T key); \ U map_##T##_##U##_put(Map(T, U) *map, T key, U value); \ U *map_##T##_##U##_ref(Map(T, U) *map, T key, bool put); \ U map_##T##_##U##_del(Map(T, U) *map, T key); \ void map_##T##_##U##_clear(Map(T, U) *map); +// +// NOTE: Keys AND values must be allocated! khash.h does not make a copy. +// MAP_DECLS(int, int) MAP_DECLS(cstr_t, ptr_t) MAP_DECLS(ptr_t, ptr_t) @@ -43,6 +47,7 @@ MAP_DECLS(String, handle_T) #define map_free(T, U) map_##T##_##U##_free #define map_get(T, U) map_##T##_##U##_get #define map_has(T, U) map_##T##_##U##_has +#define map_key(T, U) map_##T##_##U##_key #define map_put(T, U) map_##T##_##U##_put #define map_ref(T, U) map_##T##_##U##_ref #define map_del(T, U) map_##T##_##U##_del @@ -52,7 +57,9 @@ MAP_DECLS(String, handle_T) #define pmap_free(T) map_free(T, ptr_t) #define pmap_get(T) map_get(T, ptr_t) #define pmap_has(T) map_has(T, ptr_t) +#define pmap_key(T) map_key(T, ptr_t) #define pmap_put(T) map_put(T, ptr_t) +/// @see pmap_del2 #define pmap_del(T) map_del(T, ptr_t) #define pmap_clear(T) map_clear(T, ptr_t) @@ -62,4 +69,6 @@ MAP_DECLS(String, handle_T) #define map_foreach_value(map, value, block) \ kh_foreach_value(map->table, value, block) +void pmap_del2(PMap(cstr_t) *map, const char *key); + #endif // NVIM_MAP_H diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c index ead6b4405d..1cf045d7e0 100644 --- a/src/nvim/mbyte.c +++ b/src/nvim/mbyte.c @@ -1375,6 +1375,7 @@ int utf8_to_utf16(const char *str, wchar_t **strw) int utf16_to_utf8(const wchar_t *strw, char **str) FUNC_ATTR_NONNULL_ALL { + *str = NULL; // Compute the space required to store the string as UTF-8. DWORD utf8_len = WideCharToMultiByte(CP_UTF8, 0, @@ -1400,7 +1401,7 @@ int utf16_to_utf8(const wchar_t *strw, char **str) NULL, NULL); if (utf8_len == 0) { - free(*str); + xfree(*str); *str = NULL; return GetLastError(); } diff --git a/src/nvim/memory.c b/src/nvim/memory.c index b49b521bc9..0f402611df 100644 --- a/src/nvim/memory.c +++ b/src/nvim/memory.c @@ -109,7 +109,7 @@ void *xmalloc(size_t size) return ret; } -/// free wrapper that returns delegates to the backing memory manager +/// free() wrapper that delegates to the backing memory manager void xfree(void *ptr) { free(ptr); diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index c6794e4be5..8a8220b6e7 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -19,6 +19,7 @@ #include "nvim/eval.h" #include "nvim/ex_getln.h" #include "nvim/version.h" +#include "nvim/map.h" #ifdef WIN32 #include "nvim/mbyte.h" // for utf8_to_utf16, utf16_to_utf8 @@ -32,65 +33,119 @@ #include <sys/utsname.h> #endif +// Because `uv_os_getenv` requires allocating, we must manage a map to maintain +// the behavior of `os_getenv`. +static PMap(cstr_t) *envmap; +static uv_mutex_t mutex; + +void env_init(void) +{ + envmap = pmap_new(cstr_t)(); + uv_mutex_init(&mutex); +} + /// Like getenv(), but returns NULL if the variable is empty. const char *os_getenv(const char *name) FUNC_ATTR_NONNULL_ALL { - const char *e = getenv(name); - return e == NULL || *e == NUL ? NULL : e; + char *e; + size_t size = 64; + if (name[0] == '\0') { + return NULL; + } + uv_mutex_lock(&mutex); + if (pmap_has(cstr_t)(envmap, name) + && !!(e = (char *)pmap_get(cstr_t)(envmap, name))) { + if (e[0] != '\0') { + // Found non-empty cached env var. + // NOTE: This risks incoherence if an in-process library changes the + // environment without going through our os_setenv() wrapper. If + // that turns out to be a problem, we can just remove this codepath. + goto end; + } + pmap_del2(envmap, name); + } + e = xmalloc(size); + int r = uv_os_getenv(name, e, &size); + if (r == UV_ENOBUFS) { + e = xrealloc(e, size); + r = uv_os_getenv(name, e, &size); + } + if (r != 0 || size == 0 || e[0] == '\0') { + xfree(e); + e = NULL; + if (r != 0 && r != UV_ENOENT && r != UV_UNKNOWN) { + ELOG("uv_os_getenv(%s) failed: %d %s", name, r, uv_err_name(r)); + } + goto end; + } + pmap_put(cstr_t)(envmap, xstrdup(name), e); +end: + uv_mutex_unlock(&mutex); + return (e == NULL || size == 0 || e[0] == '\0') ? NULL : e; } -/// Returns `true` if the environment variable, `name`, has been defined -/// (even if empty). +/// Returns true if environment variable `name` is defined (even if empty). +/// Returns false if not found (UV_ENOENT) or other failure. bool os_env_exists(const char *name) FUNC_ATTR_NONNULL_ALL { - return getenv(name) != NULL; + if (name[0] == '\0') { + return false; + } + // Use a tiny buffer because we don't care about the value: if uv_os_getenv() + // returns UV_ENOBUFS, the env var was found. + char buf[1]; + size_t size = sizeof(buf); + int r = uv_os_getenv(name, buf, &size); + assert(r != UV_EINVAL); + if (r != 0 && r != UV_ENOENT && r != UV_ENOBUFS) { + ELOG("uv_os_getenv(%s) failed: %d %s", name, r, uv_err_name(r)); + } + return (r == 0 || r == UV_ENOBUFS); } int os_setenv(const char *name, const char *value, int overwrite) FUNC_ATTR_NONNULL_ALL { -#ifdef WIN32 - size_t envbuflen = strlen(name) + strlen(value) + 2; - char *envbuf = xmalloc(envbuflen); - snprintf(envbuf, envbuflen, "%s=%s", name, value); - - wchar_t *p; - utf8_to_utf16(envbuf, &p); - xfree(envbuf); - if (p == NULL) { + if (name[0] == '\0') { return -1; } - _wputenv(p); - xfree(p); // Unlike Unix systems, we can free the string for _wputenv(). - return 0; -#elif defined(HAVE_SETENV) - return setenv(name, value, overwrite); -#elif defined(HAVE_PUTENV_S) +#ifdef WIN32 if (!overwrite && os_getenv(name) != NULL) { return 0; } - if (_putenv_s(name, value) == 0) { +#else + if (!overwrite && os_env_exists(name)) { return 0; } - return -1; -#else -# error "This system has no implementation available for os_setenv()" #endif + uv_mutex_lock(&mutex); + pmap_del2(envmap, name); + int r = uv_os_setenv(name, value); + assert(r != UV_EINVAL); + if (r != 0) { + ELOG("uv_os_setenv(%s) failed: %d %s", name, r, uv_err_name(r)); + } + uv_mutex_unlock(&mutex); + return r == 0 ? 0 : -1; } /// Unset environment variable -/// -/// For systems where unsetenv() is not available the value will be set as an -/// empty string int os_unsetenv(const char *name) + FUNC_ATTR_NONNULL_ALL { -#ifdef HAVE_UNSETENV - return unsetenv(name); -#else - return os_setenv(name, "", 1); -#endif + if (name[0] == '\0') { + return -1; + } + uv_mutex_lock(&mutex); + pmap_del2(envmap, name); + int r = uv_os_unsetenv(name); + if (r != 0) { + ELOG("uv_os_unsetenv(%s) failed: %d %s", name, r, uv_err_name(r)); + } + uv_mutex_unlock(&mutex); + return r == 0 ? 0 : -1; } char *os_getenvname_at_index(size_t index) @@ -527,7 +582,7 @@ static char *remove_tail(char *path, char *pend, char *dirname) return pend; } -/// Iterate over a delimited list. +/// Iterates $PATH-like delimited list `val`. /// /// @note Environment variables must not be modified during iteration. /// @@ -562,7 +617,7 @@ const void *vim_env_iter(const char delim, } } -/// Iterate over a delimited list in reverse order. +/// Iterates $PATH-like delimited list `val` in reverse order. /// /// @note Environment variables must not be modified during iteration. /// @@ -599,11 +654,12 @@ const void *vim_env_iter_rev(const char delim, } } -/// Vim's version of getenv(). -/// Special handling of $HOME, $VIM and $VIMRUNTIME, allowing the user to -/// override the vim runtime directory at runtime. Also does ACP to 'enc' -/// conversion for Win32. Result must be freed by the caller. +/// Vim getenv() wrapper with special handling of $HOME, $VIM, $VIMRUNTIME, +/// allowing the user to override the Nvim runtime directory at runtime. +/// Result must be freed by the caller. +/// /// @param name Environment variable to expand +/// @return [allocated] Expanded environment variable, or NULL char *vim_getenv(const char *name) { // init_path() should have been called before now. @@ -869,9 +925,8 @@ char_u * home_replace_save(buf_T *buf, char_u *src) FUNC_ATTR_NONNULL_RET return dst; } -/// Our portable version of setenv. -/// Has special handling for $VIMRUNTIME to keep the localization machinery -/// sane. +/// Vim setenv() wrapper with special handling for $VIMRUNTIME to keep the +/// localization machinery sane. void vim_setenv(const char *name, const char *val) { os_setenv(name, val, 1); diff --git a/src/nvim/os/fs_defs.h b/src/nvim/os/fs_defs.h index 2277d926b3..c9b148a5aa 100644 --- a/src/nvim/os/fs_defs.h +++ b/src/nvim/os/fs_defs.h @@ -21,9 +21,7 @@ typedef struct { uv_dirent_t ent; ///< @private The entry information. } Directory; -/// Function to convert libuv error to char * error description -/// -/// negative libuv error codes are returned by a number of os functions. +/// Converts libuv error (negative int) to error description string. #define os_strerror uv_strerror // Values returned by os_nodetype() diff --git a/src/nvim/os/pty_process_unix.c b/src/nvim/os/pty_process_unix.c index bcf57e1b5b..5fdf0e6181 100644 --- a/src/nvim/os/pty_process_unix.c +++ b/src/nvim/os/pty_process_unix.c @@ -157,11 +157,11 @@ static void init_child(PtyProcess *ptyproc) // New session/process-group. #6530 setsid(); - unsetenv("COLUMNS"); - unsetenv("LINES"); - unsetenv("TERMCAP"); - unsetenv("COLORTERM"); - unsetenv("COLORFGBG"); + os_unsetenv("COLUMNS"); + os_unsetenv("LINES"); + os_unsetenv("TERMCAP"); + os_unsetenv("COLORTERM"); + os_unsetenv("COLORFGBG"); signal(SIGCHLD, SIG_DFL); signal(SIGHUP, SIG_DFL); @@ -177,7 +177,7 @@ static void init_child(PtyProcess *ptyproc) } char *prog = ptyproc->process.argv[0]; - setenv("TERM", ptyproc->term_name ? ptyproc->term_name : "ansi", 1); + os_setenv("TERM", ptyproc->term_name ? ptyproc->term_name : "ansi", 1); execvp(prog, ptyproc->process.argv); ELOG("execvp failed: %s: %s", strerror(errno), prog); _exit(122); // 122 is EXEC_FAILED in the Vim source. diff --git a/src/nvim/path.c b/src/nvim/path.c index 7903e3f4f4..a706e32773 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -2271,7 +2271,7 @@ int path_is_absolute(const char_u *fname) void path_guess_exepath(const char *argv0, char *buf, size_t bufsize) FUNC_ATTR_NONNULL_ALL { - char *path = getenv("PATH"); + const char *path = os_getenv("PATH"); if (path == NULL || path_is_absolute((char_u *)argv0)) { xstrlcpy(buf, argv0, bufsize); |