Re: [PATCH 2/6] Change semantics of interpolate to work like snprintf.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Sep 10, 2007 at 09:11:21AM +0000, Pierre Habouzit wrote:
> On Sun, Sep 09, 2007 at 10:30:38PM +0000, Junio C Hamano wrote:
> > Pierre Habouzit <madcoder@xxxxxxxxxx> writes:
> > 
> > > @@ -89,14 +86,13 @@ unsigned long interpolate(char *result, unsigned long reslen,
> > >  			}
> > >  		}
> > >  		/* Straight copy one non-interpolation character. */
> > > +		if (newlen < reslen)
> > >  			*dest++ = *src;
> > >  		src++;
> > >  		newlen++;
> > >  	}
> > >  
> > > +	if (reslen > 0)
> > > +		*dest = '\0';
> > > +	return newlen;
> > >  }
> > 
> > Are you sure about this *dest = '\0' assignment?  Shouldn't it
> > be done only when "(newlen < reslen)" just like the straight
> > copy and substitution cases?
> 
>   Actually no, this test is the proper one, I should have kept the 
> " + 1" in the previous ones though so that we never append a char to a

  I mean the if (newlen < reslen) should read if (newlen + 1 < reslen),
the +1 in the previous test (the one with the memcopy) whould not have
the +1.

  The rule is:
     if (newlen + WHAT_WE_WANT_TO_ADD < reslen) {
       // adding WHAT_WE_WANT_TO_ADD bytes is ok, there will be space
       // for the \0
     }

  In the latter test, WHAT_WE_WANT_TO_ADD is 1, in the previous it's
valuelen and not valuelen + 1.

-- 
·O·  Pierre Habouzit
··O                                                madcoder@xxxxxxxxxx
OOO                                                http://www.madism.org

Attachment: pgp1YOXkpiEOM.pgp
Description: PGP signature


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux