bug-gnulib
[Top][All Lists]
Advanced

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

Re: [bug-gnulib] Re: base64


From: Stepan Kasal
Subject: Re: [bug-gnulib] Re: base64
Date: Sat, 27 Nov 2004 15:42:38 +0100
User-agent: Mutt/1.4.1i

Hi,

On Sat, Nov 27, 2004 at 01:42:38PM +0100, Simon Josefsson wrote:
> The goal is to compute:
> 
> #define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4)

I suggest the following:

base64_encode_alloc (const char *in, size_t inlen, char **out)
{
  size_t outlen = ((inlen + 2) / 3) * 4;

  if (inlen > outlen)
        /* overflow! */

Proof:
If there is no overflow, outlen >= inlen.

If the operation (inlen + 2) overflows then it yields at most +1, so
outlen is 0.

If the multiplication overflows, we lose at least half of the correct
value, so the result is < ((inlen + 2) / 3) * 2, which is less than
(inlen + 2) * 0.66667, which is less than inlen as soon as (inlen > 4).
\qed

You can put the proof in a comment above the check, if you please.

Regards,
        Stepan




reply via email to

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