diff options
author | Milo Yip <miloyip@gmail.com> | 2016-04-21 04:29:48 +0300 |
---|---|---|
committer | Milo Yip <miloyip@gmail.com> | 2016-04-21 04:29:48 +0300 |
commit | f191cd9dfcf58efd10439e65c7c14d5fbaa5ff38 (patch) | |
tree | 8eebcc359859ab354f3fcd741198296a1a014024 | |
parent | 0fe08c222fc7170f67be5a079921e2d70efbcc1c (diff) |
Fix potential memory leak of RAPIDJSON_SCHEMA_USE_STDREGEXregex
-rw-r--r-- | include/rapidjson/schema.h | 13 | ||||
-rw-r--r-- | test/unittest/schematest.cpp | 2 |
2 files changed, 11 insertions, 4 deletions
diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 0a8bb7c5..36846186 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -32,11 +32,13 @@ RAPIDJSON_DIAG_OFF(c++98-compat-pedantic) #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 @@ private: #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 d1027ad5..e65a3857 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -352,7 +352,7 @@ TEST(SchemaValidator, String_Pattern) { 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); |