[H-GEN] malloc() causing a segfault

Byron Ellacott bje at apnic.net
Thu Aug 31 00:29:37 EDT 2000


[ Humbug *General* list - semi-serious discussions about Humbug and ]
[ Unix-related topics.  Please observe the list's charter.          ]

On Thu, 31 Aug 2000, Martin Pool wrote:

>     strcpy(s, p);  /* C is very nice sometimes */

If, however, you check the manpage for strcpy(), you will see you
shouldn't use it on overlapping regions.  memmove() as aj suggested is
more suitable.  However, aj also uses strlen(), meaning the string is
iterated over twice, although memmove() probably uses architecture
specific optimizations.

char *ltrim(char *i)
{
    char *o = i;
    while (isspace(*i++));
    while (*o++ = *i++);
    return o;
}

as discussed on #humbug, irc.uq.edu.au.  Another option pointed out by
Andrae is to use strspn, but I'm not fond of that as it defeats the
purpose of using isspace().

-- 
bje


--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.



More information about the General mailing list