diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 00d863cbb..68f6f6be3 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -27,6 +27,7 @@ New features:: Bug fixes:: * core: fix display glitch in command errors when a wide char is set in option weechat.look.command_chars (issue #1871) + * api: readjust string size in function string_dyn_free when string is not freed * irc: fix join of channels in "autojoin" server option on first connection to server if auto reconnection is performed (issue #1873) * typing: fix crash when pointer buffer is not received in callback for signal "input_text_changed" (issue #1869) diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c index 01dce0999..d80be2df9 100644 --- a/src/core/wee-eval.c +++ b/src/core/wee-eval.c @@ -334,10 +334,7 @@ eval_string_range_chars (const char *range) end: if (string) - { - result = *string; - string_dyn_free (string, 0); - } + result = string_dyn_free (string, 0); return (result) ? result : strdup (""); } diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 346cfc265..368cee7d2 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -4409,7 +4409,6 @@ string_dyn_copy (char **string, const char *new_string) if (!string_realloc) return 0; ptr_string_dyn->string = string_realloc; - *string = string_realloc; ptr_string_dyn->size_alloc = new_size_alloc; } @@ -4475,7 +4474,6 @@ string_dyn_concat (char **string, const char *add, int bytes) return 0; } ptr_string_dyn->string = string_realloc; - *string = string_realloc; ptr_string_dyn->size_alloc = new_size_alloc; } @@ -4496,8 +4494,11 @@ string_dyn_concat (char **string, const char *add, int bytes) * string_dyn_alloc or a string pointer modified by string_dyn_concat. * * If free_string == 1, the string itself is freed in the structure. - * Otherwise the pointer (*string) remains valid after this call, and - * the caller must manually free the string with a call to free(). + * + * If free_string == 0, the pointer (*string) remains valid after this call, + * and the caller must manually free the string with a call to free(). + * Be careful, the pointer in *string may change after this call because + * the string can be reallocated to its exact size. * * Returns the pointer to the string if "free_string" is 0 (string * pointer is still valid), or NULL if "free_string" is 1 (string @@ -4508,7 +4509,7 @@ char * string_dyn_free (char **string, int free_string) { struct t_string_dyn *ptr_string_dyn; - char *ptr_string; + char *ptr_string, *string_realloc; if (!string || !*string) return NULL; @@ -4522,6 +4523,14 @@ string_dyn_free (char **string, int free_string) } else { + /* if needed, realloc the string to its exact size */ + if (ptr_string_dyn->size_alloc > ptr_string_dyn->size) + { + string_realloc = realloc (ptr_string_dyn->string, + ptr_string_dyn->size); + if (string_realloc) + ptr_string_dyn->string = string_realloc; + } ptr_string = ptr_string_dyn->string; } diff --git a/src/plugins/buflist/buflist-bar-item.c b/src/plugins/buflist/buflist-bar-item.c index 1c7abf3c9..d7dcc0868 100644 --- a/src/plugins/buflist/buflist-bar-item.c +++ b/src/plugins/buflist/buflist-bar-item.c @@ -574,8 +574,7 @@ buflist_bar_item_buflist_cb (const void *pointer, void *data, -1); } } - str_hotlist = *hotlist; - weechat_string_dyn_free (hotlist, 0); + str_hotlist = weechat_string_dyn_free (hotlist, 0); } } weechat_hashtable_set ( @@ -655,15 +654,15 @@ buflist_bar_item_buflist_cb (const void *pointer, void *data, line_number++; } - str_buflist = *buflist; + str_buflist = weechat_string_dyn_free (buflist, 0); goto end; error: + weechat_string_dyn_free (buflist, 1); str_buflist = NULL; end: - weechat_string_dyn_free (buflist, 0); weechat_arraylist_free (buffers); if ((line_number_current_buffer != old_line_number_current_buffer[item_index]) diff --git a/src/plugins/irc/irc-join.c b/src/plugins/irc/irc-join.c index 82f8566b2..609c2b50c 100644 --- a/src/plugins/irc/irc-join.c +++ b/src/plugins/irc/irc-join.c @@ -372,10 +372,7 @@ irc_join_build_string (struct t_arraylist *arraylist) end: if (channels) - { - result = *channels; - weechat_string_dyn_free (channels, 0); - } + result = weechat_string_dyn_free (channels, 0); if (keys) weechat_string_dyn_free (keys, 1);