On 8/2/05, Mark Frazer <[EMAIL PROTECTED]> wrote: > Hello. I'm not on the list, so please CC me with any replies. > > I have come across a bug found during some code which serializes > doubles. The bug is only encountered when the optimization level is set > to -O2 or greater. > > The bug is not encountered when compiled under > gcc (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7) > at any optimization level. > > The de-serialization code is: > > typedef unsigned char uint8; > static uint8 next_byte(uint &offset, std::vector<uint8> const &bytecode) > throw (std::invalid_argument) > { > if (offset >= bytecode.size()) > throw (std::invalid_argument("Unexpected end of bytecode")); > return bytecode[offset++]; > } > > double parse_double(uint &offset, std::vector<unsigned char> const &bytecode) > throw (std::invalid_argument) > { > typedef unsigned long long uint64; > uint64 rtn = uint64(next_byte(offset, bytecode)) << 56; > rtn |= uint64(next_byte(offset, bytecode)) << 48; > rtn |= uint64(next_byte(offset, bytecode)) << 40; > rtn |= uint64(next_byte(offset, bytecode)) << 32; > rtn |= uint64(next_byte(offset, bytecode)) << 24; > rtn |= uint64(next_byte(offset, bytecode)) << 16; > rtn |= uint64(next_byte(offset, bytecode)) << 8; > rtn |= uint64(next_byte(offset, bytecode)); > return *reinterpret_cast<double*>(&rtn); > } > > Full source code to a demonstration of the bug, and a Makefile is at > http://mjfrazer.org/~mjfrazer/tmp/pack-test/ > The tar file in the directory contains all the other files, so you just > need to grab that.
Try -fno-strict-aliasing. This may be related to PR23192. Richard.