Skip to content

Commit

Permalink
fix core: UASSERT for non-empty label name
Browse files Browse the repository at this point in the history
commit_hash:5d5788305043304a8e076ab6c67b848fa2b8ed4f
  • Loading branch information
ArkadyRudenko committed Jan 26, 2025
1 parent e7c1a9f commit 598550b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
10 changes: 7 additions & 3 deletions core/include/userver/utils/statistics/labels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <string_view>
#include <type_traits>

#include <userver/utils/assert.hpp>

USERVER_NAMESPACE_BEGIN

namespace utils::statistics {
Expand All @@ -16,10 +18,12 @@ class Label;
/// @brief Non owning label name+value storage.
class LabelView final {
public:
LabelView() = default;
LabelView() = delete;
LabelView(Label&& label) = delete;
explicit LabelView(const Label& label) noexcept;
constexpr LabelView(std::string_view name, std::string_view value) noexcept : name_(name), value_(value) {}
explicit LabelView(const Label& label);
constexpr LabelView(std::string_view name, std::string_view value) : name_(name), value_(value) {
UINVARIANT(!name_.empty(), "The lable name must not be empty.");
}

template <class T, std::enable_if_t<std::is_arithmetic_v<T>>* = nullptr>
constexpr LabelView(std::string_view, T) {
Expand Down
4 changes: 3 additions & 1 deletion core/src/utils/statistics/labels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ USERVER_NAMESPACE_BEGIN

namespace utils::statistics {

LabelView::LabelView(const Label& label) noexcept : name_(label.Name()), value_(label.Value()) {}
LabelView::LabelView(const Label& label) : name_(label.Name()), value_(label.Value()) {
UINVARIANT(!name_.empty(), "The lable name must not be empty.");
}

LabelsSpan::LabelsSpan(const LabelView* begin, const LabelView* end) noexcept : begin_(begin), end_(end) {
UASSERT(begin <= end);
Expand Down
3 changes: 2 additions & 1 deletion core/src/utils/statistics/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ void Writer::ResetState() noexcept {
kFixitHint
)
);
state_->add_labels.resize(initial_labels_size_);
auto& labels = state_->add_labels;
labels.erase(labels.begin() + initial_labels_size_, labels.end());

state_ = nullptr;
}
Expand Down

0 comments on commit 598550b

Please sign in to comment.