Skip to content

Commit 8b865f3

Browse files
committed
Refactor to remove duplicate code in ValidateSchema & ValidateDTD
- Reported by Sonarcloud
1 parent 2fb446a commit 8b865f3

File tree

2 files changed

+29
-54
lines changed

2 files changed

+29
-54
lines changed

src/operators/validate_dtd.h

+3-20
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <utility>
2929

3030
#include "src/operators/operator.h"
31+
#include "validate_schema.h"
3132

3233

3334
namespace modsecurity {
@@ -62,36 +63,18 @@ class ValidateDTD : public Operator {
6263

6364

6465
static void error_runtime(void *ctx, const char *msg, ...) {
65-
const Transaction *t = reinterpret_cast<Transaction *>(ctx);
66-
char buf[1024];
67-
std::string s;
6866
va_list args;
69-
7067
va_start(args, msg);
71-
int len = vsnprintf(buf, sizeof(buf), msg, args);
68+
ValidateSchema::callback_func(ctx, ValidateSchema::log_msg, ValidateSchema::PREFIX_ERROR, msg, args);
7269
va_end(args);
73-
74-
if (len > 0) {
75-
s = "XML Error: " + std::string(buf);
76-
}
77-
ms_dbg_a(t, 4, s);
7870
}
7971

8072

8173
static void warn_runtime(void *ctx, const char *msg, ...) {
82-
const Transaction *t = reinterpret_cast<Transaction *>(ctx);
83-
char buf[1024];
84-
std::string s;
8574
va_list args;
86-
8775
va_start(args, msg);
88-
int len = vsnprintf(buf, sizeof(buf), msg, args);
76+
ValidateSchema::callback_func(ctx, ValidateSchema::log_msg, ValidateSchema::PREFIX_WARNING, msg, args);
8977
va_end(args);
90-
91-
if (len > 0) {
92-
s = "XML Warning: " + std::string(buf);
93-
}
94-
ms_dbg_a(t, 4, s);
9578
}
9679

9780

src/operators/validate_schema.h

+26-34
Original file line numberDiff line numberDiff line change
@@ -45,71 +45,63 @@ class ValidateSchema : public Operator {
4545

4646

4747
static void error_load(void *ctx, const char *msg, ...) {
48-
std::string *t = reinterpret_cast<std::string *>(ctx);
49-
char buf[1024];
5048
va_list args;
51-
5249
va_start(args, msg);
53-
int len = vsnprintf(buf, sizeof(buf), msg, args);
50+
callback_func(ctx, append_msg, PREFIX_ERROR, msg, args);
5451
va_end(args);
55-
56-
if (len > 0) {
57-
t->append("XML Error: " + std::string(buf));
58-
}
5952
}
6053

6154

6255
static void warn_load(void *ctx, const char *msg, ...) {
63-
std::string *t = reinterpret_cast<std::string *>(ctx);
64-
char buf[1024];
6556
va_list args;
66-
6757
va_start(args, msg);
68-
int len = vsnprintf(buf, sizeof(buf), msg, args);
58+
callback_func(ctx, append_msg, PREFIX_WARNING, msg, args);
6959
va_end(args);
70-
71-
if (len > 0) {
72-
t->append("XML Warning: " + std::string(buf));
73-
}
7460
}
7561

7662

7763
static void error_runtime(void *ctx, const char *msg, ...) {
78-
const Transaction *t = reinterpret_cast<Transaction *>(ctx);
79-
char buf[1024];
80-
std::string s;
8164
va_list args;
82-
8365
va_start(args, msg);
84-
int len = vsnprintf(buf, sizeof(buf), msg, args);
66+
callback_func(ctx, log_msg, PREFIX_ERROR, msg, args);
8567
va_end(args);
86-
87-
if (len > 0) {
88-
s = "XML Error: " + std::string(buf);
89-
}
90-
ms_dbg_a(t, 4, s);
9168
}
9269

9370

9471
static void warn_runtime(void *ctx, const char *msg, ...) {
95-
const Transaction *t = reinterpret_cast<Transaction *>(ctx);
96-
char buf[1024];
97-
std::string s;
9872
va_list args;
99-
10073
va_start(args, msg);
101-
int len = vsnprintf(buf, sizeof(buf), msg, args);
74+
callback_func(ctx, log_msg, PREFIX_WARNING, msg, args);
10275
va_end(args);
76+
}
77+
78+
static void null_error(void *ctx, const char *msg, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
79+
}
80+
81+
template<typename Pred>
82+
static void callback_func(void *ctx, Pred pred, const char *base_msg, const char *msg, va_list args) {
83+
char buf[1024];
84+
const auto len = vsnprintf(buf, sizeof(buf), msg, args);
10385

10486
if (len > 0) {
105-
s = "XML Warning: " + std::string(buf);
87+
const auto msg = base_msg + std::string(buf);
88+
pred(ctx, msg);
10689
}
107-
ms_dbg_a(t, 4, s);
10890
}
10991

110-
static void null_error(void *ctx, const char *msg, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
92+
static void log_msg(void *ctx, const std::string &msg) {
93+
auto t = reinterpret_cast<const Transaction *>(ctx);
94+
ms_dbg_a(t, 4, msg);
11195
}
11296

97+
static void append_msg(void *ctx, const std::string &msg) {
98+
auto s = reinterpret_cast<std::string*>(ctx);
99+
s->append(msg);
100+
}
101+
102+
static constexpr auto PREFIX_WARNING = "XML Warning: ";
103+
static constexpr auto PREFIX_ERROR = "XML Error: ";
104+
113105
private:
114106
std::string m_resource;
115107
std::string m_err;

0 commit comments

Comments
 (0)