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/binarystore.cpp b/binarystore.cpp
index a18a980..1c94dd3 100644
--- a/binarystore.cpp
+++ b/binarystore.cpp
@@ -74,15 +74,22 @@
log<level::WARNING>(
"Fail to parse. There might be no persisted blobs",
entry("BASE_ID=%s", baseBlobId_.c_str()));
+
+ return true;
}
}
- catch (const std::exception& e)
+ catch (const std::system_error& e)
{
/* Read causes unexpected system-level failure */
log<level::ERR>("Reading from sysfile failed",
entry("ERROR=%s", e.what()));
return false;
}
+ catch (const std::exception& e)
+ {
+ log<level::WARNING>("Invalid size. There might be no persisted blobs.");
+ return true;
+ }
if (blob_.blob_base_id() != baseBlobId_)
{