bug-gnulib
[Top][All Lists]
Advanced

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

Re: [bug-gnulib] base64


From: Bruno Haible
Subject: Re: [bug-gnulib] base64
Date: Wed, 24 Nov 2004 23:01:05 +0100
User-agent: KMail/1.5

Simon Josefsson wrote:
> ... to get review on the code. ... (I'm not saying this implementation
> isn't buggy.)

You are a realist :-)

> +#define BASE64_LENGTH(inlen) (((4 - (inlen) % 3) % 4) + (4 * (inlen) / 3))

This is overly complex. You can also write it as
   ((inlen) + 2) / 3) * 4

> +  while (inlen && outlen)
> +    {
> +      if (outlen--)
> +     *optr++ = b64[iptr[0] >> 2];
> +      if (outlen--)
> +     *optr++ = b64[((iptr[0] << 4) +
> +                    (--inlen ? (iptr[1] >> 4) : 0)) & 0x3f];
> +      if (outlen--)
> +     *optr++ = inlen ? b64[((iptr[1] << 2) +
> +                            (--inlen ? (iptr[2] >> 6) : 0)) & 0x3f] : '=';
> +      if (outlen--)
> +     *optr++ = inlen ? b64[iptr[2] & 0x3f] : '=';
> +      if (inlen)
> +     inlen--;
> +      iptr += 3;
> +    }
> +
> +  if (outlen)
> +    *optr = '\0';

If outlen becomes zero in the loop, it continues to be decremented
nevertheless, so that at the end of the loop it has the value
0xffffffff or 0xfffffffe or 0xfffffffd, and the final '\0' is stored
where it shouldn't.

I generally avoid post-decrement on counter variables for reasons like this.
Pre-decrement is OK.

Bruno





reply via email to

[Prev in Thread] Current Thread [Next in Thread]