1
0
mirror of https://github.com/unrealircd/unrealircd.git synced 2026-07-10 03:43:12 +02:00

Add strldup() and safestrldup(), reducing ridiculous amount of code in

m_pass and m_topic.c when duplicating strings with a length limit.
+/* strldup(str,max) copies a string and ensures the new buffer
+ * is at most 'max' size, including nul byte. The syntax is pretty
+ * much identical to strlcpy() except that the buffer is newly
+ * allocated.
+ * If you wonder why not use strndup() instead?
+ * I feel that mixing code with strlcpy() and strndup() would be
+ * rather confusing since strlcpy() assumes buffer size including
+ * the nul byte and strndup() assumes without the nul byte and
+ * will write one character extra. Hence this strldup(). -- Syzop
+ */
This commit is contained in:
Bram Matthys
2018-04-22 15:40:21 +02:00
parent 6990b7d9a6
commit bb4758f321
7 changed files with 50 additions and 68 deletions
+3 -8
View File
@@ -114,7 +114,7 @@ ConfigItem_ban *bconf;
CMD_FUNC(m_pass)
{
char *password = parc > 1 ? parv[1] : NULL;
int PassLen = 0;
if (BadPtr(password))
{
sendto_one(cptr, err_str(ERR_NEEDMOREPARAMS),
@@ -128,13 +128,8 @@ CMD_FUNC(m_pass)
return 0;
}
PassLen = strlen(password);
if (cptr->local->passwd)
MyFree(cptr->local->passwd);
if (PassLen > (PASSWDLEN))
PassLen = PASSWDLEN;
cptr->local->passwd = MyMallocEx(PassLen + 1);
strlcpy(cptr->local->passwd, password, PassLen + 1);
/* Store the password */
safestrldup(cptr->local->passwd, password, PASSWDLEN+1);
/* note: the original non-truncated password is supplied as 2nd parameter. */
RunHookReturnInt2(HOOKTYPE_LOCAL_PASS, sptr, password, !=0);
+9 -48
View File
@@ -80,8 +80,6 @@ CMD_FUNC(m_topic)
aChannel *chptr = NullChn;
char *topic = NULL, *name, *tnick = NULL;
TS ttime = 0;
int topiClen = 0;
int nicKlen = 0;
int i = 0;
Hook *h;
int ismember; /* cache: IsMember() */
@@ -176,29 +174,11 @@ long flags = 0; /* cache: membership flags */
* some services do this in their topic enforcement -- codemastr
*/
{
/* setting a topic */
topiClen = strlen(topic);
nicKlen = strlen(tnick);
if (chptr->topic)
MyFree(chptr->topic);
if (topiClen > (TOPICLEN))
topiClen = TOPICLEN;
if (nicKlen > (NICKLEN+USERLEN+HOSTLEN+5))
nicKlen = (NICKLEN+USERLEN+HOSTLEN+5);
chptr->topic = MyMallocEx(topiClen + 1);
strlcpy(chptr->topic, topic, topiClen + 1);
if (chptr->topic_nick)
MyFree(chptr->topic_nick);
chptr->topic_nick = MyMallocEx(nicKlen + 1);
strlcpy(chptr->topic_nick, tnick, nicKlen + 1);
/* Set the topic */
safestrldup(chptr->topic, topic, TOPICLEN+1);
safestrldup(chptr->topic_nick, tnick, NICKLEN+USERLEN+HOSTLEN+5);
chptr->topic_time = ttime;
RunHook4(HOOKTYPE_TOPIC, cptr, sptr, chptr, topic);
sendto_server(cptr, PROTO_SID, 0, ":%s TOPIC %s %s %lu :%s",
ID(sptr), chptr->chname, chptr->topic_nick,
@@ -279,36 +259,17 @@ long flags = 0; /* cache: membership flags */
}
RunHook4(HOOKTYPE_LOCAL_TOPIC, cptr, sptr, chptr, topic);
}
/* setting a topic */
topiClen = strlen(topic);
#ifndef TOPIC_NICK_IS_NUHOST
nicKlen = strlen(sptr->name);
#else
#ifdef TOPIC_NICK_IS_NUHOST
if (IsPerson(sptr))
tnick = make_nick_user_host(sptr->name, sptr->user->username, GetHost(sptr));
else
tnick = sptr->name;
nicKlen = strlen(tnick);
#endif
if (chptr->topic)
MyFree(chptr->topic);
/* Set the topic */
safestrldup(chptr->topic, topic, TOPICLEN+1);
safestrldup(chptr->topic_nick, tnick, NICKLEN+USERLEN+HOSTLEN+5);
if (topiClen > (TOPICLEN))
topiClen = TOPICLEN;
if (nicKlen > (NICKLEN+USERLEN+HOSTLEN+5))
nicKlen = NICKLEN+USERLEN+HOSTLEN+5;
chptr->topic = MyMallocEx(topiClen + 1);
strlcpy(chptr->topic, topic, topiClen + 1);
if (chptr->topic_nick)
MyFree(chptr->topic_nick);
chptr->topic_nick = MyMallocEx(nicKlen + 1);
#ifndef TOPIC_NICK_IS_NUHOST
strlcpy(chptr->topic_nick, sptr->name, nicKlen + 1);
#else
strlcpy(chptr->topic_nick, tnick, nicKlen + 1);
#endif
RunHook4(HOOKTYPE_TOPIC, cptr, sptr, chptr, topic);
if (ttime && IsServer(cptr))
chptr->topic_time = ttime;