buffet: Fix crash when CommandInstance::SetResults() is invoked
The crash was a result of use-after-delete in
CloudCommandProxy::OnResultsChanged() where TypedValueToJson returned
std::unique_ptr while the JSON value was set to Dictionary by using
unique_ptr::get() on the temporary. Should have called release()
instead to transfer the ownership to the dictionary.
Added a unit test to buffet/device_registration_info_unittest.cc to
test this specific scenario.
BUG=brillo:770
TEST=`FEATURES=test emerge-link buffet`
Change-Id: I34a28ce610a06f2b77af1cc75e358b3fb3562a13
Reviewed-on: https://chromium-review.googlesource.com/264465
Trybot-Ready: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/utils.h b/buffet/utils.h
index a776e0c..32738d2 100644
--- a/buffet/utils.h
+++ b/buffet/utils.h
@@ -6,6 +6,7 @@
#define BUFFET_UTILS_H_
#include <memory>
+#include <string>
#include <base/values.h>
#include <base/files/file_path.h>
@@ -32,6 +33,10 @@
std::unique_ptr<const base::DictionaryValue> LoadJsonDict(
const base::FilePath& json_file_path, chromeos::ErrorPtr* error);
+// Helper function to load a JSON dictionary from a string.
+std::unique_ptr<const base::DictionaryValue> LoadJsonDict(
+ const std::string& json_string, chromeos::ErrorPtr* error);
+
} // namespace buffet
#endif // BUFFET_UTILS_H_