Skip to content

Commit d1e7e7b

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

File tree

2 files changed

+30
-57
lines changed

2 files changed

+30
-57
lines changed

src/operators/validate_dtd.h

Lines changed: 4 additions & 21 deletions
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,40 +63,22 @@ 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

98-
static void null_error(void *ctx, const char *msg, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
81+
static void null_error(void *, const char *, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
9982
}
10083

10184
private:

src/operators/validate_schema.h

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,71 +45,61 @@ 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+
}
10377

104-
if (len > 0) {
105-
s = "XML Warning: " + std::string(buf);
106-
}
107-
ms_dbg_a(t, 4, s);
78+
static void null_error(void *, const char *, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
10879
}
10980

110-
static void null_error(void *ctx, const char *msg, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
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);
85+
86+
if (len > 0)
87+
pred(ctx, base_msg + std::string(buf));
11188
}
11289

90+
static void log_msg(const void *ctx, const std::string &msg) {
91+
auto t = reinterpret_cast<const Transaction *>(ctx);
92+
ms_dbg_a(t, 4, msg);
93+
}
94+
95+
static void append_msg(void *ctx, const std::string &msg) {
96+
auto s = reinterpret_cast<std::string*>(ctx);
97+
s->append(msg);
98+
}
99+
100+
static constexpr auto PREFIX_WARNING = "XML Warning: ";
101+
static constexpr auto PREFIX_ERROR = "XML Error: ";
102+
113103
private:
114104
std::string m_resource;
115105
std::string m_err;

0 commit comments

Comments
 (0)