Fix checking of string::npos in sensor type parsing
The current string parsing to get sensor type from the path
does not correctly check for string::npos after the calls
to rfind(). This change adds proper string::npos checking
after each rfind() before the position is used.
Tested: Ran 'ipmitool sensor list' and confirmed that the
sensor units are displayed properly.
Change-Id: If9a7113d105fdf7f4450a22d4b881cf2b5a9c090
Signed-off-by: Jason M. Bills <jason.m.bills@linux.intel.com>
diff --git a/include/sdrutils.hpp b/include/sdrutils.hpp
index 6510d6c..7c7b311 100644
--- a/include/sdrutils.hpp
+++ b/include/sdrutils.hpp
@@ -94,12 +94,18 @@
// get sensor type string from path, path is defined as
// /xyz/openbmc_project/sensors/<type>/label
size_t typeEnd = path.rfind("/");
- size_t typeStart = path.rfind("/", typeEnd - 1) + 1;
- if (typeEnd != std::string::npos && typeStart != std::string::npos)
+ if (typeEnd == std::string::npos)
{
- return path.substr(typeStart, typeEnd - typeStart);
+ return path;
}
- return path;
+ size_t typeStart = path.rfind("/", typeEnd - 1);
+ if (typeStart == std::string::npos)
+ {
+ return path;
+ }
+ // Start at the character after the '/'
+ typeStart++;
+ return path.substr(typeStart, typeEnd - typeStart);
}
inline static uint8_t getSensorTypeFromPath(const std::string& path)