|
| 1 | + |
| 2 | +typedef unsigned long size_t; |
| 3 | + |
| 4 | +namespace std |
| 5 | +{ |
| 6 | + template<class charT> struct char_traits; |
| 7 | + |
| 8 | + typedef size_t streamsize; |
| 9 | + |
| 10 | + template <class T> class allocator { |
| 11 | + public: |
| 12 | + allocator() throw(); |
| 13 | + }; |
| 14 | + |
| 15 | + template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
| 16 | + class basic_string { |
| 17 | + public: |
| 18 | + explicit basic_string(const Allocator& a = Allocator()); |
| 19 | + basic_string(const charT* s, const Allocator& a = Allocator()); |
| 20 | + |
| 21 | + const charT* c_str() const; |
| 22 | + }; |
| 23 | + |
| 24 | + typedef basic_string<char> string; |
| 25 | + |
| 26 | + template <class charT, class traits = char_traits<charT> > |
| 27 | + class basic_istream /*: virtual public basic_ios<charT,traits> - not needed for this test */ { |
| 28 | + public: |
| 29 | + basic_istream<charT,traits>& operator>>(int& n); |
| 30 | + }; |
| 31 | + |
| 32 | + template <class charT, class traits = char_traits<charT> > |
| 33 | + class basic_ostream /*: virtual public basic_ios<charT,traits> - not needed for this test */ { |
| 34 | + public: |
| 35 | + typedef charT char_type; |
| 36 | + basic_ostream<charT,traits>& write(const char_type* s, streamsize n); |
| 37 | + |
| 38 | + basic_ostream<charT, traits>& operator<<(int n); |
| 39 | + }; |
| 40 | + |
| 41 | + template<class charT, class traits> basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const charT*); |
| 42 | + template<class charT, class traits, class Allocator> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str); |
| 43 | + |
| 44 | + template<class charT, class traits = char_traits<charT>> |
| 45 | + class basic_iostream : public basic_istream<charT, traits>, public basic_ostream<charT, traits> { |
| 46 | + public: |
| 47 | + }; |
| 48 | + |
| 49 | + template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT>> |
| 50 | + class basic_stringstream : public basic_iostream<charT, traits> { |
| 51 | + public: |
| 52 | + explicit basic_stringstream(/*ios_base::openmode which = ios_base::out|ios_base::in - not needed for this test*/); |
| 53 | + |
| 54 | + basic_string<charT, traits, Allocator> str() const; |
| 55 | + }; |
| 56 | + |
| 57 | + using stringstream = basic_stringstream<char>; |
| 58 | +} |
| 59 | + |
| 60 | +char *source(); |
| 61 | +void sink(const char *s) {}; |
| 62 | +void sink(const std::string &s) {}; |
| 63 | +void sink(const std::stringstream &s) {}; |
| 64 | + |
| 65 | +void test_string() |
| 66 | +{ |
| 67 | + char *a = source(); |
| 68 | + std::string b("123"); |
| 69 | + std::string c(source()); |
| 70 | + |
| 71 | + sink(a); // tainted |
| 72 | + sink(b); |
| 73 | + sink(c); // tainted [NOT DETECTED] |
| 74 | + sink(b.c_str()); |
| 75 | + sink(c.c_str()); // tainted [NOT DETECTED] |
| 76 | +} |
| 77 | + |
| 78 | +void test_stringstream() |
| 79 | +{ |
| 80 | + std::stringstream ss1, ss2, ss3, ss4, ss5; |
| 81 | + std::string t(source()); |
| 82 | + |
| 83 | + ss1 << "1234"; |
| 84 | + ss2 << source(); |
| 85 | + ss3 << "123" << source(); |
| 86 | + ss4 << source() << "456"; |
| 87 | + ss5 << t; |
| 88 | + |
| 89 | + sink(ss1); |
| 90 | + sink(ss2); // tainted [NOT DETECTED] |
| 91 | + sink(ss3); // tainted [NOT DETECTED] |
| 92 | + sink(ss4); // tainted [NOT DETECTED] |
| 93 | + sink(ss5); // tainted [NOT DETECTED] |
| 94 | + sink(ss1.str()); |
| 95 | + sink(ss2.str()); // tainted [NOT DETECTED] |
| 96 | + sink(ss3.str()); // tainted [NOT DETECTED] |
| 97 | + sink(ss4.str()); // tainted [NOT DETECTED] |
| 98 | + sink(ss5.str()); // tainted [NOT DETECTED] |
| 99 | +} |
| 100 | + |
| 101 | +void test_stringstream_int(int source) |
| 102 | +{ |
| 103 | + std::stringstream ss1, ss2; |
| 104 | + |
| 105 | + ss1 << 1234; |
| 106 | + ss2 << source; |
| 107 | + |
| 108 | + sink(ss1); |
| 109 | + sink(ss2); // tainted [NOT DETECTED] |
| 110 | + sink(ss1.str()); |
| 111 | + sink(ss2.str()); // tainted [NOT DETECTED] |
| 112 | +} |
0 commit comments