From e3133ed98f5e24edea088b95f121f2ee4e41d5e4 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Thu, 11 Mar 2021 22:13:06 +0100 Subject: autocomplete: Use asprintf don't calculate length twice Through asprintf() we can get rid of malloc() + sprintf(). Also we don't need to calculate the strlen() again since asprintf() returns the bytes printes. Only non UTF-8 characters. But that was true before already. --- src/tools/autocomplete.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/tools') diff --git a/src/tools/autocomplete.c b/src/tools/autocomplete.c index 67cc6cb0..f8d2de62 100644 --- a/src/tools/autocomplete.c +++ b/src/tools/autocomplete.c @@ -33,6 +33,7 @@ * */ +#define _GNU_SOURCE #include #include #include @@ -303,11 +304,15 @@ autocomplete_complete(Autocomplete ac, const gchar* search_str, gboolean quote, static char* _autocomplete_param_common(const char* const input, char* command, autocomplete_func func, Autocomplete ac, gboolean quote, gboolean previous, void* context) { - GString* auto_msg = NULL; + GString* auto_msg; + char* command_cpy; char* result = NULL; - char* command_cpy = malloc(strlen(command) + 2); - sprintf(command_cpy, "%s ", command); - int len = strlen(command_cpy); + int len; + + len = asprintf(&command_cpy, "%s ", command); + if (len == -1) { + return NULL; + } if (strncmp(input, command_cpy, len) == 0) { int inp_len = strlen(input); -- cgit 1.4.1-2-gfad0