mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-13 21:36:00 +03:00
Refactor to remove duplicate code in ValidateSchema & ValidateDTD
- Reported by Sonarcloud
This commit is contained in:
parent
2fb446ab2d
commit
d1e7e7b4f2
@ -28,6 +28,7 @@
|
||||
#include <utility>
|
||||
|
||||
#include "src/operators/operator.h"
|
||||
#include "validate_schema.h"
|
||||
|
||||
|
||||
namespace modsecurity {
|
||||
@ -62,40 +63,22 @@ class ValidateDTD : public Operator {
|
||||
|
||||
|
||||
static void error_runtime(void *ctx, const char *msg, ...) {
|
||||
const Transaction *t = reinterpret_cast<Transaction *>(ctx);
|
||||
char buf[1024];
|
||||
std::string s;
|
||||
va_list args;
|
||||
|
||||
va_start(args, msg);
|
||||
int len = vsnprintf(buf, sizeof(buf), msg, args);
|
||||
ValidateSchema::callback_func(ctx, ValidateSchema::log_msg, ValidateSchema::PREFIX_ERROR, msg, args);
|
||||
va_end(args);
|
||||
|
||||
if (len > 0) {
|
||||
s = "XML Error: " + std::string(buf);
|
||||
}
|
||||
ms_dbg_a(t, 4, s);
|
||||
}
|
||||
|
||||
|
||||
static void warn_runtime(void *ctx, const char *msg, ...) {
|
||||
const Transaction *t = reinterpret_cast<Transaction *>(ctx);
|
||||
char buf[1024];
|
||||
std::string s;
|
||||
va_list args;
|
||||
|
||||
va_start(args, msg);
|
||||
int len = vsnprintf(buf, sizeof(buf), msg, args);
|
||||
ValidateSchema::callback_func(ctx, ValidateSchema::log_msg, ValidateSchema::PREFIX_WARNING, msg, args);
|
||||
va_end(args);
|
||||
|
||||
if (len > 0) {
|
||||
s = "XML Warning: " + std::string(buf);
|
||||
}
|
||||
ms_dbg_a(t, 4, s);
|
||||
}
|
||||
|
||||
|
||||
static void null_error(void *ctx, const char *msg, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
|
||||
static void null_error(void *, const char *, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -45,71 +45,61 @@ class ValidateSchema : public Operator {
|
||||
|
||||
|
||||
static void error_load(void *ctx, const char *msg, ...) {
|
||||
std::string *t = reinterpret_cast<std::string *>(ctx);
|
||||
char buf[1024];
|
||||
va_list args;
|
||||
|
||||
va_start(args, msg);
|
||||
int len = vsnprintf(buf, sizeof(buf), msg, args);
|
||||
callback_func(ctx, append_msg, PREFIX_ERROR, msg, args);
|
||||
va_end(args);
|
||||
|
||||
if (len > 0) {
|
||||
t->append("XML Error: " + std::string(buf));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void warn_load(void *ctx, const char *msg, ...) {
|
||||
std::string *t = reinterpret_cast<std::string *>(ctx);
|
||||
char buf[1024];
|
||||
va_list args;
|
||||
|
||||
va_start(args, msg);
|
||||
int len = vsnprintf(buf, sizeof(buf), msg, args);
|
||||
callback_func(ctx, append_msg, PREFIX_WARNING, msg, args);
|
||||
va_end(args);
|
||||
|
||||
if (len > 0) {
|
||||
t->append("XML Warning: " + std::string(buf));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void error_runtime(void *ctx, const char *msg, ...) {
|
||||
const Transaction *t = reinterpret_cast<Transaction *>(ctx);
|
||||
char buf[1024];
|
||||
std::string s;
|
||||
va_list args;
|
||||
|
||||
va_start(args, msg);
|
||||
int len = vsnprintf(buf, sizeof(buf), msg, args);
|
||||
callback_func(ctx, log_msg, PREFIX_ERROR, msg, args);
|
||||
va_end(args);
|
||||
|
||||
if (len > 0) {
|
||||
s = "XML Error: " + std::string(buf);
|
||||
}
|
||||
ms_dbg_a(t, 4, s);
|
||||
}
|
||||
|
||||
|
||||
static void warn_runtime(void *ctx, const char *msg, ...) {
|
||||
const Transaction *t = reinterpret_cast<Transaction *>(ctx);
|
||||
char buf[1024];
|
||||
std::string s;
|
||||
va_list args;
|
||||
|
||||
va_start(args, msg);
|
||||
int len = vsnprintf(buf, sizeof(buf), msg, args);
|
||||
callback_func(ctx, log_msg, PREFIX_WARNING, msg, args);
|
||||
va_end(args);
|
||||
|
||||
if (len > 0) {
|
||||
s = "XML Warning: " + std::string(buf);
|
||||
}
|
||||
ms_dbg_a(t, 4, s);
|
||||
}
|
||||
|
||||
static void null_error(void *ctx, const char *msg, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
|
||||
static void null_error(void *, const char *, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
|
||||
}
|
||||
|
||||
template<typename Pred>
|
||||
static void callback_func(void *ctx, Pred pred, const char *base_msg, const char *msg, va_list args) {
|
||||
char buf[1024];
|
||||
const auto len = vsnprintf(buf, sizeof(buf), msg, args);
|
||||
|
||||
if (len > 0)
|
||||
pred(ctx, base_msg + std::string(buf));
|
||||
}
|
||||
|
||||
static void log_msg(const void *ctx, const std::string &msg) {
|
||||
auto t = reinterpret_cast<const Transaction *>(ctx);
|
||||
ms_dbg_a(t, 4, msg);
|
||||
}
|
||||
|
||||
static void append_msg(void *ctx, const std::string &msg) {
|
||||
auto s = reinterpret_cast<std::string*>(ctx);
|
||||
s->append(msg);
|
||||
}
|
||||
|
||||
static constexpr auto PREFIX_WARNING = "XML Warning: ";
|
||||
static constexpr auto PREFIX_ERROR = "XML Error: ";
|
||||
|
||||
private:
|
||||
std::string m_resource;
|
||||
std::string m_err;
|
||||
|
Loading…
x
Reference in New Issue
Block a user