summaryrefslogtreecommitdiffstats
path: root/src/dict.c
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 /src/dict.c
parentc57463c9c6ee893285f553e0ac3b2fe5935f16b8 (diff)
patch 8.1.0642: swapinfo() leaks memoryv8.1.0642
Problem: swapinfo() leaks memory. Solution: Avoid allocating the strings twice.
Diffstat (limited to 'src/dict.c')
-rw-r--r--src/dict.c22
1 files changed, 21 insertions, 1 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);