Skip to content

Commit 598550b

Browse files
committed
fix core: UASSERT for non-empty label name
commit_hash:5d5788305043304a8e076ab6c67b848fa2b8ed4f
1 parent e7c1a9f commit 598550b

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

core/include/userver/utils/statistics/labels.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <string_view>
88
#include <type_traits>
99

10+
#include <userver/utils/assert.hpp>
11+
1012
USERVER_NAMESPACE_BEGIN
1113

1214
namespace utils::statistics {
@@ -16,10 +18,12 @@ class Label;
1618
/// @brief Non owning label name+value storage.
1719
class LabelView final {
1820
public:
19-
LabelView() = default;
21+
LabelView() = delete;
2022
LabelView(Label&& label) = delete;
21-
explicit LabelView(const Label& label) noexcept;
22-
constexpr LabelView(std::string_view name, std::string_view value) noexcept : name_(name), value_(value) {}
23+
explicit LabelView(const Label& label);
24+
constexpr LabelView(std::string_view name, std::string_view value) : name_(name), value_(value) {
25+
UINVARIANT(!name_.empty(), "The lable name must not be empty.");
26+
}
2327

2428
template <class T, std::enable_if_t<std::is_arithmetic_v<T>>* = nullptr>
2529
constexpr LabelView(std::string_view, T) {

core/src/utils/statistics/labels.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ USERVER_NAMESPACE_BEGIN
66

77
namespace utils::statistics {
88

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

1113
LabelsSpan::LabelsSpan(const LabelView* begin, const LabelView* end) noexcept : begin_(begin), end_(end) {
1214
UASSERT(begin <= end);

core/src/utils/statistics/writer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ void Writer::ResetState() noexcept {
201201
kFixitHint
202202
)
203203
);
204-
state_->add_labels.resize(initial_labels_size_);
204+
auto& labels = state_->add_labels;
205+
labels.erase(labels.begin() + initial_labels_size_, labels.end());
205206

206207
state_ = nullptr;
207208
}

0 commit comments

Comments
 (0)