Add length limit for user-defined values

Previously, the only limit for most user-defined values like id or name
was the one enforced by dbus, which was fairly big. In order to save
space used by persistent data, new meson options were added, with length
limit for those fields.

This directly impacts following dbus interfaces:
- TriggerManager.AddTrigger:
  - Id
  - Name
  - Reports
  - Thresholds (only name of discrete threshold)
- Trigger.Name
- Trigger.Reports
- Trigger.Thresholds (only name of discrete threshold)
- ReportManager.AddReport(FutureVersion):
  - Id
  - Name
  - MetricParameters (metricId)
- Report.Name
- Report.ReadingParameters (metricId)

For Id fields we support 'prefixes', which also are limited, but those
limit are separate. So if limit for prefix is set to 5 and limit for
id/name is set to 5, following Ids are fine:
- 12345/12345
- 12345
and following are not:
- 123456/1234
- 1/123456

Testing done:
- UTs are passing.
- new limits are reflected when calling mentioned dbus interfaces.

Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: I29291a1cc56a344d92fb65491c9186fdb90a8529
diff --git a/src/utils/dbus_path_utils.cpp b/src/utils/dbus_path_utils.cpp
index 9d37cfc..5c1fddb 100644
--- a/src/utils/dbus_path_utils.cpp
+++ b/src/utils/dbus_path_utils.cpp
@@ -35,16 +35,48 @@
     if (path.str.starts_with(constants::reportDirStr))
     {
         auto id = path.str.substr(constants::reportDirStr.length());
-        if (std::cmp_greater(std::count(id.begin(), id.end(), '/'),
-                             constants::maxPrefixesInId))
-        {
-            throw sdbusplus::exception::SdBusError(
-                static_cast<int>(std::errc::invalid_argument),
-                "Too many prefixes in id");
-        }
+        verifyIdPrefixes(id);
         return id;
     }
     throw sdbusplus::exception::SdBusError(
         static_cast<int>(std::errc::invalid_argument), "Invalid path prefix");
 }
+
+void verifyIdPrefixes(std::string_view id)
+{
+    size_t pos_start = 0;
+    size_t pos_end = 0;
+    size_t prefix_cnt = 0;
+    while ((pos_end = id.find('/', pos_start)) != std::string::npos)
+    {
+        if (pos_start == pos_end)
+        {
+            throw sdbusplus::exception::SdBusError(
+                static_cast<int>(std::errc::invalid_argument),
+                "Invalid prefixes in id");
+        }
+
+        if (++prefix_cnt > constants::maxPrefixesInId)
+        {
+            throw sdbusplus::exception::SdBusError(
+                static_cast<int>(std::errc::invalid_argument),
+                "Too many prefixes");
+        }
+
+        if (pos_end - pos_start > constants::maxPrefixLength)
+        {
+            throw sdbusplus::exception::SdBusError(
+                static_cast<int>(std::errc::invalid_argument),
+                "Prefix too long");
+        }
+
+        pos_start = pos_end + 1;
+    }
+
+    if (id.length() - pos_start > constants::maxIdNameLength)
+    {
+        throw sdbusplus::exception::SdBusError(
+            static_cast<int>(std::errc::invalid_argument), "Id too long");
+    }
+}
 } // namespace utils