about summary refs log tree commit diff stats
path: root/src/tools
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2021-03-11 22:13:06 +0100
committerMichael Vetter <jubalh@iodoru.org>2021-03-11 22:24:02 +0100
commite3133ed98f5e24edea088b95f121f2ee4e41d5e4 (patch)
tree39b7a2e58c37faee8c09d5fee6138203aafb11ec /src/tools
parentc1ccaee58fe576e4e8f232f7e20ebb912ffb2a1a (diff)
downloadprofani-tty-e3133ed98f5e24edea088b95f121f2ee4e41d5e4.tar.gz
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.
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/autocomplete.c13
1 files changed, 9 insertions, 4 deletions
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 <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -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);