Get rid of strlcpy(), strlcat(), charlcat() and charremove()

This commit is contained in:
attilamolnar 2013-05-16 02:23:45 +02:00
parent 01a796e239
commit 712a0e5ff3
7 changed files with 32 additions and 195 deletions

View File

@ -279,7 +279,7 @@ class CoreExport Channel : public Extensible, public InviteBase
* otherwise it is replaced with '<KEY>'
* @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

View File

@ -24,26 +24,6 @@
#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 */

View 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

View 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))
{

View 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);
}

View File

@ -21,128 +21,6 @@
#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)

View 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)