Skip to content

Commit 178ca0a

Browse files
authored
[k2] fix memory leak in timezone_cache move assignment operator (#1334)
1 parent f093f2c commit 178ca0a

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

runtime-light/stdlib/time/timelib-timezone-cache.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ class timezone_cache final {
4848
m_cache.emplace(tzinfo);
4949
}
5050

51+
void clear() noexcept {
52+
(kphp::memory::libc_alloc_guard{}, std::ranges::for_each(m_cache, [](timelib_tzinfo* tzinfo) noexcept { timelib_tzinfo_dtor(tzinfo); }));
53+
m_cache.clear();
54+
}
55+
5156
timezone_cache() noexcept = default;
5257

5358
timezone_cache(std::initializer_list<std::string_view> tzs) noexcept {
5459
kphp::memory::libc_alloc_guard _{};
5560
std::ranges::for_each(tzs, [this](std::string_view tz) noexcept {
5661
int errc{}; // it's intentionally declared as 'int' since timelib_parse_tzfile accepts 'int'
57-
auto* tzinfo{timelib_parse_tzfile(tz.data(), timelib_builtin_db(), std::addressof(errc))};
58-
if (tzinfo == nullptr) [[unlikely]] {
59-
kphp::log::warning("can't get timezone info: timezone -> {}, error -> {}", tz, timelib_get_error_message(errc));
60-
return;
61-
}
62-
put(tzinfo);
62+
put(timelib_parse_tzfile(tz.data(), timelib_builtin_db(), std::addressof(errc)));
6363
});
6464
}
6565

@@ -68,6 +68,7 @@ class timezone_cache final {
6868

6969
timezone_cache& operator=(timezone_cache&& other) noexcept {
7070
if (this != std::addressof(other)) {
71+
clear();
7172
m_cache = std::move(other.m_cache);
7273
}
7374
return *this;
@@ -77,8 +78,9 @@ class timezone_cache final {
7778
timezone_cache& operator=(const timezone_cache&) = delete;
7879

7980
~timezone_cache() {
80-
kphp::memory::libc_alloc_guard _{};
81-
std::ranges::for_each(m_cache, [](timelib_tzinfo* tzinfo) noexcept { timelib_tzinfo_dtor(tzinfo); });
81+
if (!m_cache.empty()) {
82+
clear();
83+
}
8284
}
8385
};
8486

0 commit comments

Comments
 (0)