Skip to content

Commit 0a18a6a

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

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
if (l1.empty()) {
17+
::fast_io::fast_terminate();
18+
}
19+
20+
::fast_io::list<int> l4(::std::move(l3));
21+
if (l2.empty()) {
22+
::fast_io::fast_terminate();
23+
}
24+
25+
return 0;
26+
}

0 commit comments

Comments
 (0)