Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 8 Jun 2001 16:44:53 -0700 (PDT)
From:      Matt Dillon <dillon@earth.backplane.com>
To:        Matt Dillon <dillon@earth.backplane.com>
Cc:        Peter Pentchev <roam@orbitel.bg>, John Baldwin <jhb@FreeBSD.ORG>, hackers@FreeBSD.ORG
Subject:   Re: free() and const warnings
Message-ID:  <200106082344.f58NirE99600@earth.backplane.com>
References:  <20010607195634.I724@ringworld.oblivion.bg> <XFMail.010607102051.jhb@FreeBSD.org> <20010608114957.C19938@ringworld.oblivion.bg> <200106082339.f58Ndg399460@earth.backplane.com>

next in thread | previous in thread | raw e-mail | index | archive | help

    Oh my, and just posting these I found a couple of bugs!

:char *
:safe_replace(char **pptr, const char *s)
:{
:    /*
:     * Same data (also occurs if s == *ptr), nothing to do
:     */
:    if (*pptr) {
:	if (s && strcmp(s, *pptr) == 0)
:	    return(*pptr);
:	free(*pptr);
:    }
:
:    /*
:     * free old, dup new.
:     */
:    *pptr = (s) ? strdup(s) : NULL;
:    return(*pptr);
:}

    Should call safe_strdup(), not strdup().


:char *
:safe_append(char **pptr, const char *s)
:{
:    char *old;
:    char *new;
:
:    if ((old = *pptr) != NULL) {
:	int newLen = strlen(old) + strlen(s) + 1;
:	new = malloc(newLen);
:	snprintf(new, newLen, "%s%s", old, s);
:	free(old);
:    } else {
:	new = strdup(s);
:    }
:    *pptr = new;
:    return(new);
:}

    Ditto.  Should call safe_strdup(), not strdup().

    I probably should have cleaned the source up before posting it.  I
    also have a fatalmem() routine which assert(0)'s, and DBASSERT(0)
    is simply assert(0)...  really should be a fatalmem() call too.

    Oh well.  You get the picture!

						-Matt

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200106082344.f58NirE99600>