diff --git a/headers/modsecurity/variable_value.h b/headers/modsecurity/variable_value.h index f787177624..6996098a1a 100644 --- a/headers/modsecurity/variable_value.h +++ b/headers/modsecurity/variable_value.h @@ -47,6 +47,13 @@ class VariableValue { m_value(value != nullptr?*value:"") { } + VariableValue(std::string&& key, + std::string&& value) + : m_collection(""), + m_key(std::move(key)), + m_value(std::move(value)) + { m_keyWithCollection = m_key; } + VariableValue(const std::string *collection, const std::string *key, const std::string *value) @@ -56,6 +63,14 @@ class VariableValue { m_value(*value) { } + VariableValue(const std::string *collection, + std::string&& key, + std::string&& value) + : m_collection(*collection), + m_key(std::move(key)), + m_value(std::move(value)) + { m_keyWithCollection = m_collection + ":" + m_key; } + explicit VariableValue(const VariableValue *o) : m_collection(o->m_collection), m_key(o->m_key), diff --git a/src/collection/backend/lmdb.cc b/src/collection/backend/lmdb.cc index 4f58eb7c7a..da2a168135 100644 --- a/src/collection/backend/lmdb.cc +++ b/src/collection/backend/lmdb.cc @@ -255,6 +255,7 @@ void LMDB::resolveSingleMatch(const std::string& var, reinterpret_cast(mdb_value_ret.mv_data), mdb_value_ret.mv_size); VariableValue *v = new VariableValue(&var, a); + delete a; l->push_back(v); } @@ -427,22 +428,28 @@ void LMDB::resolveMultiMatches(const std::string& var, while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { l->insert(l->begin(), new VariableValue( &m_name, - new std::string(reinterpret_cast(key.mv_data), + std::string(reinterpret_cast(key.mv_data), key.mv_size), - new std::string(reinterpret_cast(data.mv_data), + std::string(reinterpret_cast(data.mv_data), data.mv_size))); } } else { - while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { - char *a = reinterpret_cast(key.mv_data); - if (strncmp(var.c_str(), a, keySize) == 0) { - l->insert(l->begin(), new VariableValue( - &m_name, - new std::string(reinterpret_cast(key.mv_data), - key.mv_size), - new std::string(reinterpret_cast(data.mv_data), - data.mv_size))); - } + string2val(var, &key); + rc = mdb_cursor_get(cursor, &key, &data, MDB_SET_RANGE); + if (rc == 0) { + do { + char *a = reinterpret_cast(key.mv_data); + if (strncmp(var.c_str(), a, keySize) == 0) { + l->insert(l->begin(), new VariableValue( + &m_name, + std::string(reinterpret_cast(key.mv_data), + key.mv_size), + std::string(reinterpret_cast(data.mv_data), + data.mv_size))); + } else { + break; + } + } while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0); } } @@ -489,9 +496,9 @@ void LMDB::resolveRegularExpression(const std::string& var, } VariableValue *v = new VariableValue( - new std::string(reinterpret_cast(key.mv_data), + std::string(reinterpret_cast(key.mv_data), key.mv_size), - new std::string(reinterpret_cast(data.mv_data), + std::string(reinterpret_cast(data.mv_data), data.mv_size)); l->insert(l->begin(), v); }