Mercurial > vim
comparison src/dict.c @ 15267:762fccd84b7c v8.1.0642
patch 8.1.0642: swapinfo() leaks memory
commit https://github.com/vim/vim/commit/e6fdf79980c0f2856700d4f46de700293f477429
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Dec 26 22:57:42 2018 +0100
patch 8.1.0642: swapinfo() leaks memory
Problem: swapinfo() leaks memory.
Solution: Avoid allocating the strings twice.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 26 Dec 2018 23:00:06 +0100 |
parents | de63593896b3 |
children | 55ccc2d353bd |
comparison
equal
deleted
inserted
replaced
15266:f4d28214c3d3 | 15267:762fccd84b7c |
---|---|
368 * Returns FAIL when out of memory and when key already exists. | 368 * Returns FAIL when out of memory and when key already exists. |
369 */ | 369 */ |
370 int | 370 int |
371 dict_add_string(dict_T *d, char *key, char_u *str) | 371 dict_add_string(dict_T *d, char *key, char_u *str) |
372 { | 372 { |
373 return dict_add_string_len(d, key, str, -1); | |
374 } | |
375 | |
376 /* | |
377 * Add a string entry to dictionary "d". | |
378 * "str" will be copied to allocated memory. | |
379 * When "len" is -1 use the whole string, otherwise only this many bytes. | |
380 * Returns FAIL when out of memory and when key already exists. | |
381 */ | |
382 int | |
383 dict_add_string_len(dict_T *d, char *key, char_u *str, int len) | |
384 { | |
373 dictitem_T *item; | 385 dictitem_T *item; |
386 char_u *val = NULL; | |
374 | 387 |
375 item = dictitem_alloc((char_u *)key); | 388 item = dictitem_alloc((char_u *)key); |
376 if (item == NULL) | 389 if (item == NULL) |
377 return FAIL; | 390 return FAIL; |
378 item->di_tv.v_type = VAR_STRING; | 391 item->di_tv.v_type = VAR_STRING; |
379 item->di_tv.vval.v_string = str != NULL ? vim_strsave(str) : NULL; | 392 if (str != NULL) |
393 { | |
394 if (len == -1) | |
395 val = vim_strsave(str); | |
396 else | |
397 val = vim_strnsave(str, len); | |
398 } | |
399 item->di_tv.vval.v_string = val; | |
380 if (dict_add(d, item) == FAIL) | 400 if (dict_add(d, item) == FAIL) |
381 { | 401 { |
382 dictitem_free(item); | 402 dictitem_free(item); |
383 return FAIL; | 403 return FAIL; |
384 } | 404 } |