Fix issue on null ptr in funciton positionToString
Error message is as below:
smbiosmdrv2app[5607]: terminate called after throwing an instance of 'std::logic_error'
smbiosmdrv2app[5607]: what(): basic_string::_M_construct null not valid
systemd[1]: smbios-mdrv2.service: Main process exited, code=dumped, status=6/ABRT
systemd[1]: smbios-mdrv2.service: Failed with result 'core-dump'.
Have to check ptr is not null before using.
Tested:
When target prt is null return empty string, not crash app.
Change-Id: I5bc4c2681150fd1a7aeb0a2dc7b5d19e5b54dab0
Signed-off-by: Kuiying Wang <kuiying.wang@intel.com>
diff --git a/include/smbios_mdrv2.hpp b/include/smbios_mdrv2.hpp
index e53d709..a307332 100644
--- a/include/smbios_mdrv2.hpp
+++ b/include/smbios_mdrv2.hpp
@@ -222,18 +222,24 @@
uint16_t limit = mdrSMBIOSSize; // set a limit to avoid endless loop
char* target = reinterpret_cast<char*>(dataIn + structLen);
+ if (target == nullptr)
+ {
+ return "";
+ }
for (uint8_t index = 1; index < positionNum; index++)
{
for (; *target != '\0'; target++)
{
limit--;
- if (limit < 1)
+ // When target = dataIn + structLen + limit,
+ // following target++ will be nullptr
+ if (limit < 1 || target == nullptr)
{
return "";
}
}
target++;
- if (*target == '\0')
+ if (target == nullptr || *target == '\0')
{
return ""; // 0x00 0x00 means end of the entry.
}