Skip to content

Commit 28aa8de

Browse files
lxwinspuredtanous
authored andcommitted
Replace rfind method in sensors.hpp
Remove the rfind method and use the filename method of sdbusplus::message::Object_path. Tested: Built successfully and validator passes. Signed-off-by: George Liu <[email protected]> Change-Id: I762360474b18092987feb64b13f78371db144baa
1 parent cba4f44 commit 28aa8de

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

redfish-core/lib/sensors.hpp

+34-34
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,11 @@ class InventoryItem
193193
ledState(LedState::UNKNOWN)
194194
{
195195
// Set inventory item name to last node of object path
196-
auto pos = objectPath.rfind('/');
197-
if ((pos != std::string::npos) && ((pos + 1) < objectPath.size()))
196+
sdbusplus::message::object_path path(objectPath);
197+
name = path.filename();
198+
if (name.empty())
198199
{
199-
name = objectPath.substr(pos + 1);
200+
BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
200201
}
201202
}
202203

@@ -388,13 +389,13 @@ void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
388389
std::string chassisName;
389390
for (const std::string& chassis : chassisPaths)
390391
{
391-
std::size_t lastPos = chassis.rfind('/');
392-
if (lastPos == std::string::npos)
392+
sdbusplus::message::object_path path(chassis);
393+
chassisName = path.filename();
394+
if (chassisName.empty())
393395
{
394396
BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
395397
continue;
396398
}
397-
chassisName = chassis.substr(lastPos + 1);
398399
if (chassisName == asyncResp->chassisId)
399400
{
400401
chassisPath = chassis;
@@ -441,13 +442,13 @@ void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
441442
std::string chassisName;
442443
for (const std::string& chassis : chassisPaths)
443444
{
444-
std::size_t lastPos = chassis.rfind('/');
445-
if (lastPos == std::string::npos)
445+
sdbusplus::message::object_path path(chassis);
446+
chassisName = path.filename();
447+
if (chassisName.empty())
446448
{
447449
BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
448450
continue;
449451
}
450-
chassisName = chassis.substr(lastPos + 1);
451452
if (chassisName == sensorsAsyncResp->chassisId)
452453
{
453454
chassisPath = &chassis;
@@ -1149,15 +1150,16 @@ inline void populateFanRedundancy(
11491150
sensorsAsyncResp->res);
11501151
return;
11511152
}
1152-
size_t lastSlash = path.rfind('/');
1153-
if (lastSlash == std::string::npos)
1153+
sdbusplus::message::object_path objectPath(
1154+
path);
1155+
std::string name = objectPath.filename();
1156+
if (name.empty())
11541157
{
11551158
// this should be impossible
11561159
messages::internalError(
11571160
sensorsAsyncResp->res);
11581161
return;
11591162
}
1160-
std::string name = path.substr(lastSlash + 1);
11611163
std::replace(name.begin(), name.end(), '_',
11621164
' ');
11631165

@@ -1180,10 +1182,12 @@ inline void populateFanRedundancy(
11801182
sensorsAsyncResp->res.jsonValue["Fans"];
11811183
for (const std::string& item : *collection)
11821184
{
1183-
lastSlash = item.rfind('/');
1184-
// make a copy as collection is const
1185-
std::string itemName =
1186-
item.substr(lastSlash + 1);
1185+
sdbusplus::message::object_path path(item);
1186+
std::string itemName = path.filename();
1187+
if (itemName.empty())
1188+
{
1189+
continue;
1190+
}
11871191
/*
11881192
todo(ed): merge patch that fixes the names
11891193
std::replace(itemName.begin(),
@@ -2684,14 +2688,14 @@ inline bool findSensorNameUsingSensorPath(
26842688
boost::container::flat_set<std::string>& sensorsList,
26852689
boost::container::flat_set<std::string>& sensorsModified)
26862690
{
2687-
for (std::string_view chassisSensor : sensorsList)
2691+
for (auto& chassisSensor : sensorsList)
26882692
{
2689-
std::size_t pos = chassisSensor.rfind('/');
2690-
if (pos >= (chassisSensor.size() - 1))
2693+
sdbusplus::message::object_path path(chassisSensor);
2694+
std::string_view thisSensorName = path.filename();
2695+
if (thisSensorName.empty())
26912696
{
26922697
continue;
26932698
}
2694-
std::string_view thisSensorName = chassisSensor.substr(pos + 1);
26952699
if (thisSensorName == sensorName)
26962700
{
26972701
sensorsModified.emplace(chassisSensor);
@@ -2790,14 +2794,13 @@ inline void setSensorsOverride(
27902794
}
27912795
for (const auto& item : objectsWithConnection)
27922796
{
2793-
2794-
auto lastPos = item.first.rfind('/');
2795-
if (lastPos == std::string::npos)
2797+
sdbusplus::message::object_path path(item.first);
2798+
std::string sensorName = path.filename();
2799+
if (sensorName.empty())
27962800
{
27972801
messages::internalError(sensorAsyncResp->res);
27982802
return;
27992803
}
2800-
std::string sensorName = item.first.substr(lastPos + 1);
28012804

28022805
const auto& iterator = overrideMap.find(sensorName);
28032806
if (iterator == overrideMap.end())
@@ -3042,15 +3045,14 @@ class SensorCollection : public Node
30423045
{
30433046
BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
30443047

3045-
std::size_t lastPos = sensor.rfind('/');
3046-
if (lastPos == std::string::npos ||
3047-
lastPos + 1 >= sensor.size())
3048+
sdbusplus::message::object_path path(sensor);
3049+
std::string sensorName = path.filename();
3050+
if (sensorName.empty())
30483051
{
30493052
BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
30503053
messages::internalError(asyncResp->res);
30513054
return;
30523055
}
3053-
std::string sensorName = sensor.substr(lastPos + 1);
30543056
entriesArray.push_back(
30553057
{{"@odata.id",
30563058
"/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
@@ -3128,16 +3130,14 @@ class Sensor : public Node
31283130
std::vector<std::pair<std::string,
31293131
std::vector<std::string>>>>&
31303132
object) {
3131-
std::string_view sensor = object.first;
3132-
std::size_t lastPos = sensor.rfind('/');
3133-
if (lastPos == std::string::npos ||
3134-
lastPos + 1 >= sensor.size())
3133+
sdbusplus::message::object_path path(object.first);
3134+
std::string name = path.filename();
3135+
if (name.empty())
31353136
{
31363137
BMCWEB_LOG_ERROR << "Invalid sensor path: "
3137-
<< sensor;
3138+
<< object.first;
31383139
return false;
31393140
}
3140-
std::string_view name = sensor.substr(lastPos + 1);
31413141

31423142
return name == sensorName;
31433143
});

0 commit comments

Comments
 (0)