PM: Various fixes to Random Provider

* Remove mention of uint64; use uint64_t instead.

* Simpler reading of bytes from /dev/urandom into a uint64_t.

* Replace string concatenation with StringPrintf.

BUG=None
TEST=Builds and passes unit tests.

Change-Id: Ic0760ae4c2b21e4ffc823a676a9439e867bf9f64
Reviewed-on: https://chromium-review.googlesource.com/184326
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
diff --git a/policy_manager/random_provider.cc b/policy_manager/random_provider.cc
index 34e82c1..73caa04 100644
--- a/policy_manager/random_provider.cc
+++ b/policy_manager/random_provider.cc
@@ -7,6 +7,7 @@
 
 #include <base/file_path.h>
 #include <base/file_util.h>
+#include <base/stringprintf.h>
 
 #include "policy_manager/random_provider.h"
 
@@ -22,34 +23,33 @@
 namespace chromeos_policy_manager {
 
 // The random variable class implementation.
-class RandomVariable : public Variable<uint64> {
+class RandomVariable : public Variable<uint64_t> {
  public:
   RandomVariable(FILE* fp) : fp_(fp) {}
   virtual ~RandomVariable() {}
 
  protected:
-  virtual const uint64* GetValue(base::TimeDelta /* timeout */,
-                                 string* errmsg) {
-    uint8 buf[sizeof(uint64)];
+  virtual const uint64_t* GetValue(base::TimeDelta /* timeout */,
+                                   string* errmsg) {
+    uint64_t result;
+    // Aliasing via char pointer abides by the C/C++ strict-aliasing rules.
+    char* const buf = reinterpret_cast<char*>(&result);
     unsigned int buf_rd = 0;
 
-    while (buf_rd < sizeof(uint64)) {
-      int rd = fread(buf + buf_rd, 1, sizeof(uint64) - buf_rd, fp_.get());
+    while (buf_rd < sizeof(result)) {
+      int rd = fread(buf + buf_rd, 1, sizeof(result) - buf_rd, fp_.get());
       if (rd == 0 || ferror(fp_.get())) {
         // Either EOF on fp or read failed.
-        if (errmsg)
-          *errmsg = string("Error reading from the random device: ")
-              + kRandomDevice;
+        if (errmsg) {
+          *errmsg = StringPrintf("Error reading from the random device: %s",
+                                 kRandomDevice);
+        }
         return NULL;
       }
       buf_rd += rd;
     }
-    // Convert the result to a uint64.
-    uint64 result = 0;
-    for (unsigned int i = 0; i < sizeof(uint64); ++i)
-      result = (result << 8) | buf[i];
 
-    return new uint64(result);
+    return new uint64_t(result);
   }
 
  private:
diff --git a/policy_manager/random_provider_unittest.cc b/policy_manager/random_provider_unittest.cc
index af11b6e..265050e 100644
--- a/policy_manager/random_provider_unittest.cc
+++ b/policy_manager/random_provider_unittest.cc
@@ -40,7 +40,7 @@
 
 TEST_F(PmRandomProviderTest, GetRandomValues) {
   string errmsg;
-  scoped_ptr<const uint64> value(
+  scoped_ptr<const uint64_t> value(
       var_random_seed->GetValue(TimeDelta::FromSeconds(1.), &errmsg));
   ASSERT_TRUE(value != NULL);
 
@@ -48,7 +48,7 @@
   // Test that at least the returned values are different. This test fails,
   // by design, once every 2^320 runs.
   for (int i = 0; i < 5; i++) {
-    scoped_ptr<const uint64> other_value(
+    scoped_ptr<const uint64_t> other_value(
         var_random_seed->GetValue(TimeDelta::FromSeconds(1.), &errmsg));
     ASSERT_TRUE(other_value != NULL);
     always_returns_the_same_value = always_returns_the_same_value &&
diff --git a/policy_manager/random_vars.cc b/policy_manager/random_vars.cc
index 1eb0c0a..95e4eea 100644
--- a/policy_manager/random_vars.cc
+++ b/policy_manager/random_vars.cc
@@ -6,6 +6,6 @@
 
 namespace chromeos_policy_manager {
 
-Variable<uint64>* var_random_seed = NULL;
+Variable<uint64_t>* var_random_seed = NULL;
 
 }  // namespace chromeos_policy_manager
diff --git a/policy_manager/random_vars.h b/policy_manager/random_vars.h
index d983ca1..f0ed4ed 100644
--- a/policy_manager/random_vars.h
+++ b/policy_manager/random_vars.h
@@ -5,8 +5,6 @@
 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PM_RANDOM_VARS_H
 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_PM_RANDOM_VARS_H
 
-#include "base/basictypes.h"
-
 #include "policy_manager/variable.h"
 
 namespace chromeos_policy_manager {
@@ -15,7 +13,7 @@
 // by the variables are cached by the EvaluationContext, so the returned value
 // will be the same during the same policy request. If more random values are
 // needed use a PRNG seeded with this value.
-extern Variable<uint64>* var_random_seed;
+extern Variable<uint64_t>* var_random_seed;
 
 }  // namespace chromeos_policy_manager