Skip to content

Commit

Permalink
Refactor to remove duplicate code in ValidateSchema & ValidateDTD
Browse files Browse the repository at this point in the history
- Reported by Sonarcloud
  • Loading branch information
eduar-hte committed Oct 19, 2024
1 parent 2fb446a commit da28535
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 57 deletions.
25 changes: 4 additions & 21 deletions src/operators/validate_dtd.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <utility>

#include "src/operators/operator.h"
#include "validate_schema.h"


namespace modsecurity {
Expand Down Expand Up @@ -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:
Expand Down
62 changes: 26 additions & 36 deletions src/operators/validate_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *, const char *, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
}

static void null_error(void *ctx, const char *msg, ...) { // 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(void *ctx, const std::string &msg) {
auto t = reinterpret_cast<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;
Expand Down

0 comments on commit da28535

Please sign in to comment.