Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: base/metrics/field_trial.cc

Issue 10088001: Fixing issues with alignment, undefined behaviour and endianness in the FieldTrial::HashName functi… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/sys_byteorder.h » ('j') | base/sys_byteorder.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/metrics/field_trial.h" 5 #include "base/metrics/field_trial.h"
6 6
7 #include "base/build_time.h" 7 #include "base/build_time.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/rand_util.h" 9 #include "base/rand_util.h"
10 #include "base/sha1.h" 10 #include "base/sha1.h"
11 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 #include "base/sys_byteorder.h"
13 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
14 15
15 namespace base { 16 namespace base {
16 17
17 static const char kHistogramFieldTrialSeparator('_'); 18 static const char kHistogramFieldTrialSeparator('_');
18 19
19 // statics 20 // statics
20 const int FieldTrial::kNotFinalized = -1; 21 const int FieldTrial::kNotFinalized = -1;
21 const int FieldTrial::kDefaultGroupNumber = 0; 22 const int FieldTrial::kDefaultGroupNumber = 0;
22 const uint32 FieldTrial::kReservedHashValue = 0; 23 const uint32 FieldTrial::kReservedHashValue = 0;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 // static 206 // static
206 uint32 FieldTrial::HashName(const std::string& name) { 207 uint32 FieldTrial::HashName(const std::string& name) {
207 // SHA-1 is designed to produce a uniformly random spread in its output space, 208 // SHA-1 is designed to produce a uniformly random spread in its output space,
208 // even for nearly-identical inputs. 209 // even for nearly-identical inputs.
209 unsigned char sha1_hash[kSHA1Length]; 210 unsigned char sha1_hash[kSHA1Length];
210 SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.c_str()), 211 SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.c_str()),
211 name.size(), 212 name.size(),
212 sha1_hash); 213 sha1_hash);
213 214
214 COMPILE_ASSERT(sizeof(uint32) < sizeof(sha1_hash), need_more_data); 215 COMPILE_ASSERT(sizeof(uint32) < sizeof(sha1_hash), need_more_data);
215 uint32* bits = reinterpret_cast<uint32*>(&sha1_hash[0]); 216 uint32 bits;
217 memcpy(&bits, sha1_hash, sizeof(bits));
216 218
217 // We only DCHECK, since this should not happen because the registration 219 // We only DCHECK, since this should not happen because the registration
218 // of the experiment on the server should have already warn the developer. 220 // of the experiment on the server should have already warn the developer.
219 // If this ever happen, we'll ignore this experiment/group when reporting. 221 // If this ever happen, we'll ignore this experiment/group when reporting.
220 DCHECK(*bits != kReservedHashValue); 222 DCHECK(bits != kReservedHashValue);
221 return *bits; 223 return base::ByteSwapToLE32(bits);
222 } 224 }
223 225
224 //------------------------------------------------------------------------------ 226 //------------------------------------------------------------------------------
225 // FieldTrialList methods and members. 227 // FieldTrialList methods and members.
226 228
227 // static 229 // static
228 FieldTrialList* FieldTrialList::global_ = NULL; 230 FieldTrialList* FieldTrialList::global_ = NULL;
229 231
230 // static 232 // static
231 bool FieldTrialList::used_without_global_ = false; 233 bool FieldTrialList::used_without_global_ = false;
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 used_without_global_ = true; 478 used_without_global_ = true;
477 return; 479 return;
478 } 480 }
479 AutoLock auto_lock(global_->lock_); 481 AutoLock auto_lock(global_->lock_);
480 DCHECK(!global_->PreLockedFind(trial->name())); 482 DCHECK(!global_->PreLockedFind(trial->name()));
481 trial->AddRef(); 483 trial->AddRef();
482 global_->registered_[trial->name()] = trial; 484 global_->registered_[trial->name()] = trial;
483 } 485 }
484 486
485 } // namespace base 487 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/sys_byteorder.h » ('j') | base/sys_byteorder.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698