From 44d7627ec3f0e469b1d7c41f298b55fcce368d18 Mon Sep 17 00:00:00 2001 From: Yadunund Date: Wed, 24 Jan 2024 12:58:36 +0800 Subject: [PATCH] Rely on strtol to get queue depth Signed-off-by: Yadunund --- rmw_zenoh_cpp/src/detail/liveliness_utils.cpp | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp b/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp index 28e9d4fc..dc1ee027 100644 --- a/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp +++ b/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp @@ -25,6 +25,8 @@ #include "rcpputils/scope_exit.hpp" #include "rcutils/logging_macros.h" +#include "rmw/error_handling.h" + namespace liveliness { @@ -194,9 +196,32 @@ std::optional keyexpr_to_qos(const std::string & keyexpr) qos.reliability = str_to_qos_reliability.at(parts[0]); qos.durability = str_to_qos_durability.at(parts[1]); } catch (const std::exception & e) { + RMW_SET_ERROR_MSG_WITH_FORMAT_STRING("Error setting QoS values from strings: %s", e.what()); + return std::nullopt; + } + // Get the history depth. + errno = 0; + char * endptr; + int64_t num = strtol(history_parts[1].c_str(), &endptr, 10); + if (num == 0) { + // This is an error regardless; the client should never send this + RMW_SET_ERROR_MSG("a invalid zero value sent"); + return std::nullopt; + } else if (endptr == history_parts[1].c_str()) { + // No values were converted, this is an error + RMW_SET_ERROR_MSG("no valid numbers available"); + return std::nullopt; + } else if (*endptr != '\0') { + // There was junk after the number + RMW_SET_ERROR_MSG("non-numeric values"); + return std::nullopt; + } else if (errno != 0) { + // Some other error occurred, which may include overflow or underflow + RMW_SET_ERROR_MSG( + "an undefined error occurred while getting the number, this may be an overflow"); return std::nullopt; } - sscanf(history_parts[1].c_str(), "%zu", &qos.depth); + qos.depth = num; // Liveliness is always automatic given liveliness tokens. qos.liveliness = RMW_QOS_POLICY_LIVELINESS_AUTOMATIC;