You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@santuario.apache.org by Milan Tomic <mi...@setcce.org> on 2004/05/24 11:06:01 UTC

[C++] Encoding: XSCryptCryptoBase64::encode()

Berin,

I've found very interesting problem with encoding. Take a look at this
line of the code from XSCryptCryptoBase64::encode():

t = ((m_inputBuffer[i++] << 4) & 0x30) | (m_inputBuffer[i] >> 4);

The problem is that order of execution (left to right or right to left)
is not defined for "|" operator in C++ language and is left to compiler
to decide. There are cases where first is being executed left side of
operator "|" and there are cases where first is executed right side and
then left side. Because of operator ++ in expression i++, results are
different and encoding fails. I've noticed this behaviour (right to left
order) using Multithreaded (/MT) runtime, but when I use Multitreaded
DLL (/MD) runtime order is left to right (using VC7.1 compiler). The
best way to avoid this is to break this line into (at least) two lines
and avoid ++ operator side effect.

Best regards,
Milan

Re: [C++] Encoding: XSCryptCryptoBase64::encode()

Posted by Berin Lautenbach <be...@wingsofhermes.org>.
Rude words.  I think that was mentioned by someone else some time back.

Thankyou for picking up the reason.  I tend to equate bit-wise 
operations (unspecified ordering) with logical operations (defined 
ordering) and so get caught in this trap.

Thanks!  I will fix in CVS this weekend.

Cheers,
	Berin



Milan Tomic wrote:

> Berin,
> 
> I've found very interesting problem with encoding. Take a look at this 
> line of the code from XSCryptCryptoBase64::encode():
> 
> t = ((m_inputBuffer[i++] << 4) & 0x30) | (m_inputBuffer[i] >> 4);
> 
> The problem is that order of execution (left to right or right to left) 
> is not defined for "|" operator in C++ language and is left to compiler 
> to decide. There are cases where first is being executed left side of 
> operator "|" and there are cases where first is executed right side and 
> then left side. Because of operator ++ in expression i++, results are 
> different and encoding fails. I've noticed this behaviour (right to left 
> order) using Multithreaded (/MT) runtime, but when I use Multitreaded 
> DLL (/MD) runtime order is left to right (using VC7.1 compiler). The 
> best way to avoid this is to break this line into (at least) two lines 
> and avoid ++ operator side effect.
> 
> Best regards,
> Milan
>