Skip to content

Commit 7e5b734

Browse files
committed
C++: Add flow test cases for Boost::Asio.
1 parent 8a5b5d2 commit 7e5b734

File tree

2 files changed

+120
-2
lines changed

2 files changed

+120
-2
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
// --- stub library headers ---
3+
4+
namespace std {
5+
typedef unsigned long size_t;
6+
#define SIZE_MAX 0xFFFFFFFF
7+
8+
template <class T> class allocator {
9+
};
10+
11+
template<class charT> struct char_traits {
12+
};
13+
14+
template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
15+
class basic_string {
16+
public:
17+
basic_string(const charT* s, const Allocator& a = Allocator());
18+
};
19+
20+
typedef basic_string<char> string;
21+
};
22+
23+
namespace boost {
24+
namespace system {
25+
class error_code {
26+
public:
27+
operator bool() const;
28+
};
29+
};
30+
31+
namespace asio {
32+
template<typename Protocol/*, typename Executor*/>
33+
class basic_stream_socket /*: public basic_socket<Protocol, Executor>*/ {
34+
};
35+
36+
namespace ip {
37+
class tcp {
38+
public:
39+
typedef basic_stream_socket<tcp> socket;
40+
};
41+
};
42+
43+
template<typename Allocator = std::allocator<char>> class basic_streambuf {
44+
public:
45+
basic_streambuf(
46+
std::size_t maximum_size = SIZE_MAX,
47+
const Allocator &allocator = Allocator());
48+
};
49+
50+
typedef basic_streambuf<> streambuf;
51+
52+
class mutable_buffer {
53+
};
54+
55+
template<typename Elem, typename Traits, typename Allocator>
56+
mutable_buffer buffer(std::basic_string<Elem, Traits, Allocator> & data);
57+
58+
template<typename SyncReadStream, typename Allocator> std::size_t read_until(
59+
SyncReadStream &s,
60+
asio::basic_streambuf<Allocator> &b,
61+
char delim,
62+
boost::system::error_code &ec);
63+
64+
template<typename SyncWriteStream, typename ConstBufferSequence> std::size_t write(
65+
SyncWriteStream &s,
66+
const ConstBufferSequence &buffers,
67+
boost::system::error_code &ec,
68+
int constraint = 0); // simplified
69+
};
70+
};
71+
72+
// --- test code ---
73+
74+
char *source();
75+
void sink(char *);
76+
void sink(std::string);
77+
void sink(boost::asio::streambuf);
78+
void sink(boost::asio::mutable_buffer);
79+
80+
char *getenv(const char *name);
81+
int send(int, const void*, int, int);
82+
83+
void test(boost::asio::ip::tcp::socket &socket) {
84+
boost::asio::streambuf recv_buffer;
85+
boost::system::error_code error;
86+
87+
boost::asio::read_until(socket, recv_buffer, '\0', error);
88+
if (error) {
89+
// ...
90+
}
91+
sink(recv_buffer); // $ MISSING: ir
92+
93+
boost::asio::write(socket, recv_buffer, error); // $ MISSING: ir
94+
95+
// ---
96+
97+
std::string send_str = std::string(source());
98+
sink(send_str); // $ ir
99+
100+
boost::asio::mutable_buffer send_buffer = boost::asio::buffer(send_str);
101+
sink(send_buffer); // $ MISSING: ir
102+
103+
boost::asio::write(socket, send_buffer, error); // $ MISSING: ir
104+
if (error) {
105+
// ...
106+
}
107+
}

cpp/ql/test/library-tests/dataflow/external-models/flow.ql

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import TestUtilities.dataflow.FlowTestCommon
22
import cpp
33
import semmle.code.cpp.ir.dataflow.DataFlow
4-
import semmle.code.cpp.dataflow.ExternalFlow
4+
import semmle.code.cpp.security.FlowSources
55

66
module IRTest {
77
private import semmle.code.cpp.ir.IR
@@ -10,11 +10,22 @@ module IRTest {
1010
/** Common data flow configuration to be used by tests. */
1111
module TestAllocationConfig implements DataFlow::ConfigSig {
1212
predicate isSource(DataFlow::Node source) {
13+
// external flow source node
1314
sourceNode(source, _)
15+
or
16+
// test source function
17+
source.asExpr().(FunctionCall).getTarget().getName() = "source"
1418
}
1519

1620
predicate isSink(DataFlow::Node sink) {
17-
sinkNode(sink, "test-sink")
21+
// external flow sink node
22+
sinkNode(sink, _)
23+
or
24+
// test sink function
25+
exists(FunctionCall call |
26+
call.getTarget().getName() = "sink" and
27+
sink.asExpr() = call.getAnArgument()
28+
)
1829
}
1930
}
2031

0 commit comments

Comments
 (0)