Skip to content

Commit ca20f17

Browse files
committed
C++: Implement move constructor in terms of swap. I'm haven't found anything online on whether this is good or bad, and the only reason for not doing it might be performance.
1 parent 1a95095 commit ca20f17

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

cpp/ql/test/library-tests/dataflow/taint-tests/localTaint.expected

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,14 @@
623623
| taint.cpp:483:18:483:19 | ref arg & ... | taint.cpp:483:19:483:19 | n [inner post update] | |
624624
| taint.cpp:483:19:483:19 | n | taint.cpp:483:18:483:19 | & ... | |
625625
| taint.cpp:483:28:483:34 | source1 | taint.cpp:483:11:483:15 | ref arg & ... | TAINT |
626+
| taint.cpp:491:14:491:14 | t | taint.cpp:491:14:491:14 | t | |
627+
| taint.cpp:491:14:491:14 | t | taint.cpp:491:14:491:14 | t | |
628+
| taint.cpp:491:14:491:14 | t | taint.cpp:491:52:491:52 | t | |
629+
| taint.cpp:491:14:491:14 | t | taint.cpp:491:52:491:52 | t | |
630+
| taint.cpp:501:3:501:7 | this | taint.cpp:501:25:501:28 | this | |
631+
| taint.cpp:501:17:501:20 | that | taint.cpp:501:17:501:20 | that | |
632+
| taint.cpp:501:17:501:20 | that | taint.cpp:501:30:501:33 | that | |
633+
| taint.cpp:501:30:501:33 | ref arg that | taint.cpp:501:17:501:20 | that | |
626634
| taint.cpp:502:3:502:7 | this | taint.cpp:502:30:502:44 | constructor init of field data [pre-this] | |
627635
| taint.cpp:502:22:502:25 | that | taint.cpp:502:35:502:38 | that | |
628636
| taint.cpp:502:40:502:43 | data | taint.cpp:502:30:502:44 | constructor init of field data | TAINT |
@@ -698,3 +706,13 @@
698706
| taint.cpp:563:6:563:14 | ref arg call to move | taint.cpp:563:16:563:16 | x [inner post update] | |
699707
| taint.cpp:563:6:563:14 | ref arg call to move | taint.cpp:566:7:566:7 | x | |
700708
| taint.cpp:563:16:563:16 | x | taint.cpp:563:6:563:14 | call to move | |
709+
| taint.cpp:571:20:571:28 | move_from | taint.cpp:572:2:572:10 | move_from | |
710+
| taint.cpp:571:20:571:28 | move_from | taint.cpp:574:7:574:15 | move_from | |
711+
| taint.cpp:571:20:571:28 | move_from | taint.cpp:576:38:576:46 | move_from | |
712+
| taint.cpp:572:2:572:10 | move_from [post update] | taint.cpp:574:7:574:15 | move_from | |
713+
| taint.cpp:572:2:572:10 | move_from [post update] | taint.cpp:576:38:576:46 | move_from | |
714+
| taint.cpp:572:2:572:26 | ... = ... | taint.cpp:574:17:574:20 | data | |
715+
| taint.cpp:572:19:572:24 | call to source | taint.cpp:572:2:572:26 | ... = ... | |
716+
| taint.cpp:576:28:576:36 | ref arg call to move | taint.cpp:576:38:576:46 | move_from [inner post update] | |
717+
| taint.cpp:576:28:576:48 | call to Class | taint.cpp:578:7:578:13 | move_to | |
718+
| taint.cpp:576:38:576:46 | move_from | taint.cpp:576:28:576:36 | call to move | |

cpp/ql/test/library-tests/dataflow/taint-tests/taint.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ void test_getdelim(FILE* source1) {
488488
namespace std
489489
{
490490
template <class T>
491-
T &&move(T &t) noexcept; // simplified signature
491+
T &&move(T &t) noexcept { return static_cast<T&&>(t); } // simplified signature (and implementation)
492492
}
493493

494494
namespace IntWrapper
@@ -498,7 +498,7 @@ namespace IntWrapper
498498
int data;
499499

500500
Class() = default;
501-
Class(Class&&) = default;
501+
Class(Class&& that) { swap(that); }
502502
Class(const Class &that) : data(that.data) {}
503503

504504
Class &operator=(const Class &that)
@@ -565,3 +565,15 @@ void test_move_assignment_operator()
565565
sink(y.data); // tainted [FALSE NEGATIVE in IR]
566566
sink(x.data); // tainted
567567
}
568+
569+
void test_move_constructor()
570+
{
571+
IntWrapper::Class move_from;
572+
move_from.data = source();
573+
574+
sink(move_from.data); // tainted
575+
576+
IntWrapper::Class move_to(std::move(move_from));
577+
578+
sink(move_to.data); // tainted [FALSE NEGATIVE in IR]
579+
}

cpp/ql/test/library-tests/dataflow/taint-tests/taint.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@
8181
| taint.cpp:565:9:565:12 | data | taint.cpp:556:20:556:20 | x |
8282
| taint.cpp:565:9:565:12 | data | taint.cpp:558:11:558:16 | call to source |
8383
| taint.cpp:566:9:566:12 | data | taint.cpp:558:11:558:16 | call to source |
84+
| taint.cpp:574:17:574:20 | data | taint.cpp:572:19:572:24 | call to source |
85+
| taint.cpp:578:15:578:18 | data | taint.cpp:571:20:571:28 | move_from |
86+
| taint.cpp:578:15:578:18 | data | taint.cpp:572:19:572:24 | call to source |

cpp/ql/test/library-tests/dataflow/taint-tests/test_diff.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@
4949
| taint.cpp:551:10:551:13 | taint.cpp:544:24:544:25 | AST only |
5050
| taint.cpp:565:9:565:12 | taint.cpp:556:20:556:20 | AST only |
5151
| taint.cpp:565:9:565:12 | taint.cpp:558:11:558:16 | AST only |
52+
| taint.cpp:578:15:578:18 | taint.cpp:571:20:571:28 | AST only |
53+
| taint.cpp:578:15:578:18 | taint.cpp:572:19:572:24 | AST only |

cpp/ql/test/library-tests/dataflow/taint-tests/test_ir.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@
4444
| taint.cpp:551:10:551:13 | data | taint.cpp:545:12:545:17 | call to source |
4545
| taint.cpp:560:9:560:12 | data | taint.cpp:558:11:558:16 | call to source |
4646
| taint.cpp:566:9:566:12 | data | taint.cpp:558:11:558:16 | call to source |
47+
| taint.cpp:574:17:574:20 | data | taint.cpp:572:19:572:24 | call to source |

0 commit comments

Comments
 (0)