Check return values when storing peripheral info

Without these checks, in rare cases, this could trigger huddly-updater
to perform a control transfer to a device that didn't exist and thus
segfault.

BUG=chromium:752708
TEST=Plug/unplug the device until "failed to store peripheral info" is
written to /var/log/messages. A segfault should not occur.

Change-Id: I2402df0405652d81ea2c488a6c3dbfd935424ad6
Reviewed-on: https://chromium-review.googlesource.com/641590
Commit-Ready: Emil Lundmark <lndmrk@chromium.org>
Tested-by: Emil Lundmark <lndmrk@chromium.org>
Reviewed-by: Jiwoong Lee <porce@chromium.org>
diff --git a/src/flasher.cc b/src/flasher.cc
index b6fc396..4d465c6 100644
--- a/src/flasher.cc
+++ b/src/flasher.cc
@@ -61,7 +61,11 @@
 
   LOG(INFO) << ".. camera detected: " << minidev_app.GetId();
 
-  StorePeripheralInfo();  // Invoke while in App mode.
+  // Invoke while in App mode.
+  if (!StorePeripheralInfo(err_msg)) {
+    *err_msg += ".. failed to store peripheral info";
+    return false;
+  }
   StoreFirmwareInfo();
 
   // TODO(porce): When fw_hw_rev is a set, See if pl_hw_rev is a member of
@@ -166,17 +170,25 @@
   firmware_.ParseManifestJSON(&fw_app_ver_, &fw_bootloader_ver_, &fw_hw_rev_);
 }
 
-void Flasher::StorePeripheralInfo() {
+bool Flasher::StorePeripheralInfo(std::string* err_msg) {
   // This special routine is a workaround:
   // The App version can be queries only in the App mode.
   MinicamDevice minidev_app(kVendorId, kProductIdApp);
 
-  std::string err_msg;
-  minidev_app.Setup(&err_msg);
-  minidev_app.GetVersion(&pl_app_ver_, &pl_bootloader_ver_);
-  minidev_app.GetHwRevision(&pl_hw_rev_, &err_msg);
+  if (!minidev_app.Setup(err_msg)) {
+    *err_msg += ".. failed to setup minidev: ";
+    return false;
+  }
+  if (!minidev_app.GetVersion(&pl_app_ver_, &pl_bootloader_ver_)) {
+    *err_msg += ".. failed to get app version: ";
+    return false;
+  }
+  if (!minidev_app.GetHwRevision(&pl_hw_rev_, err_msg)) {
+    *err_msg += ".. failed to get hw revision: ";
+    return false;
+  }
 
-  return;
+  return true;
 }
 
 }  // namespace huddly
diff --git a/src/flasher.h b/src/flasher.h
index 102997f..622a8a0 100644
--- a/src/flasher.h
+++ b/src/flasher.h
@@ -50,7 +50,7 @@
 
   // Updates pl_app_ver_, pl_bootloader_ver_, pl_hw_rev_ with information
   // from the attached Huddly camera device.
-  void StorePeripheralInfo();
+  bool StorePeripheralInfo(std::string* err_msg);
 
   // TODO(porce): make pointers / references protected during active flash op.
   const Firmware& firmware_;