Skip to content

Commit 7441b01

Browse files
committed
FIXed unprotected string.erase() methods.
1 parent a2473a1 commit 7441b01

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

process/passivedns.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ std::string PassiveDNSPlugin::get_name(const char *data) const
214214
data += ((uint8_t) data[0] + 1);
215215
}
216216

217-
if (name[0] == '.') {
217+
if (name.length() > 0 && name[0] == '.') {
218218
name.erase(0, 1);
219219
}
220220

@@ -433,7 +433,7 @@ bool PassiveDNSPlugin::process_ptr_record(std::string name, RecordExtPassiveDNS
433433
{
434434
memset(&rec->ip, 0, sizeof(rec->ip));
435435

436-
if (name[name.length() - 1] == '.') {
436+
if (name.length() > 0 && name[name.length() - 1] == '.') {
437437
name.erase(name.length() - 1);
438438
}
439439

@@ -446,7 +446,7 @@ bool PassiveDNSPlugin::process_ptr_record(std::string name, RecordExtPassiveDNS
446446
size_t type_pos = name.find(type_str);
447447
size_t begin = 0, end = 0, cnt = 0;
448448
uint8_t *ip;
449-
if (type_pos + type_str.length() == name.length()) {
449+
if (type_pos != std::string::npos && type_pos + type_str.length() == name.length()) {
450450
// IPv4
451451
name.erase(type_pos);
452452
rec->ip_version = IP::v4;
@@ -471,7 +471,7 @@ bool PassiveDNSPlugin::process_ptr_record(std::string name, RecordExtPassiveDNS
471471
} else {
472472
type_str = ".ip6.arpa";
473473
type_pos = name.find(type_str);
474-
if (type_pos + type_str.length() == name.length()) {
474+
if (type_pos != std::string::npos && type_pos + type_str.length() == name.length()) {
475475
// IPv6
476476
name.erase(type_pos);
477477
rec->ip_version = IP::v6;

utils.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,16 @@ bool str2bool(std::string str)
8484

8585
void trim_str(std::string &str)
8686
{
87-
str.erase(0, str.find_first_not_of(" \t\n\r"));
88-
str.erase(str.find_last_not_of(" \t\n\r") + 1);
87+
// when std::string::npos returned by find
88+
// string.erase() will remove all characters till end
89+
// https://cplusplus.com/reference/string/string/erase/
90+
if (str.length() > 0) {
91+
str.erase(0, str.find_first_not_of(" \t\n\r"));
92+
size_t pos = str.find_last_not_of(" \t\n\r");
93+
if (pos != std::string::npos) {
94+
str.erase(str.find_last_not_of(" \t\n\r") + 1);
95+
}
96+
}
8997
}
9098

9199
void phton64(uint8_t *p, uint64_t v)

0 commit comments

Comments
 (0)