From 002c9bbb960f20bbb088fc716e3438d673750729 Mon Sep 17 00:00:00 2001 From: "craig.schlenter@chromium.org" Date: Thu, 12 Nov 2009 18:39:59 +0000 Subject: Fix some strict aliasing errors from r31773 caught with gcc 4.4. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I've chosen to use a union here instead of restoring the original memcpy code. Errors were as follows: cc1plus: warnings being treated as errors chrome/browser/safe_browsing/safe_browsing_database_bloom.cc: In member function ‘virtual bool SafeBrowsingDatabaseBloom::ContainsUrl(const GURL&, std::string*, std::vector >*, std::vector >*, base::Time)’: chrome/browser/safe_browsing/safe_browsing_database_bloom.cc:274: error: dereferencing pointer ‘full_hash.207’ does break strict-aliasing rules chrome/browser/safe_browsing/safe_browsing_database_bloom.cc:274: note: initialized from here chrome/browser/safe_browsing/safe_browsing_database_bloom.cc: In member function ‘void SafeBrowsingDatabaseBloom::InsertSub(int, SBPrefix, SBEntry*)’: chrome/browser/safe_browsing/safe_browsing_database_bloom.cc:478: error: dereferencing pointer ‘full_hash.258’ does break strict-aliasing rules chrome/browser/safe_browsing/safe_browsing_database_bloom.cc:478: note: initialized from here chrome/browser/safe_browsing/safe_browsing_database_bloom.cc: In member function ‘void SafeBrowsingDatabaseBloom::InsertAdd(SBPrefix, SBEntry*)’: chrome/browser/safe_browsing/safe_browsing_database_bloom.cc:402: error: dereferencing pointer ‘full_hash.237’ does break strict-aliasing rules chrome/browser/safe_browsing/safe_browsing_database_bloom.cc:402: note: initialized from here make: *** [out/Release/obj.target/browser/chrome/browser/safe_browsing/safe_browsing_database_bloom.o] Error 1 Review URL: http://codereview.chromium.org/389021 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31798 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/safe_browsing/safe_browsing_database_bloom.cc | 10 +++++----- chrome/browser/safe_browsing/safe_browsing_util.h | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/chrome/browser/safe_browsing/safe_browsing_database_bloom.cc b/chrome/browser/safe_browsing/safe_browsing_database_bloom.cc index 46fa2b7..1f4d943 100644 --- a/chrome/browser/safe_browsing/safe_browsing_database_bloom.cc +++ b/chrome/browser/safe_browsing/safe_browsing_database_bloom.cc @@ -271,7 +271,7 @@ bool SafeBrowsingDatabaseBloom::ContainsUrl( // and then fall back to the full hash if there's a hit. base::SHA256HashString(hosts[i] + paths[j], &full_hash, sizeof(SBFullHash)); - SBPrefix prefix = *reinterpret_cast(&full_hash); + SBPrefix prefix = full_hash.prefix; if (bloom_filter_->Exists(prefix)) prefix_hits->push_back(prefix); } @@ -399,7 +399,7 @@ void SafeBrowsingDatabaseBloom::InsertAdd(SBPrefix host, SBEntry* entry) { base::Time receive_time = base::Time::Now(); for (int i = 0; i < entry->prefix_count(); ++i) { SBFullHash full_hash = entry->FullHashAt(i); - SBPrefix prefix = *reinterpret_cast(&full_hash); + SBPrefix prefix = full_hash.prefix; InsertAddPrefix(prefix, encoded); InsertAddFullHash(prefix, encoded, receive_time, full_hash); } @@ -475,7 +475,7 @@ void SafeBrowsingDatabaseBloom::InsertSub( if (entry->type() == SBEntry::SUB_FULL_HASH) { for (int i = 0; i < entry->prefix_count(); ++i) { SBFullHash full_hash = entry->FullHashAt(i); - SBPrefix prefix = *reinterpret_cast(&full_hash); + SBPrefix prefix = full_hash.prefix; encoded_add = EncodeChunkId(entry->ChunkIdAtPrefix(i), entry->list_id()); InsertSubPrefix(prefix, encoded, encoded_add); InsertSubFullHash(prefix, encoded, encoded_add, full_hash, false); @@ -1069,7 +1069,7 @@ void SafeBrowsingDatabaseBloom::WriteFullHashList(const HashList& hash_list, HashList::const_iterator lit = hash_list.begin(); for (; lit != hash_list.end(); ++lit) { const HashCacheEntry& entry = *lit; - SBPrefix prefix = *reinterpret_cast(&entry.full_hash); + SBPrefix prefix = entry.full_hash.prefix; if (is_add) { if (add_del_cache_.find(entry.add_chunk_id) == add_del_cache_.end()) { InsertAddFullHash(prefix, entry.add_chunk_id, @@ -1368,7 +1368,7 @@ void SafeBrowsingDatabaseBloom::CacheHashResults( const Time now = Time::Now(); for (std::vector::const_iterator it = full_hits.begin(); it != full_hits.end(); ++it) { - SBPrefix prefix = *reinterpret_cast(&it->hash); + SBPrefix prefix = it->hash.prefix; HashList& entries = (*hash_cache_)[prefix]; HashCacheEntry entry; entry.received = now; diff --git a/chrome/browser/safe_browsing/safe_browsing_util.h b/chrome/browser/safe_browsing/safe_browsing_util.h index a184ca6..8c2d60b 100644 --- a/chrome/browser/safe_browsing/safe_browsing_util.h +++ b/chrome/browser/safe_browsing/safe_browsing_util.h @@ -41,8 +41,9 @@ typedef struct { typedef int SBPrefix; // A full hash. -typedef struct { +typedef union { char full_hash[32]; + SBPrefix prefix; } SBFullHash; inline bool operator==(const SBFullHash& rhash, const SBFullHash& lhash) { -- cgit v1.1