Skip to content

Commit aae1b51

Browse files
Bugfix: move construct an empty fast_io::list
- before this patch, the list_move.cc#L10 will crash
1 parent 7c138a6 commit aae1b51

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

include/fast_io_dsal/impl/list.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -907,15 +907,19 @@ class list
907907
}
908908

909909
inline constexpr list(list &&other) noexcept
910-
: imp(other.imp)
911910
#if 0
912-
, allochdl(std::move(other.allochdl))
911+
: allochdl(std::move(other.allochdl))
913912
#endif
914913
{
915-
auto prev = static_cast<::fast_io::containers::details::list_node_common *>(imp.prev);
916-
auto next = static_cast<::fast_io::containers::details::list_node_common *>(imp.next);
917-
next->prev = prev->next = __builtin_addressof(imp);
918-
other.imp = {__builtin_addressof(other.imp), __builtin_addressof(other.imp)};
914+
if (other.empty()) {
915+
imp = {__builtin_addressof(imp), __builtin_addressof(imp)};
916+
} else {
917+
imp = other.imp;
918+
auto prev = static_cast<::fast_io::containers::details::list_node_common *>(imp.prev);
919+
auto next = static_cast<::fast_io::containers::details::list_node_common *>(imp.next);
920+
next->prev = prev->next = __builtin_addressof(imp);
921+
other.imp = {__builtin_addressof(other.imp), __builtin_addressof(other.imp)};
922+
}
919923
}
920924

921925
inline constexpr list &operator=(list &&other) noexcept
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
#include <fast_io_dsal/list.h>
3+
4+
int main() {
5+
::fast_io::list<int> l1{};
6+
if (!l1.empty()) {
7+
::fast_io::fast_terminate();
8+
}
9+
10+
::fast_io::list<int> l2(::std::move(l1));
11+
if (!l2.empty()) {
12+
::fast_io::fast_terminate();
13+
}
14+
15+
::fast_io::list<int> l3{};
16+
l3.emplace_back(1);
17+
if (l3.empty()) {
18+
::fast_io::fast_terminate();
19+
}
20+
21+
::fast_io::list<int> l4(::std::move(l3));
22+
if (l4.empty()) {
23+
::fast_io::fast_terminate();
24+
}
25+
26+
return 0;
27+
}

0 commit comments

Comments
 (0)