Fix potential memory leak of RAPIDJSON_SCHEMA_USE_STDREGEX
diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h
index 0a8bb7c..3684618 100644
--- a/include/rapidjson/schema.h
+++ b/include/rapidjson/schema.h
@@ -32,11 +32,13 @@
#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 0
#endif
-#if !RAPIDJSON_SCHEMA_USE_INTERNALREGEX && !defined(RAPIDJSON_SCHEMA_USE_STDREGEX) && (__cplusplus >=201103L || (defined(_MSC_VER) && _MSC_VER >= 1800))
+#if !defined(RAPIDJSON_SCHEMA_USE_STDREGEX)
+#if !RAPIDJSON_SCHEMA_USE_INTERNALREGEX && (__cplusplus >=201103L || (defined(_MSC_VER) && _MSC_VER >= 1800))
#define RAPIDJSON_SCHEMA_USE_STDREGEX 1
#else
#define RAPIDJSON_SCHEMA_USE_STDREGEX 0
#endif
+#endif
#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX
#include "internal/regex.h"
@@ -1017,12 +1019,17 @@
#elif RAPIDJSON_SCHEMA_USE_STDREGEX
template <typename ValueType>
RegexType* CreatePattern(const ValueType& value) {
- if (value.IsString())
+ if (value.IsString()) {
+ RegexType* r = static_cast<RegexType*>(allocator_->Malloc(sizeof(RegexType)));
try {
- return new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString(), std::size_t(value.GetStringLength()), std::regex_constants::ECMAScript);
+ new (r) RegexType(value.GetString(), std::size_t(value.GetStringLength()), std::regex_constants::ECMAScript);
}
catch (const std::regex_error&) {
+ AllocatorType::Free(r);
+ r = 0;
}
+ return r;
+ }
return 0;
}
diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp
index d1027ad..e65a385 100644
--- a/test/unittest/schematest.cpp
+++ b/test/unittest/schematest.cpp
@@ -352,7 +352,7 @@
TEST(SchemaValidator, String_Pattern_Invalid) {
Document sd;
- sd.Parse("{\"type\":\"string\",\"pattern\":\"a{0}\"}"); // TODO: report regex is invalid somehow
+ sd.Parse("{\"type\":\"string\",\"pattern\":\"a{}\"}"); // TODO: report regex is invalid somehow
SchemaDocument s(sd);
VALIDATE(s, "\"\"", true);