[haiku-commits] Re: r40726 - haiku/trunk/src/kits/support

  • From: "Jonas Sundström" <jonas@xxxxxxxxxxx>
  • To: "haiku-commits@xxxxxxxxxxxxx" <haiku-commits@xxxxxxxxxxxxx>
  • Date: Mon, 28 Feb 2011 03:41:08 +0100

Stefano Ceccherini Stefano Ceccherini <stefano.ceccherini@xxxxxxxxx> wrote:
 ...
> > > Changeset: http://dev.haiku-os.org/changeset/40726
 ...
> > > +             bytes = bytes < 0 ? 0 : bytes;
> >
> > This clamps a negative return value to zero.
> >
> 
> There is a private static method in BString to do that.

Yes:

// returns minimum of two given values (but clamps to 0):
static inline int32
min_clamp0(int32 num1, int32 num2)
{
        if (num1 < num2)
                return num1 > 0 ? num1 : 0;

        return num2 > 0 ? num2 : 0;
}

I think               bytes = bytes < 0 ? 0 : bytes;
is more clear than    bytes = min_clamp0(bytes, bytes);

A positive 'bytes' is meant to be kept, and zero only assigned in case
of a negative return due to a vsnprintf() output error.

The BString buffer is unlocked using 'bytes'. I think that in case of
error, it's better to have it truncate, UnlockBuffer(0), than to have
UnlockBuffer(negative) run strlen(fPrivateData) on the failed output
of vsnprintf().

Is the use of min_clamp0() preferred in this case?
File scope uniformity?

/Jonas.


Other related posts: