1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

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.
This commit is contained in:
Michael Vetter 2021-03-11 22:13:06 +01:00
parent c1ccaee58f
commit e3133ed98f

View File

@ -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);