bugfix: Fix invalid data handling in deserialization

When loading from sysfile, it is possible that the 'size'
read is junk data when there was no persisted blobs, and is too large causing
'length_error' or 'bad_alloc' to be raised. Improve handling by

1) Returning empty string if 'size' specified is 0 or too large.
2) Ignoring all but system_error which is thrown from sysfile read operation.
3) Not commiting the empty blob when no persisted data is found.
4) Adding more unit tests.

Resolves openbmc/phosphor-ipmi-blobs-binarystore#1

Signed-off-by: Kun Yi <kunyi731@gmail.com>
Change-Id: Iee16cf5254242856efe6bcca59ef2ca7c4f09c7c
diff --git a/sys_file.cpp b/sys_file.cpp
index 2336af2..92d6c85 100644
--- a/sys_file.cpp
+++ b/sys_file.cpp
@@ -80,6 +80,13 @@
 std::string SysFileImpl::readAsStr(size_t pos, size_t count) const
 {
     std::string result;
+
+    /* If count is invalid, return an empty string. */
+    if (count == 0 || count > result.max_size())
+    {
+        return result;
+    }
+
     result.resize(count);
     size_t bytesRead = readToBuf(pos, count, result.data());
     result.resize(bytesRead);