aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2017-02-12 21:09:16 +0100
committerGitHub <noreply@github.com>2017-02-12 21:09:16 +0100
commit2ddadaa28cd62cb601779a1d53ea41aee13482de (patch)
treeae1ed11350dc62bad71c56bdeb7eb173e286c3e7 /src
parent30826cb2d621615264607fbd507f6f47e6f2011e (diff)
parentaa56b24ee6553b4417f2c2defdde5be302a868cd (diff)
downloadrneovim-2ddadaa28cd62cb601779a1d53ea41aee13482de.tar.gz
rneovim-2ddadaa28cd62cb601779a1d53ea41aee13482de.tar.bz2
rneovim-2ddadaa28cd62cb601779a1d53ea41aee13482de.zip
Merge #6084 from justinmk/fix-coverity-155968
coverity/155968: resource leak
Diffstat (limited to 'src')
-rw-r--r--src/nvim/globals.h26
-rw-r--r--src/nvim/os/env.c32
-rw-r--r--src/nvim/os/fs.c6
3 files changed, 28 insertions, 36 deletions
diff --git a/src/nvim/globals.h b/src/nvim/globals.h
index 696bdf586f..20a00e1d9c 100644
--- a/src/nvim/globals.h
+++ b/src/nvim/globals.h
@@ -13,10 +13,6 @@
#include "nvim/types.h"
#include "nvim/event/loop.h"
-/*
- * definition of global variables
- */
-
#define IOSIZE (1024+1) // file I/O and sprintf buffer size
#define MAX_MCO 6 // maximum value for 'maxcombine'
@@ -25,11 +21,8 @@
# define MSG_BUF_CLEN (MSG_BUF_LEN / 6) // cell length (worst case: utf-8
// takes 6 bytes for one cell)
-/*
- * Maximum length of a path (for non-unix systems) Make it a bit long, to stay
- * on the safe side. But not too long to put on the stack.
- * TODO(metrix78): Move this to os_defs.h
- */
+// Maximum length of a file path. Make it a bit long, to stay
+// on the safe side. But not too long to put on the stack.
#ifndef MAXPATHL
# ifdef MAXPATHLEN
# define MAXPATHL MAXPATHLEN
@@ -108,12 +101,9 @@ typedef enum {
* They may have different values when the screen wasn't (re)allocated yet
* after setting Rows or Columns (e.g., when starting up).
*/
-
-#define DFLT_COLS 80 /* default value for 'columns' */
-#define DFLT_ROWS 24 /* default value for 'lines' */
-
+#define DFLT_COLS 80 // default value for 'columns'
+#define DFLT_ROWS 24 // default value for 'lines'
EXTERN long Rows INIT(= DFLT_ROWS); // nr of rows in the screen
-
EXTERN long Columns INIT(= DFLT_COLS); // nr of columns in the screen
/*
@@ -889,9 +879,11 @@ EXTERN int swap_exists_action INIT(= SEA_NONE);
EXTERN int swap_exists_did_quit INIT(= FALSE);
/* Selected "quit" at the dialog. */
-EXTERN char_u IObuff[IOSIZE]; /* sprintf's are done in this buffer */
-EXTERN char_u NameBuff[MAXPATHL]; /* buffer for expanding file names */
-EXTERN char_u msg_buf[MSG_BUF_LEN]; /* small buffer for messages */
+EXTERN char_u IObuff[IOSIZE]; ///< Buffer for sprintf, I/O, etc.
+EXTERN char_u NameBuff[MAXPATHL]; ///< Buffer for expanding file names
+EXTERN char_u msg_buf[MSG_BUF_LEN]; ///< Small buffer for messages
+EXTERN char os_buf[MAX(MAXPATHL, IOSIZE)]; ///< Buffer for the os/ layer
+
/* When non-zero, postpone redrawing. */
EXTERN int RedrawingDisabled INIT(= 0);
diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c
index 5a3c1ef2c8..1697d5edb2 100644
--- a/src/nvim/os/env.c
+++ b/src/nvim/os/env.c
@@ -164,11 +164,11 @@ static char_u *homedir = NULL;
void init_homedir(void)
{
- // In case we are called a second time (when 'encoding' changes).
+ // In case we are called a second time.
xfree(homedir);
homedir = NULL;
- char_u *var = (char_u *)os_getenv("HOME");
+ const char *var = os_getenv("HOME");
#ifdef WIN32
// Typically, $HOME is not defined on Windows, unless the user has
@@ -182,10 +182,10 @@ void init_homedir(void)
homepath = "\\";
}
if (homedrive != NULL && strlen(homedrive) + strlen(homepath) < MAXPATHL) {
- snprintf((char *)NameBuff, MAXPATHL, "%s%s", homedrive, homepath);
- if (NameBuff[0] != NUL) {
- var = NameBuff;
- vim_setenv("HOME", (char *)NameBuff);
+ snprintf(os_buf, MAXPATHL, "%s%s", homedrive, homepath);
+ if (os_buf[0] != NUL) {
+ var = os_buf;
+ vim_setenv("HOME", os_buf);
}
}
}
@@ -195,17 +195,16 @@ void init_homedir(void)
#ifdef UNIX
// Change to the directory and get the actual path. This resolves
// links. Don't do it when we can't return.
- if (os_dirname(NameBuff, MAXPATHL) == OK
- && os_chdir((char *)NameBuff) == 0) {
- if (!os_chdir((char *)var) && os_dirname(IObuff, IOSIZE) == OK) {
- var = IObuff;
+ if (os_dirname((char_u *)os_buf, MAXPATHL) == OK && os_chdir(os_buf) == 0) {
+ if (!os_chdir(var) && os_dirname(IObuff, IOSIZE) == OK) {
+ var = (char *)IObuff;
}
- if (os_chdir((char *)NameBuff) != 0) {
+ if (os_chdir(os_buf) != 0) {
EMSG(_(e_prev_dir));
}
}
#endif
- homedir = vim_strsave(var);
+ homedir = vim_strsave((char_u *)var);
}
}
@@ -870,11 +869,12 @@ bool os_setenv_append_path(const char *fname)
return false;
}
const char *tail = (char *)path_tail_with_sep((char_u *)fname);
- const char *dir = (char *)vim_strnsave((char_u *)fname,
- (size_t)(tail - fname));
+ size_t dirlen = (size_t)(tail - fname);
+ assert(tail >= fname && dirlen + 1 < sizeof(os_buf));
+ xstrlcpy(os_buf, fname, dirlen + 1);
const char *path = os_getenv("PATH");
const size_t pathlen = path ? strlen(path) : 0;
- const size_t newlen = pathlen + strlen(dir) + 2;
+ const size_t newlen = pathlen + dirlen + 2;
if (newlen < MAX_ENVPATHLEN) {
char *temp = xmalloc(newlen);
if (pathlen == 0) {
@@ -883,7 +883,7 @@ bool os_setenv_append_path(const char *fname)
xstrlcpy(temp, path, newlen);
xstrlcat(temp, ENV_SEPSTR, newlen);
}
- xstrlcat(temp, dir, newlen);
+ xstrlcat(temp, os_buf, newlen);
os_setenv("PATH", temp, 1);
xfree(temp);
return true;
diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c
index 097c672887..e930561234 100644
--- a/src/nvim/os/fs.c
+++ b/src/nvim/os/fs.c
@@ -275,8 +275,8 @@ static bool is_executable(const char *name)
static bool is_executable_ext(char *name, const char *pathext)
FUNC_ATTR_NONNULL_ALL
{
- xstrlcpy((char *)NameBuff, name, sizeof(NameBuff));
- char *buf_end = xstrchrnul((char *)NameBuff, '\0');
+ xstrlcpy(os_buf, name, sizeof(os_buf));
+ char *buf_end = xstrchrnul(os_buf, '\0');
for (const char *ext = pathext; *ext; ext++) {
// Skip the extension if there is no suffix after a '.'.
if (ext[0] == '.' && (ext[1] == '\0' || ext[1] == ENV_SEPCHAR)) {
@@ -287,7 +287,7 @@ static bool is_executable_ext(char *name, const char *pathext)
const char *ext_end = xstrchrnul(ext, ENV_SEPCHAR);
STRLCPY(buf_end, ext, ext_end - ext + 1);
- if (is_executable((char *)NameBuff)) {
+ if (is_executable(os_buf)) {
return true;
}