Skip to content

Commit accfcec

Browse files
apolyakovdenisichh
authored andcommitted
[k2] fix confdata_get_value* semantics (#1505)
* do not accept empty key and wildcard * truncate keys by wildcard prefix
1 parent 6660e1c commit accfcec

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

runtime-light/stdlib/confdata/confdata-functions.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <algorithm>
88
#include <cstddef>
99
#include <span>
10+
#include <string_view>
1011
#include <utility>
1112

1213
#include "runtime-common/core/allocator/script-allocator.h"
@@ -46,6 +47,11 @@ mixed extract_confdata_value(const tl::confdataValue& confdata_value) noexcept {
4647
} // namespace
4748

4849
kphp::coro::task<mixed> f$confdata_get_value(string key) noexcept {
50+
if (key.empty()) [[unlikely]] {
51+
kphp::log::warning("empty key is not supported");
52+
co_return mixed{};
53+
}
54+
4955
auto& confdata_key_cache{ConfdataInstanceState::get().key_cache()};
5056
if (auto it{confdata_key_cache.find(key)}; it != confdata_key_cache.end()) {
5157
co_return it->second;
@@ -75,19 +81,26 @@ kphp::coro::task<mixed> f$confdata_get_value(string key) noexcept {
7581
}
7682

7783
auto value{extract_confdata_value(*maybe_confdata_value.opt_value)}; // the key exists
78-
confdata_key_cache.emplace(key, value);
84+
confdata_key_cache.emplace(std::move(key), value);
7985
co_return std::move(value);
8086
}
8187

8288
kphp::coro::task<array<mixed>> f$confdata_get_values_by_any_wildcard(string wildcard) noexcept {
83-
static constexpr size_t CONFDATA_GET_WILDCARD_STREAM_CAPACITY = 1 << 20;
89+
static constexpr size_t CONFDATA_GET_WILDCARD_INIT_BUFFER_CAPACITY = 1 << 20;
90+
91+
if (wildcard.empty()) [[unlikely]] {
92+
kphp::log::warning("empty wildcard is not supported");
93+
co_return array<mixed>{};
94+
}
8495

8596
auto& confdata_wildcard_cache{ConfdataInstanceState::get().wildcard_cache()};
8697
if (auto it{confdata_wildcard_cache.find(wildcard)}; it != confdata_wildcard_cache.end()) {
8798
co_return it->second;
8899
}
89100

90-
tl::ConfdataGetWildcard confdata_get_wildcard{.wildcard = {.value = {wildcard.c_str(), wildcard.size()}}};
101+
const std::string_view wildcard_view{wildcard.c_str(), wildcard.size()};
102+
103+
const tl::ConfdataGetWildcard confdata_get_wildcard{.wildcard = {.value = wildcard_view}};
91104
tl::storer tls{confdata_get_wildcard.footprint()};
92105
confdata_get_wildcard.store(tls);
93106

@@ -98,7 +111,7 @@ kphp::coro::task<array<mixed>> f$confdata_get_values_by_any_wildcard(string wild
98111

99112
auto stream{*std::move(expected_stream)};
100113
kphp::stl::vector<std::byte, kphp::memory::script_allocator> response{};
101-
response.reserve(CONFDATA_GET_WILDCARD_STREAM_CAPACITY);
114+
response.reserve(CONFDATA_GET_WILDCARD_INIT_BUFFER_CAPACITY);
102115
if (!co_await kphp::forks::id_managed(kphp::component::query(stream, tls.view(), kphp::component::read_ext::append(response)))) [[unlikely]] {
103116
co_return array<mixed>{};
104117
}
@@ -108,10 +121,13 @@ kphp::coro::task<array<mixed>> f$confdata_get_values_by_any_wildcard(string wild
108121
kphp::log::assertion(dict_confdata_value.fetch(tlf));
109122

110123
array<mixed> result{array_size{static_cast<int64_t>(dict_confdata_value.size()), false}};
111-
std::for_each(dict_confdata_value.begin(), dict_confdata_value.end(), [&result](const auto& dict_field) noexcept {
112-
result.set_value(string{dict_field.key.value.data(), static_cast<string::size_type>(dict_field.key.value.size())},
124+
std::ranges::for_each(dict_confdata_value, [&result, wildcard_size = wildcard_view.size()](const auto& dict_field) noexcept {
125+
kphp::log::assertion(dict_field.key.value.size() >= wildcard_size);
126+
127+
const std::string_view key_without_wildcard_prefix{dict_field.key.value.substr(wildcard_size)};
128+
result.set_value(string{key_without_wildcard_prefix.data(), static_cast<string::size_type>(key_without_wildcard_prefix.size())},
113129
extract_confdata_value(dict_field.value));
114130
});
115-
confdata_wildcard_cache.emplace(wildcard, result);
131+
confdata_wildcard_cache.emplace(std::move(wildcard), result);
116132
co_return std::move(result);
117133
}

tests/python/lib/engine.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def start(self, start_msgs=None):
158158
self._assert_availability()
159159
atexit.register(self.stop)
160160

161-
def stop(self):
161+
def stop(self, signo = signal.SIGTERM):
162162
"""
163163
Остановить движок и проверить, что все в порядке
164164
"""
@@ -167,7 +167,7 @@ def stop(self):
167167

168168
self._assert_availability()
169169
print("\nStopping engine: [{}]".format(cyan(self._engine_bin)))
170-
self.send_signal(signal.SIGTERM)
170+
self.send_signal(signo)
171171

172172
engine_stopped_properly, status = self.wait_termination(30)
173173

@@ -178,7 +178,7 @@ def stop(self):
178178
self._stats_receiver.stop()
179179
if not engine_stopped_properly:
180180
raise RuntimeError("Can't stop engine properly")
181-
self._check_status_code(status, signal.SIGTERM)
181+
self._check_status_code(status, signo)
182182

183183
def restart(self):
184184
"""

0 commit comments

Comments
 (0)