Skip to content

Commit dbdd631

Browse files
committed
Replace C pointers by shared pointer in fuzzy_hash op code
1 parent d3c1ad7 commit dbdd631

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

src/operators/fuzzy_hash.cc

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ bool FuzzyHash::init(const std::string &param2, std::string *error) {
2828
std::string digit;
2929
std::string file;
3030
std::istream *iss;
31-
struct fuzzy_hash_chunk *chunk, *t;
31+
std::shared_ptr<fuzzy_hash_chunk> chunk, t;
3232
std::string err;
3333

3434
auto pos = m_param.find_last_of(' ');
@@ -55,11 +55,10 @@ bool FuzzyHash::init(const std::string &param2, std::string *error) {
5555
}
5656

5757
for (std::string line; std::getline(*iss, line); ) {
58-
chunk = (struct fuzzy_hash_chunk *)calloc(1,
59-
sizeof(struct fuzzy_hash_chunk));
58+
chunk = std::make_shared<fuzzy_hash_chunk>();
6059

61-
chunk->data = strdup(line.c_str());
62-
chunk->next = NULL;
60+
chunk->data = std::shared_ptr<char>(strdup(line.c_str()), free);
61+
chunk->next = nullptr;
6362

6463
if (m_head == NULL) {
6564
m_head = chunk;
@@ -84,22 +83,15 @@ bool FuzzyHash::init(const std::string &param2, std::string *error) {
8483
}
8584

8685
FuzzyHash::~FuzzyHash() {
87-
struct fuzzy_hash_chunk *c = m_head;
88-
while (c) {
89-
struct fuzzy_hash_chunk *t = c;
90-
free(c->data);
91-
c->data = NULL;
92-
c = c->next;
93-
free(t);
94-
}
95-
m_head = NULL;
86+
9687
}
9788

9889

9990
bool FuzzyHash::evaluate(Transaction *t, const std::string &str) {
10091
#ifdef WITH_SSDEEP
10192
char result[FUZZY_MAX_RESULT];
102-
struct fuzzy_hash_chunk *chunk = m_head;
93+
std::shared_ptr<fuzzy_hash_chunk> chunk = m_head;
94+
10395

10496
if (fuzzy_hash_buf((const unsigned char*)str.c_str(),
10597
str.size(), result)) {
@@ -108,7 +100,7 @@ bool FuzzyHash::evaluate(Transaction *t, const std::string &str) {
108100
}
109101

110102
while (chunk != NULL) {
111-
int i = fuzzy_compare(chunk->data, result);
103+
int i = fuzzy_compare(chunk->data.get(), result);
112104
if (i >= m_threshold) {
113105
ms_dbg_a(t, 4, "Fuzzy hash: matched " \
114106
"with score: " + std::to_string(i) + ".");

src/operators/fuzzy_hash.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ namespace operators {
3131

3232

3333
struct fuzzy_hash_chunk {
34-
char *data;
35-
struct fuzzy_hash_chunk *next;
34+
std::shared_ptr<char> data;
35+
std::shared_ptr<fuzzy_hash_chunk> next;
3636
};
3737

3838
class FuzzyHash : public Operator {
@@ -49,7 +49,7 @@ class FuzzyHash : public Operator {
4949
bool init(const std::string &param, std::string *error) override;
5050
private:
5151
int m_threshold;
52-
struct fuzzy_hash_chunk *m_head;
52+
std::shared_ptr<fuzzy_hash_chunk> m_head;
5353
};
5454

5555
} // namespace operators

0 commit comments

Comments
 (0)