Skip to content

Commit cb7fb52

Browse files
authored
Added string sanitizers.
1 parent 83be14a commit cb7fb52

File tree

6 files changed

+159
-76
lines changed

6 files changed

+159
-76
lines changed

src/torrent/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ libtorrent_torrent_la_SOURCES = \
101101
utils/scheduler.h \
102102
utils/signal_bitfield.cc \
103103
utils/signal_bitfield.h \
104+
utils/string_manip.cc \
104105
utils/string_manip.h \
105106
utils/thread.cc \
106107
utils/thread.h \

src/torrent/poll_kqueue.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
// TODO: Change to LOG_CONNECTION_POLL
1919

20+
// TODO: Optimize table memory size, and add a reference to Event for direct lookup.
21+
2022
#define LT_LOG_EVENT(log_fmt, ...) \
2123
lt_log_print(LOG_CONNECTION_FD, "kqueue->%i : %s : " log_fmt, event->file_descriptor(), event->type_name(), __VA_ARGS__);
2224

src/torrent/torrent.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ initialize() {
138138
// them to finish is not required, but recommended.
139139
void
140140
cleanup() {
141-
if (manager == NULL)
141+
if (manager == nullptr)
142142
throw internal_error("torrent::cleanup() called but the library is not initialized.");
143143

144144
// Might need to wait for the threads to finish?
@@ -148,6 +148,7 @@ cleanup() {
148148
thread_disk()->stop_thread_wait();
149149
net_thread::thread()->stop_thread_wait();
150150

151+
// TODO: Set these to null.
151152
delete thread_tracker();
152153
delete thread_disk();
153154
delete net_thread::thread();

src/torrent/utils/string_manip.cc

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include "config.h"
2+
3+
#include "torrent/utils/string_manip.h"
4+
5+
namespace torrent::utils {
6+
7+
namespace {
8+
9+
inline char
10+
to_hex_char(char val, bool pos) {
11+
if (pos)
12+
val = (val >> 4) & 0x0F;
13+
else
14+
val = val & 0x0F;
15+
16+
if (val < 10)
17+
return '0' + val;
18+
19+
return 'A' + (val - 10);
20+
}
21+
22+
} // namespace
23+
24+
std::string
25+
trim_string(const std::string& str) {
26+
std::string::size_type pos{};
27+
std::string::size_type end{str.length()};
28+
29+
while (pos != end && str[pos] >= ' ' && str[pos] <= '~')
30+
pos++;
31+
32+
while (end != pos && str[end - 1] >= ' ' && str[end - 1] <= '~')
33+
end--;
34+
35+
return str.substr(pos, end - pos);
36+
}
37+
38+
std::string
39+
string_with_escape_codes(const std::string& str) {
40+
std::string result;
41+
42+
for (auto c : str) {
43+
if (c < ' ' || c > '~') {
44+
result += '%' + to_hex_char(c, true) + to_hex_char(c, false);
45+
continue;
46+
}
47+
48+
result += c;
49+
}
50+
51+
return result;
52+
}
53+
54+
std::string
55+
sanitize_string(const std::string& str) {
56+
std::string result;
57+
bool unprintable{};
58+
bool space{};
59+
60+
for (auto c : str) {
61+
if (c < ' ' || c > '~') {
62+
if (c == '\n' || c == '\r' || c == '\t') {
63+
if (!space && !unprintable)
64+
result += ' ';
65+
66+
space = true;
67+
continue;
68+
}
69+
70+
if (!unprintable)
71+
result += '*';
72+
73+
unprintable = true;
74+
space = false;
75+
continue;
76+
}
77+
78+
result += c;
79+
unprintable = false;
80+
space = false;
81+
}
82+
83+
return trim_string(result);
84+
}
85+
86+
std::string
87+
sanitize_string_with_escape_codes(const std::string& str) {
88+
std::string result;
89+
bool space{};
90+
91+
for (auto c : str) {
92+
if (c < ' ' || c > '~') {
93+
if (c == '\n' || c == '\r' || c == '\t') {
94+
if (!space)
95+
result += ' ';
96+
97+
space = true;
98+
continue;
99+
}
100+
101+
result += '%' + to_hex_char(c, true) + to_hex_char(c, false);
102+
103+
space = false;
104+
continue;
105+
}
106+
107+
result += c;
108+
space = false;
109+
}
110+
111+
return trim_string(result);
112+
}
113+
114+
std::string
115+
sanitize_string_with_tags(const std::string& str) {
116+
bool in_tag{};
117+
std::string result;
118+
std::string sanitized = sanitize_string(str);
119+
120+
for (auto c : sanitized) {
121+
if (c == '>') {
122+
in_tag = false;
123+
continue;
124+
}
125+
126+
if (in_tag || c == '<') {
127+
in_tag = true;
128+
continue;
129+
}
130+
131+
result += c;
132+
}
133+
134+
result = trim_string(result);
135+
136+
if (result.empty())
137+
return trim_string(sanitized);
138+
139+
return result;
140+
}
141+
142+
} // namespace torrent::utils

src/torrent/utils/string_manip.h

Lines changed: 10 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,21 @@
1-
#ifndef LIBTORRENT_TORRENT_UTILS_STRING_H
2-
#define LIBTORRENT_TORRENT_UTILS_STRING_H
1+
#ifndef LIBTORRENT_TORRENT_UTILS_STRING_MANIP_H
2+
#define LIBTORRENT_TORRENT_UTILS_STRING_MANIP_H
33

44
#include <string>
5+
#include <torrent/common.h>
56

67
namespace torrent::utils {
78

8-
std::string
9-
trim_string(const std::string& str) {
10-
std::string::size_type pos{};
11-
std::string::size_type end{str.length()};
9+
// TODO: Deprecate trim_string and sanitize_string.
1210

13-
while (pos != end && str[pos] >= ' ' && str[pos] <= '~')
14-
pos++;
11+
std::string trim_string(const std::string& str) LIBTORRENT_EXPORT;
1512

16-
while (end != pos && str[end - 1] >= ' ' && str[end - 1] <= '~')
17-
end--;
13+
std::string string_with_escape_codes(const std::string& str) LIBTORRENT_EXPORT;
1814

19-
return str.substr(pos, end - pos);
20-
}
21-
22-
std::string
23-
sanitize_string(const std::string& str) {
24-
std::string result;
25-
bool unprintable{};
26-
bool space{};
27-
28-
for (auto c : str) {
29-
if (c < ' ' || c > '~') {
30-
if (c == '\n' || c == '\r' || c == '\t') {
31-
if (!space && !unprintable)
32-
result += ' ';
33-
34-
space = true;
35-
continue;
36-
}
37-
38-
if (!unprintable)
39-
result += '*';
40-
41-
unprintable = true;
42-
space = false;
43-
continue;
44-
}
45-
46-
result += c;
47-
unprintable = false;
48-
space = false;
49-
}
50-
51-
return trim_string(result);
52-
}
53-
54-
std::string
55-
sanitize_string_with_tags(const std::string& str) {
56-
bool in_tag{};
57-
std::string result;
58-
std::string sanitized = sanitize_string(str);
59-
60-
for (auto c : sanitized) {
61-
if (c == '>') {
62-
in_tag = false;
63-
continue;
64-
}
65-
66-
if (in_tag || c == '<') {
67-
in_tag = true;
68-
continue;
69-
}
70-
71-
result += c;
72-
}
73-
74-
result = trim_string(result);
75-
76-
if (result.empty())
77-
return trim_string(sanitized);
78-
79-
return result;
80-
}
15+
std::string sanitize_string(const std::string& str) LIBTORRENT_EXPORT;
16+
std::string sanitize_string_with_escape_codes(const std::string& str) LIBTORRENT_EXPORT;
17+
std::string sanitize_string_with_tags(const std::string& str) LIBTORRENT_EXPORT;
8118

8219
} // namespace torrent::utils
8320

84-
#endif // LIBTORRENT_TORRENT_UTILS_STRINGSTRING
21+
#endif // LIBTORRENT_TORRENT_UTILS_STRING_MANIP_H

src/tracker/tracker_http.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class TrackerHttp : public TrackerWorker {
5050
net::HttpGet m_get;
5151
std::shared_ptr<std::stringstream> m_data;
5252

53-
bool m_drop_deliminator;
53+
bool m_drop_deliminator{};
5454
std::string m_current_tracker_id;
5555

56-
bool m_requested_scrape;
56+
bool m_requested_scrape{};
5757
utils::SchedulerEntry m_delay_scrape;
5858
};
5959

0 commit comments

Comments
 (0)