C++ question related to casting

From: Henrik Nordstrom <henrik@dont-contact.us>
Date: Tue, 23 May 2006 18:44:00 +0200

Hi Robert,

can you (or someone else who groks C++ casts) please explain why this
happened?

http://www.squid-cache.org/Versions/v3/3.0/changesets/10249.patch

store.cc:712

        ((HttpHeader) pe->getReply()->header).putStr(HDR_VARY, vary.buf());

called the ~HttpHeader destructor, which made a mess of everything (it's not supposed to be destroyed yet...)

pe is a StoreEntry

getReply returns a const HttpReply.

rep is assigned a few lines earlier

            HttpReply *rep = (HttpReply *) pe->getReply(); // bypass const

and using this saved our day.. but I don't quite understand why the
original line made such a mess of things. Feels a bit magic..

Valgrind trace which took me there:

(18:01:38) hno: valgrind traps bad memory references.. just had to disable memory pools..
(18:02:23) hno: ==25140== Invalid read of size 2
(18:02:23) hno: ==25140== at 0x41236C: String::size() const (String.cci:46)
(18:02:23) hno: ==25140== by 0x488E40: HttpHeaderEntry::packInto(Packer*) const (HttpHeader.cc:1422)
(18:02:23) hno: ==25140== by 0x488F24: HttpHeader::packInto(Packer*) const (HttpHeader.cc:615)
(18:02:23) hno: ==25140== by 0x4901B6: HttpReply::packHeadersInto(Packer*) const (HttpReply.cc:128)
(18:02:23) hno: ==25140== by 0x4CAA92: storeSetPublicKey (store.cc:734)
(18:02:23) hno: ==25140== by 0x4CACF5: StoreEntry::makePublic() (store.cc:184)
(18:02:23) hno: ==25140== by 0x4813DC: HttpStateData::haveParsedReplyHeaders() (http.cc:838)
(18:02:23) hno: ==25140== by 0x4817E1: HttpStateData::processReplyHeader() (http.cc:778)
(18:02:23) hno: ==25140== by 0x481C97: HttpStateData::readReply(unsigned long, comm_err_t, int) (http.cc:1089)
(18:02:23) hno: ==25140== by 0x481DD6: HttpStateData::ReadReplyWrapper(int, char*, unsigned long, comm_err_t, int, void*) (http.cc:970)
(18:02:23) hno: ==25140== by 0x4E3233: CommReadCallbackData::callCallback() (comm.cc:399)
(18:02:23) hno: ==25140== by 0x4E7629: CommCallbackData::callACallback() (comm.cc:438)
(18:02:23) hno: ==25140== Address 0x5492C72 is 10 bytes inside a block of size 40 free'd
(18:02:23) hno: ==25140== at 0x4905208: free (vg_replace_malloc.c:235)
(18:02:23) hno: ==25140== by 0x524447: xfree (util.c:481)
(18:02:23) hno: ==25140== by 0x51A792: MemMalloc::deallocate(void*) (MemPool.cc:522)
(18:02:23) hno: ==25140== by 0x51A93C: MemImplementingAllocator::free(void*) (MemPool.cc:538)
(18:02:23) hno: ==25140== by 0x51C997: MemAllocatorProxy::free(void*) (MemPool.cc:847)
(18:02:23) hno: ==25140== by 0x48CF6C: HttpHeaderEntry::operator delete(void*) (HttpHeader.h:184)
(18:02:23) hno: ==25140== by 0x48C1AF: HttpHeader::clean() (HttpHeader.cc:401)
(18:02:23) hno: ==25140== by 0x48C203: HttpHeader::~HttpHeader() (HttpHeader.cc:362)
(18:02:23) hno: ==25140== by 0x4CA917: storeSetPublicKey (store.cc:712)

Thanks again Andre for making it easy to disable mempool chunking,
without that capability this would have been nearly impossible to see.

Regards
Henrik

Received on Tue May 23 2006 - 10:44:58 MDT

This archive was generated by hypermail pre-2.1.9 : Thu Jun 01 2006 - 12:00:04 MDT