summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2018-12-26 22:57:42 +0100
committerBram Moolenaar <Bram@vim.org>2018-12-26 22:57:42 +0100
commite6fdf79980c0f2856700d4f46de700293f477429 (patch)
tree3234f99e0464e97f36e6b5b4086f95b469806552
parentc57463c9c6ee893285f553e0ac3b2fe5935f16b8 (diff)
patch 8.1.0642: swapinfo() leaks memoryv8.1.0642
Problem: swapinfo() leaks memory. Solution: Avoid allocating the strings twice.
-rw-r--r--src/dict.c22
-rw-r--r--src/memline.c22
-rw-r--r--src/proto/dict.pro1
-rw-r--r--src/version.c2
4 files changed, 32 insertions, 15 deletions
diff --git a/src/dict.c b/src/dict.c
index 34ba3e0e41..9b85900278 100644
--- a/src/dict.c
+++ b/src/dict.c
@@ -370,13 +370,33 @@ dict_add_number(dict_T *d, char *key, varnumber_T nr)
int
dict_add_string(dict_T *d, char *key, char_u *str)
{
+ return dict_add_string_len(d, key, str, -1);
+}
+
+/*
+ * Add a string entry to dictionary "d".
+ * "str" will be copied to allocated memory.
+ * When "len" is -1 use the whole string, otherwise only this many bytes.
+ * Returns FAIL when out of memory and when key already exists.
+ */
+ int
+dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
+{
dictitem_T *item;
+ char_u *val = NULL;
item = dictitem_alloc((char_u *)key);
if (item == NULL)
return FAIL;
item->di_tv.v_type = VAR_STRING;
- item->di_tv.vval.v_string = str != NULL ? vim_strsave(str) : NULL;
+ if (str != NULL)
+ {
+ if (len == -1)
+ val = vim_strsave(str);
+ else
+ val = vim_strnsave(str, len);
+ }
+ item->di_tv.vval.v_string = val;
if (dict_add(d, item) == FAIL)
{
dictitem_free(item);
diff --git a/src/memline.c b/src/memline.c
index 9f082660a5..d79e7ce8c0 100644
--- a/src/memline.c
+++ b/src/memline.c
@@ -2055,21 +2055,16 @@ get_b0_dict(char_u *fname, dict_T *d)
if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0))
{
if (ml_check_b0_id(&b0) == FAIL)
- dict_add_string(d, "error",
- vim_strsave((char_u *)"Not a swap file"));
+ dict_add_string(d, "error", (char_u *)"Not a swap file");
else if (b0_magic_wrong(&b0))
- dict_add_string(d, "error",
- vim_strsave((char_u *)"Magic number mismatch"));
+ dict_add_string(d, "error", (char_u *)"Magic number mismatch");
else
{
/* we have swap information */
- dict_add_string(d, "version", vim_strnsave(b0.b0_version, 10));
- dict_add_string(d, "user",
- vim_strnsave(b0.b0_uname, B0_UNAME_SIZE));
- dict_add_string(d, "host",
- vim_strnsave(b0.b0_hname, B0_HNAME_SIZE));
- dict_add_string(d, "fname",
- vim_strnsave(b0.b0_fname, B0_FNAME_SIZE_ORG));
+ dict_add_string_len(d, "version", b0.b0_version, 10);
+ dict_add_string_len(d, "user", b0.b0_uname, B0_UNAME_SIZE);
+ dict_add_string_len(d, "host", b0.b0_hname, B0_HNAME_SIZE);
+ dict_add_string_len(d, "fname", b0.b0_fname, B0_FNAME_SIZE_ORG);
dict_add_number(d, "pid", char_to_long(b0.b0_pid));
dict_add_number(d, "mtime", char_to_long(b0.b0_mtime));
@@ -2080,12 +2075,11 @@ get_b0_dict(char_u *fname, dict_T *d)
}
}
else
- dict_add_string(d, "error",
- vim_strsave((char_u *)"Cannot read file"));
+ dict_add_string(d, "error", (char_u *)"Cannot read file");
close(fd);
}
else
- dict_add_string(d, "error", vim_strsave((char_u *)"Cannot open file"));
+ dict_add_string(d, "error", (char_u *)"Cannot open file");
}
#endif
diff --git a/src/proto/dict.pro b/src/proto/dict.pro
index 94ed2a0855..b09a647bd4 100644
--- a/src/proto/dict.pro
+++ b/src/proto/dict.pro
@@ -15,6 +15,7 @@ dict_T *dict_copy(dict_T *orig, int deep, int copyID);
int dict_add(dict_T *d, dictitem_T *item);
int dict_add_number(dict_T *d, char *key, varnumber_T nr);
int dict_add_string(dict_T *d, char *key, char_u *str);
+int dict_add_string_len(dict_T *d, char *key, char_u *str, int len);
int dict_add_list(dict_T *d, char *key, list_T *list);
int dict_add_dict(dict_T *d, char *key, dict_T *dict);
long dict_len(dict_T *d);
diff --git a/src/version.c b/src/version.c
index 3150da92f5..dc1135827d 100644
--- a/src/version.c
+++ b/src/version.c
@@ -800,6 +800,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 642,
+/**/
641,
/**/
640,