1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-12 14:14:48 +02:00

relay: replace calls to malloc by weechat_asprintf

This commit is contained in:
Sébastien Helleu
2024-12-17 21:00:59 +01:00
parent 74c63c0541
commit b45d2105a5
5 changed files with 100 additions and 140 deletions
+74 -101
View File
@@ -198,7 +198,7 @@ end:
void
relay_irc_sendf (struct t_relay_client *client, const char *format, ...)
{
int length, number;
int number;
char *pos, hash_key[32], *message, *new_msg1, *new_msg2;
char modifier_data[128];
const char *str_message, *ptr_msg1, *ptr_msg2;
@@ -273,11 +273,8 @@ relay_irc_sendf (struct t_relay_client *client, const char *format, ...)
if (!new_msg2 || new_msg2[0])
{
ptr_msg2 = (new_msg2) ? new_msg2 : str_message;
length = strlen (ptr_msg2) + 16 + 1;
message = malloc (length);
if (message)
if (weechat_asprintf (&message, "%s\r\n", ptr_msg2) >= 0)
{
snprintf (message, length, "%s\r\n", ptr_msg2);
relay_client_send (client, RELAY_MSG_STANDARD,
message, strlen (message), NULL);
free (message);
@@ -706,7 +703,7 @@ relay_irc_get_line_info (struct t_relay_client *client,
const char **nick2, const char **host,
char **tags, char **message)
{
int i, num_tags, command, action, all_tags, length;
int i, num_tags, command, action, all_tags;
char str_tag[512], *pos, *message_no_color, str_time[256];
const char *ptr_tag, *ptr_message, *ptr_nick, *ptr_nick1, *ptr_nick2;
const char *ptr_host, *localvar_nick, *time_format;
@@ -836,13 +833,12 @@ relay_irc_get_line_info (struct t_relay_client *client,
tm = localtime (&msg_date);
if (strftime (str_time, sizeof (str_time), time_format, tm) == 0)
str_time[0] = '\0';
length = strlen (str_time) + strlen (pos) + 1;
*message = malloc (length);
if (*message)
snprintf (*message, length, "%s%s", str_time, pos);
weechat_asprintf (message, "%s%s", str_time, pos);
}
else
{
*message = strdup (pos);
}
}
/* if server capability "server-time" is enabled, add an irc tag with time */
@@ -1063,122 +1059,99 @@ void
relay_irc_send_join (struct t_relay_client *client,
const char *channel)
{
char *infolist_name, *nicks, *nicks2;
char infolist_name[4096], **nicks, *host;
const char *nick, *prefix, *topic;
char *host;
int length, length_nicks;
struct t_infolist *infolist_nick, *infolist_channel, *infolist_nicks;
struct t_gui_buffer *buffer;
length = strlen (client->protocol_args) + 1 + strlen (channel) + 1
+ strlen (RELAY_IRC_DATA(client, nick)) + 1;
infolist_name = malloc (length);
if (infolist_name)
snprintf (infolist_name, sizeof (infolist_name),
"%s,%s,%s",
client->protocol_args,
channel,
RELAY_IRC_DATA(client, nick));
/* get nick host */
host = NULL;
infolist_nick = weechat_infolist_get ("irc_nick", NULL, infolist_name);
if (infolist_nick)
{
/* get nick host */
host = NULL;
snprintf (infolist_name, length, "%s,%s,%s",
client->protocol_args,
channel,
RELAY_IRC_DATA(client, nick));
infolist_nick = weechat_infolist_get ("irc_nick", NULL, infolist_name);
if (infolist_nick)
if (weechat_infolist_next (infolist_nick))
{
if (weechat_infolist_next (infolist_nick))
{
host = (char *)weechat_infolist_string (infolist_nick, "host");
if (host)
host = strdup (host);
}
weechat_infolist_free (infolist_nick);
host = (char *)weechat_infolist_string (infolist_nick, "host");
if (host)
host = strdup (host);
}
weechat_infolist_free (infolist_nick);
}
relay_irc_sendf (client,
":%s!%s JOIN %s",
RELAY_IRC_DATA(client, nick),
(host && host[0]) ? host : "weechat@proxy",
channel);
free (host);
snprintf (infolist_name, length, "%s,%s",
client->protocol_args,
channel);
relay_irc_sendf (client,
":%s!%s JOIN %s",
RELAY_IRC_DATA(client, nick),
(host && host[0]) ? host : "weechat@proxy",
channel);
free (host);
buffer = NULL;
infolist_channel = weechat_infolist_get ("irc_channel", NULL,
infolist_name);
if (infolist_channel)
snprintf (infolist_name, sizeof (infolist_name),
"%s,%s", client->protocol_args, channel);
buffer = NULL;
infolist_channel = weechat_infolist_get ("irc_channel", NULL,
infolist_name);
if (infolist_channel)
{
if (weechat_infolist_next (infolist_channel))
{
if (weechat_infolist_next (infolist_channel))
buffer = weechat_infolist_pointer (infolist_channel, "buffer");
topic = weechat_infolist_string (infolist_channel, "topic");
if (topic && topic[0])
{
buffer = weechat_infolist_pointer (infolist_channel, "buffer");
topic = weechat_infolist_string (infolist_channel, "topic");
if (topic && topic[0])
{
relay_irc_sendf (client,
":%s 332 %s %s :%s",
RELAY_IRC_DATA(client, address),
RELAY_IRC_DATA(client, nick),
channel, topic);
}
relay_irc_sendf (client,
":%s 332 %s %s :%s",
RELAY_IRC_DATA(client, address),
RELAY_IRC_DATA(client, nick),
channel, topic);
}
weechat_infolist_free (infolist_channel);
}
infolist_nicks = weechat_infolist_get ("irc_nick", NULL,
infolist_name);
if (infolist_nicks)
weechat_infolist_free (infolist_channel);
}
infolist_nicks = weechat_infolist_get ("irc_nick", NULL,
infolist_name);
if (infolist_nicks)
{
nicks = weechat_string_dyn_alloc (256);
if (nicks)
{
length_nicks = 0;
nicks = NULL;
while (weechat_infolist_next (infolist_nicks))
{
nick = weechat_infolist_string (infolist_nicks, "name");
prefix = weechat_infolist_string (infolist_nicks, "prefix");
if (nick && nick[0])
{
length_nicks += strlen (nick) + 1 + 1;
if (nicks)
{
nicks2 = realloc (nicks, length_nicks);
if (!nicks2)
{
free (nicks);
return;
}
nicks = nicks2;
strcat (nicks, " ");
}
else
{
nicks = malloc (length_nicks);
nicks[0] = '\0';
}
if ((*nicks)[0])
weechat_string_dyn_concat (nicks, " ", -1);
if (prefix && (prefix[0] != ' '))
strcat (nicks, prefix);
strcat (nicks, nick);
weechat_string_dyn_concat (nicks, prefix, -1);
weechat_string_dyn_concat (nicks, nick, -1);
}
}
if (nicks)
{
relay_irc_sendf (client,
":%s 353 %s = %s :%s",
RELAY_IRC_DATA(client, address),
RELAY_IRC_DATA(client, nick),
channel, nicks);
free (nicks);
}
weechat_infolist_free (infolist_nicks);
relay_irc_sendf (client,
":%s 353 %s = %s :%s",
RELAY_IRC_DATA(client, address),
RELAY_IRC_DATA(client, nick),
channel, *nicks);
weechat_string_dyn_free (nicks, 1l);
}
relay_irc_sendf (client,
":%s 366 %s %s :End of /NAMES list.",
RELAY_IRC_DATA(client, address),
RELAY_IRC_DATA(client, nick),
channel);
free (infolist_name);
/* send backlog to client */
if (buffer)
relay_irc_send_channel_backlog (client, channel, buffer);
weechat_infolist_free (infolist_nicks);
}
relay_irc_sendf (client,
":%s 366 %s %s :End of /NAMES list.",
RELAY_IRC_DATA(client, address),
RELAY_IRC_DATA(client, nick),
channel);
/* send backlog to client */
if (buffer)
relay_irc_send_channel_backlog (client, channel, buffer);
}
/*
+6 -8
View File
@@ -1043,19 +1043,17 @@ relay_config_create_remote_option (const char *remote_name, int index_option,
const char *value)
{
struct t_config_option *ptr_option;
int length;
char *option_name;
ptr_option = NULL;
length = strlen (remote_name) + 1 +
strlen (relay_remote_option_string[index_option]) + 1;
option_name = malloc (length);
if (!option_name)
if (weechat_asprintf (&option_name,
"%s.%s",
remote_name,
relay_remote_option_string[index_option]) < 0)
{
return NULL;
snprintf (option_name, length, "%s.%s",
remote_name, relay_remote_option_string[index_option]);
}
switch (index_option)
{
+9 -14
View File
@@ -709,16 +709,14 @@ relay_http_get_auth_status (struct t_relay_client *client)
rc = -3;
goto end;
}
length = strlen (totp_secret) + strlen (client_totp) + 16 + 1;
info_totp_args = malloc (length);
if (info_totp_args)
/* validate the TOTP received from the client */
if (weechat_asprintf (
&info_totp_args,
"%s,%s,0,%d",
totp_secret, /* the shared secret */
client_totp, /* the TOTP from client */
weechat_config_integer (relay_config_network_totp_window)) >= 0)
{
/* validate the TOTP received from the client */
snprintf (info_totp_args, length,
"%s,%s,0,%d",
totp_secret, /* the shared secret */
client_totp, /* the TOTP from client */
weechat_config_integer (relay_config_network_totp_window));
info_totp = weechat_info_get ("totp_validate", info_totp_args);
totp_ok = (info_totp && (strcmp (info_totp, "1") == 0)) ?
1 : 0;
@@ -1324,7 +1322,7 @@ relay_http_send_error_json (struct t_relay_client *client,
const char *headers,
const char *format, ...)
{
int num_bytes, length;
int num_bytes;
char *error_msg, *json;
if (!client || !message || !format)
@@ -1342,11 +1340,8 @@ relay_http_send_error_json (struct t_relay_client *client,
if (!error_msg)
goto end;
length = strlen (error_msg) + 64;
json = malloc (length);
if (!json)
if (weechat_asprintf (&json, "{\"error\": \"%s\"}", error_msg) < 0)
goto end;
snprintf (json, length, "{\"error\": \"%s\"}", error_msg);
num_bytes = relay_http_send_json (client, return_code, message, headers,
json);
+3 -7
View File
@@ -405,7 +405,7 @@ relay_websocket_build_handshake (struct t_relay_http_request *request)
char **extensions, **protocol_array, str_window_bits[128];
char sec_websocket_extensions[1024];
char sec_websocket_protocol[1024];
int i, length, hash_size, protocol_count;
int i, hash_size, protocol_count;
if (!request)
return NULL;
@@ -415,16 +415,12 @@ relay_websocket_build_handshake (struct t_relay_http_request *request)
if (!sec_websocket_key || !sec_websocket_key[0])
return NULL;
length = strlen (sec_websocket_key) + strlen (WEBSOCKET_GUID) + 1;
key = malloc (length);
if (!key)
return NULL;
/*
* concatenate header "Sec-WebSocket-Key" with the GUID
* (globally unique identifier)
*/
snprintf (key, length, "%s%s", sec_websocket_key, WEBSOCKET_GUID);
if (weechat_asprintf (&key, "%s%s", sec_websocket_key, WEBSOCKET_GUID) < 0)
return NULL;
/* compute 160-bit SHA1 on the key and encode it with base64 */
if (!weechat_crypto_hash (key, strlen (key), "sha1", hash, &hash_size))
@@ -363,7 +363,7 @@ RELAY_WEECHAT_PROTOCOL_CALLBACK(init)
{
char **options, *pos, *relay_password, *totp_secret;
char *info_totp_args, *info_totp, **ptr_option;
int length, password_received, totp_received;
int password_received, totp_received;
RELAY_WEECHAT_PROTOCOL_MIN_ARGS(0);
@@ -408,16 +408,14 @@ RELAY_WEECHAT_PROTOCOL_CALLBACK(init)
totp_received = 1;
if (totp_secret)
{
length = strlen (totp_secret) + strlen (pos) + 16 + 1;
info_totp_args = malloc (length);
if (info_totp_args)
/* validate the OTP received from the client */
if (weechat_asprintf (
&info_totp_args,
"%s,%s,0,%d",
totp_secret, /* the shared secret */
pos, /* the OTP from client */
weechat_config_integer (relay_config_network_totp_window)) >= 0)
{
/* validate the OTP received from the client */
snprintf (info_totp_args, length,
"%s,%s,0,%d",
totp_secret, /* the shared secret */
pos, /* the OTP from client */
weechat_config_integer (relay_config_network_totp_window));
info_totp = weechat_info_get ("totp_validate", info_totp_args);
if (info_totp && (strcmp (info_totp, "1") == 0))
RELAY_WEECHAT_DATA(client, totp_ok) = 1;