Welcome to MSDN Blogs Sign in | Join | Help

RobU's Blog

Robert Unoki's blog for mostly Microsoft work related stuff.

Mr. Endian Bytes Back

The worse endian bug yet - a definite gem for the ages.

I spent several hours tracking down a ref counting bug in one of our CLR data structures. The following code snippets are boiled down from a much more complicated structure and set of functions.

  typedef struct
  {
      SHORT x;
      SHORT y;
  } FOO;

  void BadCode( FOO foo)
  {
      InterlockedIncrement( (PLONG)&foo.x );
  }

The code actually works just dandy on x86 since the address is always aligned on a 4 byte boundary and, since x86 is little endian, the address &foo.x actually refers to the LSB of x. This particular value in this case will never overflow the 2-byte signed SHORT, so this works as intended.

If you haven't alredy guessed it, on a big endian platform, this actually increments the value of foo.y.  Flawlessly. Every single time. Talk about a bugger to find. I noted the problem when I finally stepped over the interlocked operation and actually noticed that the wrong field changed value in the local window of the debugger!

Now the challenge - how do we fix it?  We didn't want to use 32bit values since this would eat up storage unnecessarily as this is a structure quite frequently instantiated.  So to add a hack upon a hack, we flipped the fields for big endian platforms :O

And put a very large comment next to the hack. :o\

Spoooooooky.

Published Thursday, June 15, 2006 2:56 AM by robertunoki

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Mr. Endian Bytes Back @ Thursday, June 15, 2006 6:40 AM

When does CLR code need to be big-endian?

I suspect we may have similar problems.

Me

# re: Mr. Endian Bytes Back @ Thursday, June 15, 2006 1:34 PM

If you dont mind, could you please draw a ASCII diagram and explain the situation. I am still a little confused.

Thanks!

GA

# re: Mr. Endian Bytes Back @ Wednesday, June 28, 2006 3:49 AM

Hi GA - I posted a follow up article - just for you :)

robertunoki

# re: Mr. Endian Bytes Back @ Wednesday, June 28, 2006 3:54 AM

Hi "Me";

CLR code requires care in regards to endian at a couple of spots. BitConverter is a raw bit converter class that operates using the underlying platform endianess. You must check the "BitConvert.IsLittleEndian" flag and swap your data if needed (depends on the source data endianess).

Also as with all programming, care must be taken when reading any binary data format, whether from data files or over a network stream, or even a device stream such as a USB or traditional serial port.  You must know what endianess the data you are processing conforms to before you can correctly load the data into types - whether it be CLR, Java, or native C/C++ code.

robertunoki

Leave a Comment

(required) 
required 
(required) 
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker