]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Get rid of strlcpy(), strlcat(), charlcat() and charremove()
authorattilamolnar <attilamolnar@hush.com>
Thu, 16 May 2013 00:23:45 +0000 (02:23 +0200)
committerattilamolnar <attilamolnar@hush.com>
Thu, 16 May 2013 00:23:45 +0000 (02:23 +0200)
include/channels.h
include/inspstring.h
src/channels.cpp
src/commands/cmd_who.cpp
src/filelogger.cpp
src/inspstring.cpp
src/users.cpp

index 56e1a5a49e9660c677fc2dc18aed5307dbe5aa85..b71da1570fbe5b41fdfca96031a4386fd8dbf973 100644 (file)
@@ -279,7 +279,7 @@ class CoreExport Channel : public Extensible, public InviteBase
         * otherwise it is replaced with '&lt;KEY&gt;'
         * @return The channel mode string
         */
-       char* ChanModes(bool showkey);
+       const char* ChanModes(bool showkey);
 
        /** Spool the NAMES list for this channel to the given user
         * @param user The user to spool the NAMES list to
index 7a433427a5a8efdba001044509b497ca8e7b09cc..6864245b0219e719e5eda500e913c41c53f3a569 100644 (file)
 #include "config.h"
 #include <cstring>
 
-#ifndef HAS_STRLCPY
-/** strlcpy() implementation for systems that don't have it (linux) */
-CoreExport size_t strlcpy(char *dst, const char *src, size_t siz);
-/** strlcat() implementation for systems that don't have it (linux) */
-CoreExport size_t strlcat(char *dst, const char *src, size_t siz);
-#endif
-
-/** charlcat() will append one character to a string using the same
- * safety scemantics as strlcat().
- * @param x The string to operate on
- * @param y the character to append to the end of x
- * @param z The maximum allowed length for z including null terminator
- */
-CoreExport int charlcat(char* x,char y,int z);
-/** charremove() will remove all instances of a character from a string
- * @param mp The string to operate on
- * @param remove The character to remove
- */
-CoreExport bool charremove(char* mp, char remove);
-
 /** Binary to hexadecimal conversion */
 CoreExport std::string BinToHex(const std::string& data);
 /** Base64 encode */
index 39ff8d59cce60fcddd44cf64de11b39b866bb1a6..0cc00746496ef196b8de3285df6055f5dba960ac 100644 (file)
@@ -733,43 +733,37 @@ int Channel::CountInvisible()
        return count;
 }
 
-char* Channel::ChanModes(bool showkey)
+const char* Channel::ChanModes(bool showkey)
 {
-       static char scratch[MAXBUF];
-       static char sparam[MAXBUF];
-       char* offset = scratch;
-       std::string extparam;
+       static std::string scratch;
+       std::string sparam;
 
-       *scratch = '\0';
-       *sparam = '\0';
+       scratch.clear();
 
        /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
        for(int n = 0; n < 64; n++)
        {
                if(this->modes[n])
                {
-                       *offset++ = n + 65;
-                       extparam.clear();
+                       scratch.push_back(n + 65);
                        if (n == 'k' - 65 && !showkey)
                        {
-                               extparam = "<key>";
+                               sparam += " <key>";
                        }
                        else
                        {
-                               extparam = this->GetModeParameter(n + 65);
-                       }
-                       if (!extparam.empty())
-                       {
-                               charlcat(sparam,' ',MAXBUF);
-                               strlcat(sparam,extparam.c_str(),MAXBUF);
+                               const std::string param = this->GetModeParameter(n + 65);
+                               if (!param.empty())
+                               {
+                                       sparam += ' ';
+                                       sparam += param;
+                               }
                        }
                }
        }
 
-       /* Null terminate scratch */
-       *offset = '\0';
-       strlcat(scratch,sparam,MAXBUF);
-       return scratch;
+       scratch += sparam;
+       return scratch.c_str();
 }
 
 /* compile a userlist of a channel into a string, each nick seperated by
index 5a5c27dcfd87f15262eb8391bc8a92cf33739bb5..c865cf67bf4a8a274f9bc0d3de0ff6931a30436f 100644 (file)
@@ -249,33 +249,17 @@ CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *
        opt_far = false;
        opt_time = false;
 
-       Channel *ch = NULL;
        std::vector<std::string> whoresults;
        std::string initial = "352 " + user->nick + " ";
 
-       char matchtext[MAXBUF];
-       bool usingwildcards = false;
-
        /* Change '0' into '*' so the wildcard matcher can grok it */
-       if (parameters[0] == "0")
-               strlcpy(matchtext, "*", MAXBUF);
-       else
-               strlcpy(matchtext, parameters[0].c_str(), MAXBUF);
+       std::string matchtext = ((parameters[0] == "0") ? "*" : parameters[0]);
 
