So I'm working on incorporating the new packed range methods into MOOSE for
use in transferring raw byte strings containing user-defined state and ran
into an issue that I wanted to share. As a start I've taken the example
code that Roy created in the new packed_range unit tests, which is fairly
close to what I'd already written for the old packed range code. When
reading the encoded length values back out of the stream, I've found it's
important to cast those bytes to unsigned chars instead of reading them as
chars in the general case. See the code that reads the bytes one at a time
multiplying the partial results by the byte value (2^8 = 256) for each byte
here:

https://github.com/libMesh/libmesh/blob/master/tests/parallel/packed_range_test.C#L27-L31

OK, on the surface that makes sense, we're encoding 4 bytes representing an
unsigned int into 4 chars in the std::string and then reading them back out
in the code above. It seems like we'd probably better not allow those
individual bytes to be signed. However, what's odd is that the code above
seems to work fine as is (char version) even when you throw really large
strings that require multi-byte encoding so I know I'm using the individual
sign bit on one more more of the bytes. However, as soon as I give the
packed range utility a string containing bytes that fall outside of the
normal ASCII character range (i.e. a raw byte string) it seems to interfere
with the decoding of the string length which should be completely
independent of the content! I simply don't understand why. To be clear, I'm
not changing the logic at all. I'm only changing the characters that appear
in the string to be packed and unpacked and seeing a change in the behavior
when reading the length encoding bytes! I can take a really long string
(like a chapter from a book) pack and unpack it just fine with the existing
logic. But if I take a raw binary string - NOPE! I get errors immediately
in debug mode like this:

Assertion `my_packable_size == my_packed_size' failed.

my_packable_size = 156

my_packed_size = 4294967196

BTW - I ran into the same issue before the packed range refactor and ended
up just adding a few extra "unsigned char" casts in front of the logic that
reads the encoding and solved it then. Well here I am again and I'm finding
myself adding that same cast to get this code working. If anyone has any
idea why this matters, I'd be curious as to an explanation. I don't see why
it works for the normal string case. I argue that it should be broken in
both cases without the cast.

Cody
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Libmesh-devel mailing list
Libmesh-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libmesh-devel

Reply via email to