Skip to content

Commit f191cd9

Browse files
committed
Fix potential memory leak of RAPIDJSON_SCHEMA_USE_STDREGEX
1 parent 0fe08c2 commit f191cd9

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

include/rapidjson/schema.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ RAPIDJSON_DIAG_OFF(c++98-compat-pedantic)
3232
#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 0
3333
#endif
3434

35-
#if !RAPIDJSON_SCHEMA_USE_INTERNALREGEX && !defined(RAPIDJSON_SCHEMA_USE_STDREGEX) && (__cplusplus >=201103L || (defined(_MSC_VER) && _MSC_VER >= 1800))
35+
#if !defined(RAPIDJSON_SCHEMA_USE_STDREGEX)
36+
#if !RAPIDJSON_SCHEMA_USE_INTERNALREGEX && (__cplusplus >=201103L || (defined(_MSC_VER) && _MSC_VER >= 1800))
3637
#define RAPIDJSON_SCHEMA_USE_STDREGEX 1
3738
#else
3839
#define RAPIDJSON_SCHEMA_USE_STDREGEX 0
3940
#endif
41+
#endif
4042

4143
#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX
4244
#include "internal/regex.h"
@@ -1017,12 +1019,17 @@ class Schema {
10171019
#elif RAPIDJSON_SCHEMA_USE_STDREGEX
10181020
template <typename ValueType>
10191021
RegexType* CreatePattern(const ValueType& value) {
1020-
if (value.IsString())
1022+
if (value.IsString()) {
1023+
RegexType* r = static_cast<RegexType*>(allocator_->Malloc(sizeof(RegexType)));
10211024
try {
1022-
return new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString(), std::size_t(value.GetStringLength()), std::regex_constants::ECMAScript);
1025+
new (r) RegexType(value.GetString(), std::size_t(value.GetStringLength()), std::regex_constants::ECMAScript);
10231026
}
10241027
catch (const std::regex_error&) {
1028+
AllocatorType::Free(r);
1029+
r = 0;
10251030
}
1031+
return r;
1032+
}
10261033
return 0;
10271034
}
10281035

test/unittest/schematest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ TEST(SchemaValidator, String_Pattern) {
352352

353353
TEST(SchemaValidator, String_Pattern_Invalid) {
354354
Document sd;
355-
sd.Parse("{\"type\":\"string\",\"pattern\":\"a{0}\"}"); // TODO: report regex is invalid somehow
355+
sd.Parse("{\"type\":\"string\",\"pattern\":\"a{}\"}"); // TODO: report regex is invalid somehow
356356
SchemaDocument s(sd);
357357

358358
VALIDATE(s, "\"\"", true);

0 commit comments

Comments
 (0)