-       for (const char* check = matchtext; *check; check++)
-       {
-               if (*check == '*' || *check == '?')
-               {
-                       usingwildcards = true;
-                       break;
-               }
-       }
+       // WHO flags count as a wildcard
+       bool usingwildcards = ((parameters.size() > 1) || (matchtext.find_first_of("*?") != std::string::npos));
 
        if (parameters.size() > 1)
        {
-               /* Fix for bug #444, WHO flags count as a wildcard */
-               usingwildcards = true;
-
                for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
                {
                        switch (*iter)
@@ -325,7 +309,7 @@ CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *
 
 
        /* who on a channel? */
-       ch = ServerInstance->FindChan(matchtext);
+       Channel* ch = ServerInstance->FindChan(matchtext);
 
        if (ch)
        {
@@ -364,7 +348,7 @@ CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *
                        {
                                User* oper = *i;
 
-                               if (whomatch(user, oper, matchtext))
+                               if (whomatch(user, oper, matchtext.c_str()))
                                {
                                        if (!user->SharesChannelWith(oper))
                                        {
@@ -380,7 +364,7 @@ CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *
                {
                        for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
                        {
-                               if (whomatch(user, i->second, matchtext))
+                               if (whomatch(user, i->second, matchtext.c_str()))
                                {
                                        if (!user->SharesChannelWith(i->second))
                                        {
index 0575256d08c8261c8b7b905dc283e2cd9c0c6cf5..245cbbaabfeb89827bdc329dbceada068d108dd5 100644 (file)
@@ -40,7 +40,7 @@ FileLogStream::~FileLogStream()
 
 void FileLogStream::OnLog(int loglevel, const std::string &type, const std::string &text)
 {
-       static char TIMESTR[26];
+       static std::string TIMESTR;
        static time_t LAST = 0;
 
        if (loglevel < this->loglvl)
@@ -53,11 +53,13 @@ void FileLogStream::OnLog(int loglevel, const std::string &type, const std::stri
                time_t local = ServerInstance->Time();
                struct tm *timeinfo = localtime(&local);
 
-               strlcpy(TIMESTR,asctime(timeinfo),26);
-               TIMESTR[24] = ':';
+               TIMESTR.assign(asctime(timeinfo), 24);
+               TIMESTR += ": ";
                LAST = ServerInstance->Time();
        }
 
-       std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
+       std::string out = TIMESTR;
+       out += text;
+       out += '\n';
        this->f->WriteLogLine(out);
 }
index 534b7ce008558a32ac466eaa786ea1f6c1194560..72d6c64c83f1b3acd33e4e22ec8a4bcdb405f0e2 100644 (file)
 
 #include "inspircd.h"
 
-/*
- * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
- * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef HAS_STRLCPY
-CoreExport size_t strlcat(char *dst, const char *src, size_t siz)
-{
-       char *d = dst;
-       const char *s = src;
-       size_t n = siz, dlen;
-
-       while (n-- != 0 && *d != '\0')
-               d++;
-
-       dlen = d - dst;
-       n = siz - dlen;
-
-       if (n == 0)
-               return(dlen + strlen(s));
-
-       while (*s != '\0')
-       {
-               if (n != 1)
-               {
-                       *d++ = *s;
-                       n--;
-               }
-
-               s++;
-       }
-
-       *d = '\0';
-       return(dlen + (s - src)); /* count does not include NUL */
-}
-
-CoreExport size_t strlcpy(char *dst, const char *src, size_t siz)
-{
-       char *d = dst;
-       const char *s = src;
-       size_t n = siz;
-
-       /* Copy as many bytes as will fit */
-       if (n != 0 && --n != 0)
-       {
-               do
-               {
-                       if ((*d++ = *s++) == 0)
-                               break;
-               } while (--n != 0);
-       }
-
-       /* Not enough room in dst, add NUL and traverse rest of src */
-       if (n == 0)
-       {
-               if (siz != 0)
-                       *d = '\0'; /* NUL-terminate dst */
-               while (*s++);
-       }
-
-       return(s - src - 1); /* count does not include NUL */
-}
-#endif
-
-CoreExport int charlcat(char* x,char y,int z)
-{
-       char* x__n = x;
-       int v = 0;
-
-       while(*x__n++)
-               v++;
-
-       if (v < z - 1)
-       {
-               *--x__n = y;
-               *++x__n = 0;
-       }
-
-       return v;
-}
-
-CoreExport bool charremove(char* mp, char remove)
-{
-       char* mptr = mp;
-       bool shift_down = false;
-
-       while (*mptr)
-       {
-               if (*mptr == remove)
-               shift_down = true;
-
-               if (shift_down)
-                       *mptr = *(mptr+1);
-
-               mptr++;
-       }
-
-       return shift_down;
-}
-
 static const char hextable[] = "0123456789abcdef";
 
 std::string BinToHex(const std::string& data)
index 3fd558dafb936f43cd173679c9a19646637907a3..460f97a188b9398c84ff90b8f35825c2adf6baa0 100644 (file)
@@ -153,15 +153,15 @@ void User::SetMode(unsigned char m, bool value)
 
 const char* User::FormatModes(bool showparameters)
 {
-       static char data[MAXBUF];
+       static std::string data;
        std::string params;
-       int offset = 0;
+       data.clear();
 
        for (unsigned char n = 0; n < 64; n++)
        {
                if (modes[n])
                {
-                       data[offset++] = n + 65;
+                       data.push_back(n + 65);
                        ModeHandler* mh = ServerInstance->Modes->FindMode(n + 65, MODETYPE_USER);
                        if (showparameters && mh && mh->GetNumParams(true))
                        {
@@ -171,9 +171,8 @@ const char* User::FormatModes(bool showparameters)
                        }
                }
        }
-       data[offset] = 0;
-       strlcat(data, params.c_str(), MAXBUF);
-       return data;
+       data += params;
+       return data.c_str();
 }
 
 User::User(const std::string &uid, const std::string& sid, int type)