Replace C pointers by shared pointer in fuzzy_hash op code

This commit is contained in:
Ervin Hegedus 2025-03-12 22:09:51 +01:00
parent d3c1ad7177
commit dbdd6318ff
No known key found for this signature in database
GPG Key ID: 5FA5BC3F5EC41F61
2 changed files with 11 additions and 19 deletions

View File

@ -28,7 +28,7 @@ bool FuzzyHash::init(const std::string &param2, std::string *error) {
std::string digit;
std::string file;
std::istream *iss;
struct fuzzy_hash_chunk *chunk, *t;
std::shared_ptr<fuzzy_hash_chunk> chunk, t;
std::string err;
auto pos = m_param.find_last_of(' ');
@ -55,11 +55,10 @@ bool FuzzyHash::init(const std::string &param2, std::string *error) {
}
for (std::string line; std::getline(*iss, line); ) {
chunk = (struct fuzzy_hash_chunk *)calloc(1,
sizeof(struct fuzzy_hash_chunk));
chunk = std::make_shared<fuzzy_hash_chunk>();
chunk->data = strdup(line.c_str());
chunk->next = NULL;
chunk->data = std::shared_ptr<char>(strdup(line.c_str()), free);
chunk->next = nullptr;
if (m_head == NULL) {
m_head = chunk;
@ -84,22 +83,15 @@ bool FuzzyHash::init(const std::string &param2, std::string *error) {
}
FuzzyHash::~FuzzyHash() {
struct fuzzy_hash_chunk *c = m_head;
while (c) {
struct fuzzy_hash_chunk *t = c;
free(c->data);
c->data = NULL;
c = c->next;
free(t);
}
m_head = NULL;
}
bool FuzzyHash::evaluate(Transaction *t, const std::string &str) {
#ifdef WITH_SSDEEP
char result[FUZZY_MAX_RESULT];
struct fuzzy_hash_chunk *chunk = m_head;
std::shared_ptr<fuzzy_hash_chunk> chunk = m_head;
if (fuzzy_hash_buf((const unsigned char*)str.c_str(),
str.size(), result)) {
@ -108,7 +100,7 @@ bool FuzzyHash::evaluate(Transaction *t, const std::string &str) {
}
while (chunk != NULL) {
int i = fuzzy_compare(chunk->data, result);
int i = fuzzy_compare(chunk->data.get(), result);
if (i >= m_threshold) {
ms_dbg_a(t, 4, "Fuzzy hash: matched " \
"with score: " + std::to_string(i) + ".");

View File

@ -31,8 +31,8 @@ namespace operators {
struct fuzzy_hash_chunk {
char *data;
struct fuzzy_hash_chunk *next;
std::shared_ptr<char> data;
std::shared_ptr<fuzzy_hash_chunk> next;
};
class FuzzyHash : public Operator {
@ -49,7 +49,7 @@ class FuzzyHash : public Operator {
bool init(const std::string &param, std::string *error) override;
private:
int m_threshold;
struct fuzzy_hash_chunk *m_head;
std::shared_ptr<fuzzy_hash_chunk> m_head;
};
} // namespace operators